Search code examples
node.jsdockernpmdocker-composenodemon

Error when running node container using docker compose


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


Solution

  • 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.