简体   繁体   中英

Node.js Docker image environment variables

I have a Node.js application which is built into the docker image. In this application I have a config file with some API urls (API key, for example) which might change from time to time. Is it possible to launch the docker image with some additional parameter and then access this param from the node.js code (I assume this could be done through using environment vars) so as not to rebuild the image every time the value of this param should be changed. This is the pseudo code which I assume can be used:

docker run -p 8080:8080 paramApiKey="12345" mydockerimage

and then I'd like to access it from the node.js app:

var apiKey = process.env.paramApiKey

Can this somehow be achieved?

In order to define environment variables with docker at the time you use the run command, you have to use the -e flag and the format is should be "name=value" , meaning that your ENV variable should be "paramApiKey=12345" so that you can access it by doing process.env.paramApiKey in your application.

That being said, your command would look like:

docker run -p 8080:8080 -e "paramApiKey=12345" mydockerimage

当然,请尝试:

docker run -p 8080:8080 -e "paramApiKey=12345" mydockerimage 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM