Docker Community Forums

Share and learn in the Docker community.

Disclude a subdir from a bind mount

Hi!

I’ve got a Ruby on Rails project that uses nodejs, and has a large node_modules dir. I need to sync the whole project dir between my host and my docker container, but I don’t need this node_modules folder synced back to my host, and I’d like to save some performance by not syncing it in my bind mount.

I read on this SO answer that by listing that subdir as a volume with no named volume or host side bind mount, docker would not try to sync it back to the host.

I also have a named volume to cache the node_modules so I can blow away my containers without redownloading all my node_modules.

Here’s an example of my app container’s volumes from docker-compose.yml:

volumes:
  - ./:/opt/project
  - node_modules:/opt/project/client/node_modules
  # Exclude directories from bind mounts. Access inside container or `docker cp`.
  - /opt/project/client/node_modules/

However, because of https://github.com/docker/compose/pull/6406, this configuration now raises an error, because we have the same container dir mapped to two different mounts.

Is there a better way to do this I’m not aware of? Or am I requesting a feature here?
OS: MacOS 10.13.6, though the coworker experiencing the issue is on Linux, who has the 1.24.0 docker-compose release. Docker for Mac still uses docker-compose 1.23.2.
App Version: Docker-compose 1.24.0

Thank you!

3 Likes

Docker uses a linux bind mount to put the volume into the container. This specific node_modules problems has a few solutions, and this block post I found sums up the options quiet well: https://nickjanetakis.com/blog/docker-tip-75-how-to-avoid-node-modules-in-your-volume-mounts

Alternatively, you could utilize a symlink to get the node_modules to point to a different location that made sense both inside the container and outside it.

1 Like

Excellent, I’ll give that a shot. Thanks so much for the quick reply!
I was going to ask if we could apply the same thing for the project/tmp dir, but I suppose we can configure that too within rails:

Thank you!