Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SeyyedKhandon opened this issue Oct 13, 2024 · 0 comments

Comments

@SeyyedKhandon
Copy link

SeyyedKhandon commented Oct 13, 2024

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?

@SeyyedKhandon SeyyedKhandon changed the title nodemon in docker compose does not work Reactive docker to source code changes: nodemon in docker compose does not work Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant