3

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:

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.

  • 1
    did you consider setting the environmental variable when starting the container, e.g. `docker run -E MUNGO_URI=....` or linking the mongo container `docker run -l mongo_container:mongo` ? – Mykola Gurov Dec 09 '14 at 20:28
  • I do link it. That is where the ${MONGODB_PORT_27017_TCP_ADDR} is coming from. Keystone.js uses another, MONGO_URI, so basically want to make MONGO_URI = MONGODB_PORT_27017_TCP_ADDR. Which as above is possible but not using forever. Ive been launching it using "node keystone.js" and the variable is set fine. – Ian Stansbury Dec 09 '14 at 22:20
  • Could you mark this question as answered/closed? Thanks :) – joh.scheuer Dec 10 '14 at 15:41

1 Answers1

3

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.

joh.scheuer
  • 566
  • 3
  • 11