Unitech / Unitech/pm2

Next.js app fails to run in PM2 cluster mode within Docker container

Open
#5,955 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

I'm encountering an issue running a Next.js application in a Docker container using PM2's cluster mode. While the application runs successfully in fork mode, it fails when attempting to use cluster mode.

Current Behavior

  • Application fails to start in cluster mode with PM2 inside Docker
  • Works fine in fork mode
  • When executing into the container and running pm2 start manually, it works in cluster mode
  • Only fails when using pm2-runtime which is required for Docker

Environment

  • Next.js version: 14.2.3
  • Node.js version: 19
  • PM2 for process management
  • Docker containerized environment
ecosystem.config.js
module.exports = {
  apps: [
    {
      name: "nextjs",
      cwd: "./",
      script: "node_modules/.bin/next",
      args: "start",
      // instances: "max",
      // exec_mode: "cluster",
      autorestart: true,
      max_memory_restart: "1G",
      env: {
        PORT: 3000,
        NODE_ENV: "production",
        wait_ready: true,
        listen_timeout: 10000,
        NEXT_SKIP_ENV_VALIDATION: true,
        SKIP_ENV_VALIDATION: true,
      },
    },
  ],
};
Dockerfile CMD
CMD ["pm2-runtime", "/ecosystem.config.js", "--no-daemon"]

Error Logs

No such directory exists as the project root: /ecosystem.config.js
2025-02-17T10:28:53: PM2 log: App [nextjs:1] online
2025-02-17T10:28:53: PM2 error: Cancelling versioning data parsing
2025-02-17T10:28:53: PM2 log: App name:nextjs id:2 disconnected
2025-02-17T10:28:53: PM2 log: App [nextjs:2] exited with code [1] via signal [SIGINT]
2025-02-17T10:28:53: PM2 log: App [nextjs:2] starting in -cluster mode-

Steps to Reproduce

  1. Build and run the Docker container with the above configuration
  2. Enable cluster mode in ecosystem.config.js by uncommenting:
    instances: "max",
    exec_mode: "cluster",
    
  3. Application fails to start in cluster mode

Additional Information

  • The application runs successfully in fork mode
  • Manual pm2 start inside the container works in cluster mode
  • Issue only occurs with pm2-runtime in cluster mode
  • Using Next.js 14.2.3 with Node.js 19

Any assistance or guidance would be greatly appreciated.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ecosystem.config.js and the Dockerfile CMD using pm2-runtime, then reproduce the cluster-mode startup with the listed Next.js and Node.js versions. Trace how pm2-runtime resolves /ecosystem.config.js and compare it with the manual pm2 start behavior; done means the Docker container starts the Next.js app successfully in cluster mode.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, javascript, nextjs, nodejs
Domain
backend, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.