Carlos Torrecillas
Carlos Torrecillas

Reputation: 5756

Run PM2 Docker with specific environment

I have been trying to pass the environment to my PM2 process so that I can target development or production.

My Dockerfile is the following:

FROM keymetrics/pm2:latest
COPY dist dist/
COPY pm2.json .
CMD ["pm2-runtime", "start", "pm2.json"]

I have the following pm2.json:

{
  "name": "my-app",
  "script": "dist/server.js",
  "instances": "1",
  "env": {
    "NODE_ENV": "development"
  },
  "env_production" : {
    "NODE_ENV": "production"
  }
}

And I couldn't manage to get the environment set properly so when I access process.env.NODE_ENV I always get "none" back.

I have tried changing the CMD to:

CMD ["pm2-runtime", "start", "pm2.json", "--env", "production"] and no joy.

I have also tried to get the environment variable set in the dockerfile like this:

ENV NODE_ENV=production and that's not picked up either.

I have also checked the latest PM2 documentation and swapped the pm2.json with a ecosystem.config.js with pretty much the same structure but that hasn't work either.

What am I missing? I'm sure that has to be something really easy to fix but can't get it to work.

THanks

Upvotes: 1

Views: 2190

Answers (1)

Aidin
Aidin

Reputation: 30135

CMD [ "pm2-runtime", "start", "ecosystem.config.js", "--env=production"] is working for me.

Upvotes: 1

Related Questions