Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vgulerianb opened this issue Feb 17, 2025 · 0 comments
Open

Comments

@vgulerianb
Copy link

vgulerianb commented Feb 17, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant