2

I've been wondering if the linked container shuts down and starts again, does the container that is linked with it restores the --link connection?

Fire 2 containers.

docker run -d --name fluentd millisami/fluentd
docker run -d --name railsapp --link fluentd:fluentd millisami/rails

Now if the fluentd container is stopped and restarted, will the railsapp container restores the link and linked ENV vars automatically?

millisami
  • 9,931
  • 15
  • 70
  • 112
  • It seems like it should, but be careful about the networking. If you are dynamically setting environment variables based on the container's IPAddress or Ports (if they are generated by docker) using `docker run -e` then those won't update. –  Sep 29 '14 at 06:39
  • possible duplicate of [How to setup linkage between docker containers so that restarting won't break it?](http://stackoverflow.com/questions/24252598/how-to-setup-linkage-between-docker-containers-so-that-restarting-wont-break-it) – Ben Whaley Sep 30 '14 at 04:57

1 Answers1

2

UPDATE:

As of Docker 1.3 (or probably even from earlier version, not sure), the /etc/hosts file will be updated with the new ip of a linked containers if it restarts. This means that if you access it via its name within the /etc/hosts entry as opposed to the environment variable, your link will still work even after restart.

Example:

When you starts two containers app_image and mysql and link them like this:

$ docker run --name mysql mysql:5.6.20
$ docker run -d --link mysql:mysql app_image

you'll get an entry in your /etc/hosts within app_image:

# /etc/hosts example
mysql 172.17.0.12

and that ip will be refreshed in case mysql crashes and is restarted.

So don't use environment variables when referring to your linked container:

$ ping $MYSQL_PORT_3306_TCP_ADDR # --> don't use! won't work after the linked container restarts

$ ping mysql # --> instead, access it by the name as in /etc/hosts

Old answer:

Nope,it won't. In the face of crashing containers scenario, links are as good as dead. I think it is pretty much an open problem,i.e., there are many candidate solutions, but none are yet to be crowned the standard approach.

You might want to take a look at http://jasonwilder.com/blog/2014/07/15/docker-service-discovery/

lolski
  • 16,231
  • 7
  • 34
  • 49
  • you can just do `ping mysql`?! wow. Didn't realize /etc/hosts was that magical! Very nice answer; not sure why the official docker documentation continues to blab on mostly about ENV vars when `/etc/hosts` is both easier to use and is stable over restarts.... Saved me much headache you have! – cboettig Feb 16 '15 at 21:36
  • 1
    You might want to be wary still, I think it's a bit glitchy with Docker sometimes (like 10% of the time) fails to update the linking. So to overcome that, I ended up using Consul service discovery (consul.io) which is more robust and can scale to multiple machines / clusters – lolski Feb 17 '15 at 02:30
  • Okay, good to know. So far seems stable to me, and is super simple and easy. Is there a known bug/issue to watch on Docker regarding said glitch? – cboettig Feb 17 '15 at 21:40