2

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.

2
  • 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. Commented May 1, 2017 at 21:57
  • Checked. Files inside docker container are not updated. Commented May 1, 2017 at 22:18

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked for me. Also thank you for best practices question answer!
I couldn't believe I didn't see the difference in my docker build work directory and the compose mount target path. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.