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: 
There is no bi-directional link since you can not link to a non-running container.

Unless you are [disabling inter-container communication](https://docs.docker.com/articles/networking/#between-containers), all containers on the same host can *see* any other containers on the network. All you need is to provide them the ip address of the container you want to contact.

The simplest way of knowing the ip address of a container is to run:

```
docker inspect --format '{{ .NetworkSettings.IPAddress }}' container1

```

You can look it up after starting both containers (just don't use `--link`).

If you need to know the IP of *container2* from inside *container1* automatically, there are a few options:

1. Mount the docker socket as a volume and use the remote API

docker run -i -t --name container1 -v /var/run/docker.sock:docker.sock ubuntu:trusty /bin/bash
echo -e "GET /containers/container2/json HTTP/1.0\r\n" | nc -U /docker.sock | sed 's/.*IPAddress":"([0-9.]*).\*/\1/'
2. Use an orchestration service… there are so many to choose from, but I personally like the DNS-based ones like [Skydock](https://github.com/crosbymichael/skydock) or [registrator](https://github.com/progrium/registrator) and access containers by dns name.
3. Use a docker management service (such as dockerize.it —disclaimer: I am working on it—) that will setup the DNS services for you.
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.