4

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.

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).

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Spot on @jwodder. Solved the master/slave linkage, but would still be great to solve the underlying usage of the environment variable, for instance, the slaves can be given names, an argument to set one to use the containers hostname for example. --name ${hostname} still doesn't work for example so the slave just generates random ID names making it impossible to tally back up to its container.
@simbolo have you tried using the hostname in backticks - the contents get evaluated at runtime, e.g. --name `hostname`
The answer in stackoverflow.com/a/40472001 suggests to escape the dollar sign with an extra dollar sign. Though it didn't work for me (Docker version 19.03.6).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.