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.

  • debugging tornado server in docker container
  • How to circumvent “Read-only file system” error on Docker version 0.11 or greater when running sysctl -w during an image build?
  • docker - libudev: udev_has_devtmpfs: name_to_handle_at on /dev: Operation not permitted
  • Issue with docker compose
  • Use separate docker container for code only
  • How to customize the configuration file of the official PostgreSQL Docker image?
  • 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 port expose
  • Docker is filling up my disk space
  • Disable access to LAN from docker container
  • Importing self-signed cert into Docker's JRE cacert is not recognized by the service
  • Docker error from CMD command prompt: Something went wrong running an SSH Command
  • “kubectl get services” doesn't show an “ExternalIP” column
  • 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.