How awesome would it be if we could exclude specific dirs or files from bind mounts. Think of cache directories, log files, and, of course, node_modules.
The common workaround to this, is to create an empty volume and map whatever you don't want mounted to it. You'll end up with an empty directory in your host system, but its contents are available from within the container.
It has many problems though, mostly permission and ownership related, but it somehow makes Docker for Mac even slower and crash even more often than usual.
Example minimal docker-compose.yaml file for a custom "devcontainer" we use in VSCode that showcases the workaround:
version: '3.8'
services:
project:
image: devcontainer:dev
container_name: devcontainer
build:
context: .
dockerfile: Dockerfile
args:
ALPINE_VERSION: '3.13'
NODE_VERSION: '14.16.1'
ports:
- 3000:3000
volumes:
- ..:/project
- node_modules:/project/node_modules:cached
volumes:
node_modules:
This feature gets asked for quite often. Here are two issues on GitHub I was quickly able to find:
https://github.com/docker/compose/issues/6470
https://github.com/docker/compose/issues/6997