Docker Compose – Command using Container Environment Variable

Using Docker Compose to link a master and slave service together. The slave container is thus automatically injected by Compose with environment variables containing the various ports and IPs needed to connect to the other master container.

The service accepts the IP/Port of the master via a command line argument, so I set this in my commands.

  • Docker rails app and git
  • Using Ansible docker_container, how can I display standard out? (stdout)
  • What does BandwidthIn and BandwidthOut graph represent for a service?
  • Additional steps in Dockerfile
  • How can I access my docker maria db?
  • Docker volume - need permissions to write to database
  • master:
      command: myservice
      ports:
        - '29015'
    slave:
      command: myservice --master ${MASTER_PORT_29015_TCP_ADDR}:${MASTER_PORT_29015_TCP_PORT}
      links:
        - master:master
    

    The problem is that the environment variables like MASTER_PORT_29015_TCP_PORT are evaluated when the compose command is run, and not from within the container itself where they are actually set.

    When starting the cluster – you see the warning: WARNING: The MASTER_PORT_29015_TCP_ADDR variable is not set. Defaulting to a blank string.

    I tried setting entrypoint: ["/bin/sh", "-c"] but produced unusual behaviour where the service wouldn’t see any variables at all. (For information, the service I’m actually using is RethinkDB).

  • Create an nginx reverse proxy container to forward to specific URL/Port based on requested URL
  • Dynamic DNS Resolution with HAProxy and Docker
  • Share dynamic content across docker containers nodes in Jelastic
  • What is the exact difference between SizeRootFs and SizeRw in Docker containers?
  • docker build taking too much time in aws ecs
  • Execute rake tasks from a container to an other
  • One Solution collect form web for “Docker Compose – Command using Container Environment Variable”

    As stated in the documentation, link environment variables are now discouraged, and you should just write master instead of $MASTER_PORT_29015_TCP_ADDR. Moreover, there doesn’t seem to be any point to writing $MASTER_PORT_29015_TCP_PORT when you know its value’s going to be 29015.

    Hence, change the command to:

    myservice --master master:29015
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.