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

- 472
- 6
- 19
-
possible duplicate of http://stackoverflow.com/questions/27812548/how-do-you-set-an-environment-variable-in-a-running-docker-container – hankchiutw Apr 05 '16 at 13:27
3 Answers
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...

- 18,604
- 3
- 43
- 57
-
This approach seems like unnecssary work in our use case. We clone our repo into our fresh docker image. So with your approach we would have to create a config file from docker cmd line then run the script – Hillboy Apr 05 '16 at 13:23
-
1no, just create file config.local.json in docker app, and it will keep configuration, also You can try to modify config/index.js to read configuration from arguments, using node-argument-parser it's good package. – num8er Apr 05 '16 at 13:28
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
).

- 836
- 1
- 11
- 23
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);

- 7,603
- 1
- 16
- 18