Supposed I have a Docker container and a folder on my host /hostFolder
. Now if I want to add this folder to the Docker container as a volume, then I can do this either by using ADD
in the Dockerfile
or mounting it as a volume.
So far, so good.
Now /hostFolder
contains a sub-folder, /hostFolder/subFolder
.
I want to mount /hostFolder
into the Docker container (whether as read-write or read-only does not matter, works both for me), but I do NOT want to have it included /hostFolder/subFolder
. I want to exclude this, and I also want the Docker container be able to make changes to this sub-folder, without the consequence of having it changed on the host as well.
Is this possible? If so, how?
Docker volume and bind mounts are used to bind directories on the host OS to locations in the container's file system. While they're commonly used to mount entire directories, you can also use them to symlink individual files.
Getting started using bind mounts The most notable difference between the two options is that --mount is more verbose and explicit, whereas -v is more of a shorthand for --mount . It combines all the options you pass to --mount into one field.
In Dockerfile you can specify only the destination of a volume inside a container. e.g. /usr/src/app . When you run a container, e.g. docker run --volume=/opt:/usr/src/app my_image , you may but do not have to specify its mounting point ( /opt ) on the host machine.
Using docker-compose I'm able to use node_modules locally, but ignore it in the docker container using the following syntax in the docker-compose.yml
volumes: - './angularApp:/opt/app' - /opt/app/node_modules/
So everything in ./angularApp
is mapped to /opt/app
and then I create another mount volume /opt/app/node_modules/
which is now empty directory - even if in my local machine ./angularApp/node_modules
is not empty.
If you want to have subdirectories ignored by docker-compose but persistent, you can do the following in docker-compose.yml
:
volumes: node_modules: services: server: volumes: - .:/app - node_modules:/app/node_modules
This will mount your current directory as a shared volume, but mount a persistent docker volume in place of your local node_modules
directory. This is similar to the answer by @kernix, but this will allow node_modules
to persist between docker-compose up
runs, which is likely the desired behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With