Add a volume to Docker, but exclude a sub-folder
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.
To exclude a file, use the following
volumes: - /hostFolder:/folder - /dev/null:/folder/fileToBeExcluded