Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a volume to Docker, but exclude a sub-folder

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?

like image 738
Golo Roden Avatar asked Mar 21 '15 09:03

Golo Roden


People also ask

Can a docker volume be a single file?

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.

What is the difference between BIND and volume in docker?

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.

How do I specify a docker volume?

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.


2 Answers

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.

like image 183
kernix Avatar answered Sep 24 '22 07:09

kernix


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.

like image 34
Nate T Avatar answered Sep 22 '22 07:09

Nate T