Skip to content

Reactive docker to source code changes: nodemon in docker compose does not work #25

Open
@SeyyedKhandon

Description

@SeyyedKhandon

I was trying to use the dockerfile config for nodemon from https://containers-v2.holt.courses/lessons/multi-container-projects/docker-compose , but it does not work,

FROM node:latest

RUN npm i -g nodemon

USER node

RUN mkdir /home/node/code

WORKDIR /home/node/code

COPY --chown=node:node package-lock.json package.json ./

RUN npm ci

COPY --chown=node:node . .

CMD ["nodemon", "index.js"]

I guess the issue is, since we have copied the files into the docker, therefore by changing files in our editor, these changes wont end up inside the running docker, therefore nodemon see no changes and there will be no updates.

Therefore in order to make it react to our local files, we need to make the source code available to the docker by mounting it with the docker-compose:

v1:

    volumes:
      - ./api:/home/node/code # Mount host's ./api folder into the container and you have to make sure that you have node_module in /api folder of your editor,

The issue with version 1, is you may have some conflicts between the packages for your local machine and the docker os

v2:

    volumes:
      - ./api:/home/node/code # Mount your source code
      - /home/node/code/node_modules # Don't overwrite node_modules directory of api docker image

The problem with version2 is, if you install a new lib or remove something from it, it wont gets updated in the node_module of your docker

v3:

    volumes:
      - ./api/index.js:/home/node/code/index.js # Mount host's ./api/index.js into the container

the problem with version 3 is like v2 and also is super limited.

Final version:

services:
  api:
    build: api
    ports:
      - "8080:8080"
    links:
      - db
    environment:
      MONGO_CONNECTION_STRING: mongodb://db:27017
    volumes:
      - ./api:/home/node/code # Mount your source code
      - /home/node/code/node_modules # Don't overwrite node_modules directory of api docker image
  db:
    image: mongo:7
  web:
    build: web
    ports:
      - "8081:80"

What is your suggestion?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions