Using environment variables post docker container linking in java
I am trying to simulate docker container linking using a simple use case which is as follows
1) A docker container with a simple pub-sub java application, there is a publisher and subscriber both within the same container. I have used dockerfiles for building this
2) A docker container running rabbitmq, this was pulled from docker hub.
Now I link both the containers, I am able to see rabbitmq environment variables in my container #1.
Now my question is what is the best way to utilize these container variables in my pub-sub container #1. I can always java System.getenv and hardcode a environment variable. Are there any better ways of doing it?
One Solution collect form web for “Using environment variables post docker container linking in java”
Hard-coding an environment variable seems OK here. The environment variables follow a standard format, like
RABBITMQ_PORT_5672_TCP_PORT. The only bit of those names which would change is the label
RABBITMQ, which is set based on the options to
docker run. Whoever runs your container controls that bit, either with
--link rabbitmq or
--link someothercontainer:rabbitmq to set an alias. This just forms part of your container’s “contract” with the outside world: the container must be run in a way that adds variables with the right alias.
Incidentally, this doesn’t force you to use Docker links if you don’t want to, as you can always just pass in the environment variables if Rabbit MQ were on a different machine (e.g.