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,250 points
1,077 views

6 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+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
• 980 points

selected May 7 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 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 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 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 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 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,100 points
50 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
• 35,720 points
46 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 29, 2018 in DevOps & Agile by Tyrion anex
• 8,280 points
73 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,250 points
38 views
0 votes
1 answer
0 votes
1 answer
0 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
• 56,100 points
56 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
• 980 points
49 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.