How to add a volume to Docker but also exclude a sub-folder

+2 votes
I want to add some folder to the Docker container as a volume, I know that I can do this either by using ADD in the Dockerfile or by mounting it as a volume.

Now that folder is having a sub-folder, /someFolder/subFolder.

I want to mount /someFolder into the Docker container but I don't want to have it included /hostFolder/subFolder. I want to exclude this, and I also want the Docker container to be able to make changes to this sub-folder, without the consequence of having it changed on the host as well.

Can anyone help me with this?

Thanks
Nov 21, 2018 in DevOps & Agile by Damon Salvatore
• 5,980 points
25,654 views

6 answers to this question.

+2 votes
Best answer

Here is what you can try if you want to have subdirectories ignored by docker-compose but persistent, 

volumes:
  node_modules:
services:
  server:
    volumes:
      - .:/app
      - node_modules:/app/node_modules

the above YAML file definition will mount your current directory as a shared volume but mount a persistent docker volume in place of your local node_modules directory.

I hope that the above explanation will resolve your query.

answered Nov 21, 2018 by Shubham
• 1,000 points

selected May 7, 2019 by Kalgi
+1 vote

This is very similar to @Shubham's answer but a little bit different.

You can use node_modules for this purpose. Have a look at the following example:

volumes:
   - './angularApp:/opt/app'
   - /opt/app/node_modules/

Here everything in ./angularApp is mapped to /opt/app. And then I've created another volume /opt/app/node_modules/ which is an empty directory but when used locally it'll have all the files that are in /opt/app.

answered May 7, 2019 by Ishwar
This did not work
Yeah it hasn't worked for a lot of people. You can follow @Siddhart's approach. It will work

Or else create an empty folder and map it to a target folder.

volumes:
   - ./angularApp:/opt/app
   - .empty:/opt/app/node_modules/
+1 vote

You can exclude a file by using something like this:

volumes:
   - /hostFolder:/folder
   - /dev/null:/folder/fileToBeExcluded
answered May 7, 2019 by Barbara
+1 vote

Instead of using Volume, you can use COPY. You can use it with dockerignore file to exclude the subdirectories. 

By using the .dockerignore file which is mentioned at the end of a Dockerfile, all the unwanted files can be excluded from the final image. 

This is how your dockerignore file should look:

.*
docs
mkdocs.yml
docker-compose.yml
test*
*.md
answered May 7, 2019 by Laila
+1 vote
There is another very simple approach. What you can do is COPY the entire folder and run a cp -rm command to remove the subdirectories that are not needed. I am not sure if this affects the performance but it works for sure.
answered May 7, 2019 by Siddhart
+1 vote

create an empty folder and map it to a target folder.

volumes:
   - ./angularApp:/opt/app
   - .empty:/opt/app/node_modules/
answered May 7, 2019 by Anand

Related Questions In DevOps & Agile

+1 vote
2 answers

How do I run Apache server from Docker without mapping to a location?

If you're seeing a 500 error, that's ...READ MORE

answered Oct 18, 2018 in DevOps & Agile by lina
• 8,220 points
2,193 views
0 votes
1 answer

How to get remote access to a private docker-registry?

New client version refuses to work with ...READ MORE

answered Jul 31, 2018 in DevOps & Agile by Kalgi
• 52,350 points
2,380 views
0 votes
1 answer

How to a run deployed app in Docker for Windows?

To deploy an ASP.NET web application to ...READ MORE

answered Aug 30, 2018 in DevOps & Agile by Tyrion anex
• 8,700 points
1,217 views
0 votes
1 answer

How to edit file after I shell to a docker container?

Since docker images are trimmed to bare minimum ...READ MORE

answered Sep 11, 2018 in DevOps & Agile by Damon Salvatore
• 5,980 points
3,788 views
0 votes
1 answer

How do I push a docker image to a private repository?

Here is what you can do. Just try ...READ MORE

answered Sep 19, 2018 in DevOps & Agile by Damon Salvatore
• 5,980 points
1,388 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
2,715 views
0 votes
1 answer

How to delete multiple docker images at once?

Here is what you can try. There is ...READ MORE

answered Nov 21, 2018 in DevOps & Agile by Shubham
• 1,000 points
4,188 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP