2

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?

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

Have you an idea for handling a long list of environment variables ?
@Sachacr: Put them to a file and use --env-file, see docs.docker.com/engine/reference/commandline/run/…
what if some variables are stuff like passwords and JWT Secret hashes?
and how would you tell cloud.docker.com to use them? for example: cloud.docker.com/app/gkatsanos/repository/docker/gkatsanos/…
1

Sure, just try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.