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.

  • docker build : Fatal error: Class 'Memcached' not found
  • Docker port mapping not working, “connection refused”
  • return from docker-compose up in jenkins
  • docker container throws error: php_network_getaddresses: getaddrinfo failed: Name or service not known
  • How does one use Apache in a Docker Container and write nothing to disk (all logs to STDIO / STDERR)?
  • Swarm mode rolling updates progress
  • 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?

  • unable to run web application on docker
  • Is there a way how to run LXD inside Docker?
  • docker openerp can not start - stop
  • Starting bash script on Docker container startup doesn't work
  • Slow codeception coverage in docker
  • How does service discovery work with modern docker/docker-compose?
  • 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_ADDR and 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. --env RABBITMQ_PORT_5672_TCP_ADDR=1.2.3.4).

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