Create environmental variable in Docker container at startup

How do I get the ip address of the mongo container and set it as an environment variable when building a node image?

I got a problem with conflicting tech packages: keystone.js, forever and docker. My problem is that I need to set up an environment variable for a separate mongo container, which would be easy to do by running a shell script when starting the container, which includes:

export MONGO_URI="mongodb://${MONGODB_PORT_27017_TCP_ADDR}:27017/(db_name)"

      

The problem is related to starting the tracing application. Normally I would put it in the same script and call it docker run, but we need to use this project forever. The team will be forever keystone.js

. The problem with this is that the docker container crashes immediately. If I run the app with a plain forever start

one and not a script, the app starts fine, but the required env variable is not set. It is hardcoded in the docker image, but of course this is not a very good solution as the ip of mongodb may change in the future and then when you reload the node container it will not be able to find the db. See Several possibilities:

  • Switch to using node keystone.js, lose eternal run functionality (which will restart your app if a crash occurs). Tested and it works, but maybe someone knows a way to make it work forever, or a viable alternative?

  • Find a way to set the above export from docker file when generating the image. Couldn't get this to work, but I know the name that mongdb will use no matter what if it helps

Any help is most appreciated.

+3


source to share


1 answer


The best way is to use the docker link , this gives you the hostname + your environment variables.

docker run ... --link mongodb:mongodb ..

      

Also you can use command line option from run



docker run -e MONGO_URI="mongodb://${MONGODB_PORT_27017_TCP_ADDR}:27017/(db_name)"

      

The option for dynamic dns will be SkyDNS + SkyDock .

+3


source







All Articles