0

Below is a very simple process.json to launch a js app with pm2.

My goal is to have the process kick off the below node_args but only for non-prod launches. So, for example, if a certain environment variable is set a certain way, then trigger the node_args, otherwise ignore them.

Is this possible? If not, any workarounds I can do?

{
  apps : [
    {
      name      : 'API',
      script    : './app.js',
      node_args: ["--inspect","--debug=0.0.0.0:7000"],
      env: {
        NODE_ENV: 'development'
      }
    }
  ]
}

I know that it's possible to throw node_args at the command line, however the goal is to run this in docker where the "run" command is static to the source image so that's not really viable.

Here is the CMD entry in my Dockerfile:

CMD [ "pm2-docker", "--auto-exit", "--watch", "process.json"]

emmdee
  • 1,541
  • 3
  • 25
  • 46

1 Answers1

3

You can define multiple declaration of same app, depending on its environment settings.

In your case, your process file definition can be as follows:

process.json

{
  apps : [
    {
      name      : 'API-DEV',
      script    : './app.js',
      node_args: ["--inspect","--debug=0.0.0.0:7000"],
      env: {
        NODE_ENV: 'development'
      }
    },
    {
      name      : 'API-PROD',
      script    : './app.js',
      env: {
        NODE_ENV: 'production'
      }
    }
  ]
}

Then, if you want to run the development version of the app,CMD entry of Dockerfile will be

CMD [ "pm2-docker", "start", "process.json", "--only", "API-DEV", "--auto-exit", "--watch" ] 

If you don't like the duplicacy, you can use .config.js file instead of json and define your configuration like this

process.config.js

const appDefs = [
  {
    "suffix": "prod",
    "node_args": [],
    "env": {
      "NODE_ENV": "production"
    }
  },
  {
    "suffix": "dev",
    "node_args": ["--inspect","--debug=0.0.0.0:7000"],
    "env": {
      "NODE_ENV": "development"
    }
  }
];

module.exports = {
  "apps": appDefs.map(appDef => ({
    "name": `API-${appDef.suffix}`,
    "script": "./app.js",
    "node_args": appDef.node_args,
    "env": appDef.env
  }))
};

Then, CMD entry will be

CMD [ "pm2-docker", "start", "process.config.js", "--only", "API-DEV", "--auto-exit", "--watch" ] 
SALEH
  • 1,552
  • 1
  • 13
  • 22
  • Thanks. So it seems like I'll need separate images regardless due to the differing `CMD` entry? It would be nice to have the same source image, then when launching the container the environment can be set - is that possible? – emmdee Dec 04 '17 at 17:44
  • I am no expert in docker, but what i have understood is that, you want to pass different values to your `CMD` entry while executing `docker run` command. In that case, i think [this](https://stackoverflow.com/questions/40873165/use-docker-run-command-to-pass-arguments-to-cmd-in-dockerfile) will solve the problem – SALEH Dec 05 '17 at 05:35