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.

  • Record Sound on Ubuntu Docker Image
  • How to reach the service running in docker container(overlay) externally from different hosts
  • Benchmarking contianers in clouds
  • what is “COPY . /src”for when building a image
  • Python script to run docker
  • Error while running docker container
  • 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).

  • Docker & Amazon Beanstalk - Deploy an Angular application
  • How to Set Capabilities on Node Browser with selenium Docker
  • Docker creates files as root in mounted volume [duplicate]
  • Launch Docker Container using ansible-playbook from a locally available docker image
  • Docker Registry v2 Images cannot be removed
  • Can I use Docker like this …?
  • 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.