1

I use docker-compose (version 1.24.0) to run my Node.js App inside a container. In development mode I mount my src volume, but I'd like the container to use a node_modules directory that is not created on my host, just locally within the container.

Excerpt from my docker-compose

volumes:
  - .:/usr/src/app
command:
 - /bin/sh
 - -c
 - |
   npm install
   npm run dev

Is there a way to make the sub-directory /usr/src/app/node_modules local to the container and have npm install not create and populate the directory ./node_modules on the host?

I tried this solution posted on Stackoverflow, somehow the directory node_modules still gets created and populated on my host upon running npm install . inside the container. I used the following modified docker-compose.yml:

volumes:
  - .:/usr/src/app
  - /usr/src/app/node_modules
command:
 - /bin/sh
 - -c
 - |
   npm install
   npm run dev

I even tried using named volumes like so:

  volumes:
    - .:/usr/src/app
    - node_modules:/usr/src/app/node_modules

volumes:
- node_modules

Help will be much appreciated.

user3139868
  • 393
  • 2
  • 12
  • 1
    Possible duplicate of [Add a volume to Docker, but exclude a sub-folder](https://stackoverflow.com/questions/29181032/add-a-volume-to-docker-but-exclude-a-sub-folder) – leopal Apr 02 '19 at 11:20
  • @leopal: This is _not_ a duplicate of [Add a volume to Docker, but exclude a sub-folder](https://stackoverflow.com/questions/29181032/add-a-volume-to-docker-but-exclude-a-sub-folder). I tried that solution and it did not work for me, even though I am using latest `docker-compose`. – user3139868 Apr 02 '19 at 11:44
  • Does your volumes: - node_modules compile without complaining? Try volumes: node_modules: (drop the - in front, add : after) – Daniel M May 30 '19 at 15:16

0 Answers0