Creating an environmental variable within Docker container when starting up

How would I get the ip address of a mongo container and set it as environmental variable when creating a node image?

I’ve been running into an issue with conflicting tech stacks: keystone.js, forever, and docker. My problem is that I need to set up an environmental variable for a separate mongo container which would seem easy to do by running a shell script when I start up the container that includes:

  • Keep container alive and linked using docker-compose
  • Docker Backup and restore postgres
  • Could not run asp.net mvc 6 on Docker
  • Docker Mysql link to another container
  • Why am I getting an RTNETLINK Operation Not Permitted when using Pipework with Docker containers?
  • Need for service discovery for docker engine swarm mode
  • export MONGO_URI="mongodb://${MONGODB_PORT_27017_TCP_ADDR}:27017/(db_name)"
    

    The issue comes with starting the keystone app. Normally I would place it in the same script and call it with docker run but this project we need to use forever. Command would be forever keystone.js. There is an issue with this in that the docker container drops immediatly. If I start the app with a simple forever start rather than going to the script the app starts up fine but the env variable needed is not set. It’s hard coded in the docker image but of course this is not a good solution as the ip of the mongodb may change in the future and then on the node container restart it would not be able to find the db. See a couple of possibilities:

    1. Switch to just using a node keystone.js, would loose the functionality of the forever start (which will restart the app if there is a critical failure). Tested and this works but maybe someone knows a way to make forever work or a viable alternate?

    2. Find a way to set the above export from the docker file when creating the image. Haven’t been able to get this to work but I do know the name that the mongdb is going to use no matter what if that helps

    Any help is most appreciated.

  • Dynamically set JAVA_HOME of docker container
  • How to change permissions of a docker mounted volume on OSX to work with Nginx web server running Laravel 5.1?
  • How to share localhost between two different Docker containers?
  • returned a non-zero code: 2 on EVERY SINGLE COMMAND in known good Dockerfiles
  • Docker on Windows and Samba/CIFS
  • How to access a docker container running on MacOSX from another host?
  • One Solution collect form web for “Creating an environmental variable within Docker container when starting up”

    The best way is to use docker link this provides you a hostname + your environmental variables.

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

    Also you can use the command line option from run

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

    An Option for dynamic dns would be SkyDNS + SkyDock.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.