Question: I have to link two containers so they can see each other. Of course the following...

```
docker run -i -t --name container1 --link container2:container2 ubuntu:trusty /bin/bash
docker run -i -t --name container2 --link container1:container1 ubuntu:trusty /bin/bash

```

...fails at line 1 because a container needs to be up and running in order to be a link target:

```
2014/08/15 03:20:27 Error response from daemon: Could not find entity for container2

```

What is the simplest way to create a bidirectional link?

Answer: 
Since there is no bidirectional link I solved this issue with the [--net](https://docs.docker.com/reference/commandline/cli/#run) argument. That way they are using the same network stack and can therefore access each other over the loopback device (localhost).

```
docker run -d --name web me/myserver
docker run -d --name selenium --net container:web me/myotherserver

```

So I can access from web the selenium server (port 4444) and my selenium server can access my web server (port 80).
Here's how I've solved this for myself:

First, I go through all my containers (which need to know from each other) and create dnsmasq entries like so:

```
for f in container1 container2 container3; do
  IP=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' $f 2>/dev/null`
  if [ -n "$IP" ]; then
    echo $f "$IP"
    echo "host-record=$f,$IP" > /etc/dnsmasq.d/0host_$f
  else
    rm -f /etc/dnsmasq.d/0host_$f
  fi
done

```

Then I start a dns container which has dnsmasq-base installed and starts the dnsmasq service:

```
docker run -d -P -h dns --name dns -v /etc/dnsmasq.d:/etc/dnsmasq.d:ro dns

```

then I get the IP address of this container:

```
DNS_IP=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' dns`

```

And start the containers like so:

```
docker run -d -P -h container1 --name container1 --dns $DNS_IP container1
docker run -d -P -h container2 --name container2 --dns $DNS_IP container2
docker run -d -P -h container3 --name container3 --dns $DNS_IP container3

```

This is a simplified version of my setup but it shows the gist. I've also added a mechanism that forces dnsmasq to rescan when files in /etc/dnsmasq.d change via inotify-tools. That way all containers get the new ip address whenever one container is restarted.