2
votes

I try to reload my node js app code inside docker container. I use pm2 as process manager. Here is my configurations: Dockerfile

FROM node:6.9.5

LABEL maintainer "[email protected]"

RUN mkdir -p /usr/src/koa2-app

COPY . /usr/src/koa2-app

WORKDIR /usr/src/koa2-app

RUN npm i -g pm2

RUN npm install

EXPOSE 9000

CMD [ "npm", "run", "production"]

ecosystem.prod.config.json (aka pm2 config)

{
  "apps" : [
    {
      "name"        : "koa2-fp",
      "script"      : "./bin/www.js",
      "watch"       : true,
      "merge_logs"  : true,
      "log_date_format": "YYYY-MM-DD HH:mm Z",
      "env": {
        "NODE_ENV": "production",
        "PROTOCOL": "http",
        "APP_PORT": 3000
      },
      "instances": 4,
      "exec_mode"  : "cluster_mode",
      "autorestart": true
    }
  ]
}

docker-compose.yaml

version: "3"
services:
  web:
    build: .
    volumes:
      - ./:/koa2-app
    ports:
      - "3000:3000"

npm run production - pm2 start --attach ecosystem.prod.config.json

I run 'docker-compose up' in the CLI and it works, I'm able to interact with my app on localhost:3000. But if make some change to code it will not show up in web. How can I configure code reloading inside docker?

P.S. And best practices question: Is it really OK to develop using docker stuff? Or docker containers is most preferable for production use.

1
Did you verify the files are updated in your container? There are two possible issues here. Either Docker doesn't properly update the files, or your server doesn't detect the change. You can use docker-compose exec web sh to get a shell and verify the files get modified properly.kichik
Checked. Files inside docker container are not updated.TK-95

1 Answers

3
votes

It seems that you COPY your code in one place and the volume is in another place.

Try:

version: "3"
services:
  web:
    build: .
    volumes:
      - ./:/usr/src/koa2-app
    ports:
      - "3000:3000"

Then, when you change the js code outside container (your IDE), now the PM2 is able to see the changes, and therefore reload the application (you need to be sure of that part).

Regarding the use of Docker in development environment: it is a really good thing to do because of many reasons. For instance, you manage the same app installation for different environments reducing a lot of bugs, etc.