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

Docker container requires rebuild for every change in server.js despite using volumes #12190

Open
said96dev opened this issue Oct 7, 2024 · 1 comment

Comments

@said96dev
Copy link

said96dev commented Oct 7, 2024

Description

I’m working on a Node.js backend project, and I’m using Docker with Docker Compose for my setup. My folder structure looks like this:

project-root/
│
├── server/
│   ├── Controllers/
│   ├── db/
│   ├── node_modules/
│   ├── Routers/
│   ├── server.js
│   ├── Dockerfile
│   ├── package.json
│   └── package-lock.json
├── docker-compose.yml
└── Client/

Steps To Reproduce

I’m working on a Node.js backend project, and I’m using Docker with Docker Compose for my setup. My folder structure looks like this:

project-root/
│
├── server/
│   ├── Controllers/
│   ├── db/
│   ├── node_modules/
│   ├── Routers/
│   ├── server.js
│   ├── Dockerfile
│   ├── package.json
│   └── package-lock.json
├── docker-compose.yml
└── Client/

Despite using volumes in my docker-compose.yml to map my local server folder to the container, every time I make a change in server.js, I need to rebuild the Docker container for the changes to take effect. My expectation is that changes should be immediately reflected in the running container without a rebuild, especially since I’m using nodemon to watch for file changes.

docker-compose.yml:


version: '3.8'

services:
  api:
    build:
      context: ./server
      dockerfile: Dockerfile
    volumes:
      - ./server:/app
      - /app/node_modules
    ports:
      - "5000:5000"
    environment:
      - NODE_ENV=development
    command: npm start

Dockerfile

FROM node:16

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm install

COPY . .

CMD ["npm", "start"]

Package.json

{
  "scripts": {
    "start": "nodemon server.js"
  }
}

Compose Version

Docker Compose version v2.29.2-desktop.2

I want changes in my server.js file (and others) to be automatically picked up by the container without needing to rebuild. How can I achieve this? Is there something wrong with my volume setup, or am I missing something else?

Docker Environment

No response

Anything else?

No response

@ndeloof
Copy link
Contributor

ndeloof commented Oct 8, 2024

This should work.
Please run docker compose exec api ls -al /app to double check file is well updated inside container after you made local change.
I wonder: don't you need to run npm in dev mode to get files reloaded ?

As an alternative to bind mount you could use the watch feature to sync ./server with container's /app on filesystem changes.

@ndeloof ndeloof self-assigned this Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants