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
-
possible duplicate of stackoverflow.com/questions/27812548/…hankchiutw– hankchiutw2016-04-05 13:27:32 +00:00Commented Apr 5, 2016 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...
2 Comments
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);