Health check of PM2 in Docker

· 1 min read
Health check of PM2 in Docker

PM2 used to have a --web option for health checking. But since version 4.0 it was broken.

To resolve the PM2 health check issue in Docker, we can use pure shell command instead:

version: '3.7'
services:
  queue:
    restart: always
    image: awesome-pm2:latest
    healthcheck:
      test: ["CMD-SHELL", "status=`pm2 jlist | jq '[.[]|select(.name==\"awesome-app\")][0]' | jq -r '.pm2_env' | jq -r '.status'`;if [ \"$$status\" != \"online\" ]; then exit 1; fi"]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 10s
    volumes:
      - ./app:/var/www/html
    depends_on:
      - db
      - redis

FYI:

  • You can name your PM2 process with custom name (in Dockerfile), ie. CMD ["pm2-runtime", "start", "pm2.yaml", "--name", "awesome-app"]
  • You may need to install the jq library in your docker first, eg. in Alpine image, RUN apk add jq.