Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js docker container not updating to changes in volume

I am trying to host a development environment on my Windows machine which hosts a frontend and backend container. So far I have only been working on the backend. All files are on the C Drive which is shared via Docker Desktop.

I have the following docker-compose file and Dockerfile, the latter is inside a directory called backend within the root directory.

Dockerfile:

FROM node:12.15.0-alpine

WORKDIR /usr/app

COPY package*.json ./

RUN npm install

EXPOSE 5000

CMD [ "npm", "start" ]

docker-compose.yml:

version: "3"
services:
  backend:
    container_name: backend
    build:
        context: ./backend
        dockerfile: Dockerfile
    volumes:
      - ./backend:/usr/app
    environment:
      - APP_PORT=80
    ports:
      - '5000:5000'

  client:
    container_name: client
    build:
      context: ./client
      dockerfile: Dockerfile
    volumes:
      - ./client:/app
    ports:
      - '80:8080'

For some reason, when I make changes in my local files they are not reflecting inside the container. I am testing this by slightly modifying the outputs of one of my files, but I am having to rebuild the container each time to see the changes take effect.

I have worked with Docker in PHP applications before, and have basically done the same thing. So I am unsure why this is not working with by Node.js app. I am wondering if I am just missing something glaringly obvious as to why this is not working.

Any help would be appreciated.

like image 347
James Pavett Avatar asked Feb 12 '20 15:02

James Pavett


1 Answers

The difference between node and PHP here is that php automatically picks up file system changes between requests, but a node server doesn't.

I think you'll see that the file changes get picked up if you restart node by bouncing the container with docker-compose down then up (no need to rebuild things!).

If you want node to pick up file system changes without needing to bounce the server you can use some of the node tooling. nodemon is one: https://www.npmjs.com/package/nodemon. Follow the installation instructions for local installation and update your start script to use nodemon instead of node.

Plus I really do think you have a mistake in your dockerfile and you need to copy the source code into your working directory. I'm assuming you got your initial recipe from here: https://dev.to/alex_barashkov/using-docker-for-nodejs-in-development-and-production-3cgp. This is the docker file is below. You missed a step!

FROM node:10-alpine

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

CMD [ "npm", "start" ]
like image 94
Robert Moskal Avatar answered Oct 18 '22 07:10

Robert Moskal