4

I'm attempting to use Docker Compose to bring together a number of Node.js apps in my development environment. I'm running into an issue, however, with node_modules.

Here's what's happening:

  1. npm install is run as part of the Dockerfile.
  2. I do not have node_modules in my local directory. (I shouldn't because the installation of dependencies should happen in the container, right? It seems to defeat the purpose otherwise, since I'd need to have Node.js installed locally.)
  3. In docker-compose.yml, I'm setting up a volume with the source code.
  4. docker-compose build runs fine.
  5. When I docker-compose up, the node_modules directory disappears in the container — I'm assuming because the volume is mounted and I don't have it in my local directory.

How do I ensure that node_modules sticks around?

Dockerfile

FROM       node:0.10.37

COPY       package.json /src/package.json

WORKDIR    /src

RUN        npm install -g grunt-cli && npm install

COPY       . /src

EXPOSE     9001

CMD        ["npm", "start"]

docker-compose.yml

api:
  build: .
  command: grunt
  links:
    - elasticsearch
  ports:
    - "9002:9002"
  volumes:
    - .:/src

elasticsearch:
  image: elasticsearch:1.5
Justin Stayton
  • 5,261
  • 8
  • 33
  • 43
  • 1
    Possible duplicate of [Docker-compose: node\_modules not present in a volume after npm install succeeds](http://stackoverflow.com/questions/30043872/docker-compose-node-modules-not-present-in-a-volume-after-npm-install-succeeds) – FrederikNS Feb 29 '16 at 12:29
  • Justin did you figure this out, as I appear to have a very similar problem that I simply cannot get working: http://stackoverflow.com/questions/38406573/docker-compose-volume-mapping-with-nodejs-app - it works without the volume mapping, but that is no good since I have to `build` every time I change anything, which is too time consuming. – Joseph McDermott Jul 16 '16 at 10:42
  • @JosephMcDermott http://stackoverflow.com/a/32785014/7596 is the best solution I've found. – Justin Stayton Jul 20 '16 at 21:33

2 Answers2

3

Due to the way Node.js loads modules, simply place node_modules higher in the source code path. For example, put your source at /app/src and your package.json in /app, so /app/node_modules is where they're installed.

Justin Stayton
  • 5,261
  • 8
  • 33
  • 43
0

I tried your fix, but the issue is that most people run npm install in the /usr/src/app directory, resulting in the node_modules folder ending up in the /app directory. As a result the node_modules folder ends up in both the /usr/src and /usr/src/app directory in the container and you end up with the same issue you started with.

Brodan
  • 147
  • 17
  • Yes, I ended up running into this as well. I tried really hard to keep everything in the container (including the development flow, like build tools), but I don't think it's feasible realistically. I now run the development flow back on my host machine, so I do need `node_modules`, which means it's synced to `/app/src` as you said. So far it's not a problem, but I can see it being one if something is built on my host machine (OS X) that's not compatible with the container (Ubuntu). – Justin Stayton Jul 10 '15 at 21:44