Skip to content

Instantly share code, notes, and snippets.

@garthk
Last active January 4, 2016 02:19
Show Gist options
  • Save garthk/8553920 to your computer and use it in GitHub Desktop.
Save garthk/8553920 to your computer and use it in GitHub Desktop.
Do docker's environment variables for container linking make as much sense as we'd like?
#!/bin/bash
# show VICTIM_A_PORT_80_TCP_PORT=80 because repetition
docker build -t=polite-listener .
docker run -d -p 80 -name fake80a polite-listener
docker run -d -p 80 -name fake80b polite-listener
docker run -rm -i -link fake80a:VICTIM_A -link fake80b:VICTIM_B stackbrew/ubuntu env
docker stop fake80a fake80b
docker rm fake80a fake80b
#!/bin/bash
# show VICTIM_PORT=tcp://172.17.0.35:8000 because clobbered
docker build -t=polite-listener .
docker run -d -p 8000 -p 8001 -name victim multi-listen 8000 8001
docker run -rm -i -link victim:VICTIM stackbrew/ubuntu env
docker stop victim
docker rm victim
# Politely listens on the nominated TCP ports.
FROM stackbrew/ubuntu
ADD multi-listen /usr/local/bin/multi-listen
RUN chmod a+x /usr/local/bin/multi-listen
ENTRYPOINT ["/usr/local/bin/multi-listen"]
CMD ["80"]
#!/bin/bash
# Politely listens to ports.
# Unlike 'nc', supports >1 port and will obey 'docker stop'.
PIDS=
for PORT in $*; do
nc -l $PORT & PIDS=$| $PIDS
done
trap 'kill 0' SIGINT SIGTERM EXIT
wait $PIDS
@garthk
Copy link
Author

garthk commented Jan 22, 2014

From a debate about the environment variables for container linking involving @SvenDowideit, @cmelbye, and @tianon:

Under docker 0.7.6, the variables become:

VICTIM_A_PORT=tcp://172.17.0.2:80
VICTIM_A_PORT_80_TCP=tcp://172.17.0.2:80
VICTIM_A_PORT_80_TCP_ADDR=172.17.0.2
VICTIM_A_PORT_80_TCP_PORT=80
VICTIM_A_PORT_80_TCP_PROTO=tcp
VICTIM_A_NAME=/clever_ptolemy/VICTIM_A
VICTIM_B_PORT=tcp://172.17.0.3:80
VICTIM_B_PORT_80_TCP=tcp://172.17.0.3:80
VICTIM_B_PORT_80_TCP_ADDR=172.17.0.3
VICTIM_B_PORT_80_TCP_PORT=80
VICTIM_B_PORT_80_TCP_PROTO=tcp
VICTIM_B_NAME=/clever_ptolemy/VICTIM_B

The extra 80_ element in all but the top-level environment variable (linkname_PORT) is redundant, as it has no information to expose.

@garthk
Copy link
Author

garthk commented Jan 22, 2014

Updated to show ${ALIAS}_PORT clobbering when linking to a victim with multiple ports. That makes the variable unsafe: you could test with ${ALIAS}_PORT, be happy, and then get broken later when they expose another port.

@garthk
Copy link
Author

garthk commented Jan 22, 2014

Michael Crosby confirms that the port you see in your container isn't guaranteed to match the port the original container exposed. It is now, because source code, but they might change that in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment