Hillboy
Hillboy

Reputation: 715

Docker Node.js environment variables

I'm trying to use environment variables on docker only needed for on command. On Mac/Linux I can simple just run token=1234 node command.js and token is available as an environment variable. But when I do this with docker docker exec $CONTAINER nenv token=123 node command.js I get unknown command token=123

Upvotes: 2

Views: 6339

Answers (3)

BlackStork
BlackStork

Reputation: 8381

Even if you need to simply retrieve certain configurations from the command (at docker run time) you can do it simply by switching from node env (process.env) to argv usage.
Such casers are not uncommon (docker-compose), and could be done in very easy way.

npm install yargs --save

run code with docker run or docker exec:

docker exec $CONTAINER node command.j --token 123

then in code:

const argv = require('yargs').argv;
...
let boo = do.something(argv.token);

Upvotes: 0

Sylvain GIROD
Sylvain GIROD

Reputation: 856

EDIT caused by my stupidness !

You can't set new env var using docker on an existing docker. You have to do this when you build it (using Dockerfile or docker-compose), or when you run it (using docker run $CONTAINER -e "name=value" command).

Upvotes: 1

num8er
num8er

Reputation: 19372

I don't use node env, I recommend to do following:

create config folder

put this in config/index.js

var
    nconf = require('nconf'),
    path = require('path');

nconf.env().argv();

nconf.file('local', path.join(__dirname, 'config.local.json'));
nconf.file(path.join(__dirname, 'config.json'));

module.exports = nconf;

create files: config/config.json (template of config) and config/config.local.json (copy of template with real configuration)

for example:

{
  "app": {
    "useCluster": false,
    "http": {
      "enabled": true,
      "port": 8000,
      "host": "0.0.0.0"
    },
    "https": {
      "enabled": false,
      "port": 443,
      "host": "0.0.0.0",
      "certificate": {
        "key": "server.key",
        "cert": "server.crt"
      }
    },
    "env": "production",
    "profiler": false
  },
  "db": {
    "driver": "mysql",
    "host": "address here",
    "port": 3306,
    "user": "username here",
    "pass": "password here",
    "name": "database name here"
  },
}

use in beginning of Your app: var config = require('./config');

and use config object whenever You need:

var config = require('./config'),
    cluster = require('./components/cluster'),
    http = require('http'),
    ...
    ...
    https = require('https');

cluster.start(function() {
    if (config.get('app:http:enabled')) {
        var httpServer = http.createServer(app);
        httpServer.listen(config.get('app:http:port'), config.get('app:http:host'),
            function () {
                winston.info('App listening at http://%s:%s', config.get('app:http:host'), config.get('app:http:port'));
            });
    }

    if (config.get('app:https:enabled')) {
        var httpsServer = https.createServer({
            key: fs.readFileSync(path.join(__dirname, 'certificates', config.get('app:https:certificate:key'))),
            cert: fs.readFileSync(path.join(__dirname, 'certificates', config.get('app:https:certificate:cert')))
        }, app);
        httpsServer.listen(config.get('app:https:port'), config.get('app:https:host'),
            function () {
                winston.info('App listening at https://%s:%s', config.get('app:https:host'), config.get('app:https:port'));
            });
    }
});

this example is more accurate way to have environment based configs. for example: config.local.json configuration that will be added to .gitignore and so on...

Upvotes: 1

Related Questions