0

When running "docker-compose up", I get the following error:

npm info lifecycle server@1.0.0~dev: server@1.0.0

> server@1.0.0 dev /code/app
> nodemon -L ./bin/www --exec babel-node

sh: 0: getcwd() failed: No such file or directory
path.js:1144
      cwd = process.cwd();
                    ^

Error: ENOENT: no such file or directory, uv_cwd at Error (native)
at Object.resolve (path.js:1144:25)
at Function.Module._resolveLookupPaths (module.js:361:17)
at Function.Module._resolveFilename (module.js:431:31)
at Function.Module._load (module.js:388:25)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> 
(/usr/local/lib/node_modules/nodemon/bin/nodemon.js:3:11)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)

npm info lifecycle server@1.0.0~dev: Failed to exec dev script
npm ERR! Linux 4.9.36-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "dev"
npm ERR! node v6.3.1
npm ERR! npm  v3.10.3
npm ERR! code ELIFECYCLE
npm ERR! server@1.0.0 dev: `nodemon -L ./bin/www --exec babel-node`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the server@1.0.0 dev script 'nodemon -L ./bin/www --
exec babel-node'.

My dockerfile looks like this:

FROM joakimbeng/node-zeromq

RUN mkdir /code/
RUN mkdir /code/app/
COPY package.json /code/

WORKDIR /code
RUN npm install -g nodemon babel-cli
RUN npm install

WORKDIR /code/app

CMD ["npm", "run", "dev"]

And my service like this:

node:
    build: ./node/
    ports:
      - "3000:3000"
    volumes:
      - ../code:/code/app
    links:
      - mongodb
      - python
    environment:
      - NODE_ENV=dev
      - NODE_PATH=/code/node_modules
      - MONGODB_ADDRESS=mongodb
      - PYTHON_ADDRESS=python

I've tried to delete all containers and images and run the whole thing again, but the same error appears. It seems to build fine when running "docker-compose build".

What I'm trying to accomplish here is: 1. Let the container handle all the dependencies (node modules) 2. Mount my code base to the container 3. Use nodemon for hot reload

mrlarssen
  • 285
  • 6
  • 19
  • I think another possible solution to this problem is explained [here](https://stackoverflow.com/questions/30043872/docker-compose-node-modules-not-present-in-a-volume-after-npm-install-succeeds/32785014#32785014) – jonathan-dev Aug 14 '17 at 02:11

2 Answers2

2

I ended up with something similar to what I did initially. Not sure what caused the error in my OP, but the difference seems to be that I mount my dependencies in a different directory.

Dockerfile:

FROM joakimbeng/node-zeromq

RUN mkdir /code/
RUN mkdir /dependencies/
COPY package.json /dependencies/

WORKDIR /dependencies/
RUN npm install -g nodemon babel-cli
npm install

WORKDIR /code/
CMD bash -c "npm run dev"

Service in docker-compose:

  node:
build: ./node/
ports:
  - "3000:3000"
volumes:
  - ../code/:/code
links:
  - mongodb
  - python
environment:
  - NODE_ENV=dev
  - NODE_PATH=/dependencies/node_modules
  - MONGODB_ADDRESS=mongodb
  - PYTHON_ADDRESS=python

This way my dependencies are only installed on build.

mrlarssen
  • 285
  • 6
  • 19
0

Your issue is the volume sharing. When you share a volume from host to the container. If the folder already exists in the container then the host container will shadow the container folder.

If you have 10 files inside container and 0 files on your host, then after volume mapping your container will see 0 files. Because the the host folder is mounted and it has nothing. So you Dockerfile statement

RUN npm install

Is effectively gone, if the host volume doesn't have the npm install done. Luckily the solution is simple. You can change your CMD to below

CMD bash -c "npm install && npm run dev"

In case you don't want to change the Dockerfile you can add the below in your docker-compose.yml file for the node service

command: bash -c "npm install && npm run dev"

Edit (14-Aug):

If you want your dependencies to be in image then you need to make few changes in your docker-compose.yml, what you need is the internal code to be left alone and just linking the node_modules from that directory to a you app directory

node:
    build: ./node/
    ports:
      - "3000:3000"
    volumes:
      - ../code:/code/app
    command: bash -c "ln -fs /code/node_modules /code/app/node_modules && exec npm run dev"
    links:
      - mongodb
      - python
    environment:
      - NODE_ENV=dev
      - NODE_PATH=/code/node_modules
      - MONGODB_ADDRESS=mongodb
      - PYTHON_ADDRESS=python

Another point i notice is that your running package.json install in /code and putting your code /code/app which is probably wrong when you run the image. But with the new edit I have suggested above, this should work

Tarun Lalwani
  • 124,930
  • 8
  • 149
  • 214
  • This solution works, but results in npm install running on each "docker-compose up", which is not preferable. Dependencies should be installed when I build the image. – mrlarssen Aug 14 '17 at 07:40
  • Thank you. Never thought of creating a symlink. While investigating I found an alternative approach which also seems to work. See my answer. – mrlarssen Aug 15 '17 at 07:48
  • Not sure if there are any pros and/or cons with the different approaches? – mrlarssen Aug 15 '17 at 07:54
  • I think both approaches are fine, I didn't realize the `NODE_PATH` environment variable in your compose, else would have suggested the same thing you did – Tarun Lalwani Aug 15 '17 at 07:56