3

I've dockerized a node.js app and created a docker-compose file to run it along with mongo. I'm using docker for development, so for this reason I need nodemon.

Here's my Dockerfile:

FROM node:carbon-alpine

RUN mkdir -p usr/src/app
WORKDIR /usr/src/app

RUN npm install -g nodemon

COPY package*.json /usr/src/app

RUN yarn install

COPY . /usr/src/app

EXPOSE 3000

CMD [ "yarn", "start-local" ]

And here's the docker-compose

version: "3"

services:
  app:
    container_name: app
    restart: always
    build: .
    volumes:
      - .:/usr/src/app
    environment:
      - MONGO_URI=mongodb://mongo:27017/test
    ports:
      - "3000:3000"
    depends_on:
      - mongo
    networks:
      app_net:

  mongo:
    container_name: app-mongo
    image: mongo:3.6.6-jessie
    volumes:
      - ./data:/data/db
    networks:
      app_net:
        aliases:
          - mongo

networks:
  app_net:

When I run docker-compose up I get an error /bin/sh: nodemon: not found. When I run a single container without compose Everything works fine. What's wrong with the docker-compose definition?

2
  • yarn start-local will look for nodemon in your dependencies, make sure in it's in your package.json, under dependencies or devDependencies. Commented Jul 17, 2018 at 14:24
  • @Selfish Because it is installed globally, it's not in dependences. Why then I don't get this error when running container without compose? Commented Jul 17, 2018 at 15:11

1 Answer 1

6

I think your problem may be that you don't have node_modules installed on your host machine. So when you map the host and container file system through the docker-compose volumes, your host overwrites your container's node_modules folder.

In order to avoid that you should ignore that folder, like this:

version: "3"

services:
  app:
    container_name: app
    restart: always
    build: .
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules/
    environment:
      - MONGO_URI=mongodb://mongo:27017/test
    ports:
      - "3000:3000"
    depends_on:
      - mongo
    networks:
      app_net:

  mongo:
    container_name: app-mongo
    image: mongo:3.6.6-jessie
    volumes:
      - ./data:/data/db
    networks:
      app_net:
        aliases:
          - mongo

networks:
  app_net:

Hope it helps :)

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't have installed node_modules on my VPS. After mounted node_modules in volumes, everything started working - thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.