0

I know we can mount a folder into a docker container like this:

docker run -v /home/user:/home/user <image>

But it will mask everything that is present in the container's /home/user.

Is it possible to mount in such a way that if the file or folder is present in host machine's /home/user/, it will be mounted; everything else will be retained from the container?

For example, suppose I have the following contents in my host and container respectively:

Host

/home/user/.ssh
/home/user/.config
/home/user/code-server  # v2

Container

/home/user/apps/web-server
/home/user/code-server  # v1

I would like to have a union of these after mounting:

/home/user/.ssh
/home/user/.config
/home/user/apps/web-server  # because the container has it.
/home/user/code-server  # because the host has it.

1 Answer 1

1

Short answer: no. You'd need to either create an overlay filesystem outside of the container to mount as a volume (not using container folders), or more likely you want to store files inside the container in a different location and restore them on the container startup.

I've done the latter with the save-volume and load-volume scripts in this base image that you can use to implement this. You save off the volume to a directory during the build process (inside your Dockerfile). And then the entrypoint runs a load to restore files to the volume directory.

1
  • Thanks - I will check those scripts! I was thinking of writing some logic in the entrypoint to do this if there are no other alternatives.
    – Nishant
    Mar 5, 2020 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .