Le garcon
Le garcon

Reputation: 8377

Getting error: nodemon not found when running docker-compose

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?

Upvotes: 3

Views: 3231

Answers (1)

Nicolas
Nicolas

Reputation: 1263

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 :)

Upvotes: 6

Related Questions