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?
Since there is no bidirectional link I solved this issue with the --net argument. That way they are using the same network stack and can therefore access each other over the loopback device (localhost).
So I can access from web the selenium server (port 4444) and my selenium server can access my web server (port 80).
I solved this by appending an ip-table into /etc/hosts of each container, for example
There is no bi-directional link since you can not link to a non-running container.
Unless you are disabling inter-container communication, 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:
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:
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/'
Use an orchestration service… there are so many to choose from, but I personally like the DNS-based ones like Skydock or registrator and access containers by dns name.
Use a docker management service (such as dockerize.it —disclaimer: I am working on it—) that will setup the DNS services for you.
Docker 1.10 addresses this very nicely by introducing advanced container networking. (Details: https://docs.docker.com/engine/userguide/networking/dockernetworks/ )
First, create a network. The example below creates a basic "bridge" network, which works on one host only. You can check out docker's more complete documentation to do this across hosts using an overlay network.
Docker networks in 1.10 now create a special DNS resolution inside of containers that can resolve the names in a special way. First, you can continue to use --link, but as you've pointed out your example doesn't work. What I recommend is using --net-alias= in your docker run commands:
Note that having --name container2 is setting the container name, which also creates a DNS entry and --net-alias=container2 just creates a DNS entry on the network, so in this particular example you could omit --net-alias but I left it there in case you wanted to rename your containers and still have a DNS alias that does not match your container name.
(Details here: https://docs.docker.com/engine/userguide/networking/configure-dns/ )
And here you go:
And from container1
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:
Then I start a dns container which has dnsmasq-base installed and starts the dnsmasq service:
then I get the IP address of this container:
And start the containers like so:
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.