Now docker has production ready multi-host networking capabilities and it has commands to manage networks from the command line. This guide walks you through the basic docker multi-host networking using docker machine and consul service discovery tool.[alert style=”e.g. white, grey, red, yellow, green”] There is only one prerequisite for this tutorial. You just need the latest docker toolbox installed on your system.[/alert]
Docker Multi-Host Networking
In this setup, we will create three docker hosts on virtual box using docker machine. One host runs consul and other two hosts share the network information using consul service discovery container on the first host. You can learn more about consul from here.[alert style=”e.g. white, grey, red, yellow, green”] To know more about networking basics, refer this official docker documentation[/alert]
Follow the steps given below to set up multi-host networking.
1. Create a docker machine named “host1-consul”
docker-machine create -d virtualbox host1-consul
2. Launch a consul container on the host1-consul host using the following docker run command.
docker $(docker-machine config host1-consul) run -d -p "8500:8500" -h "consul" progrium/consul -server -bootstrap
3. You can verify the running container status using the following command.
docker $(docker-machine config host1-consul) ps
4. Now, launch the second docker machine host with parameters to register it with consul running on the host1-consul host.
docker-machine create -d virtualbox --engine-opt="cluster-store=consul://$(docker-machine ip host1-consul):8500" --engine-opt="cluster-advertise=eth1:0" host2
5. Launch the third docker machine.
docker-machine create -d virtualbox --engine-opt="cluster-store=consul://$(docker-machine ip host1-consul):8500" --engine-opt="cluster-advertise=eth1:0" host3
Now the two hosts have the default networks which can be used only for single host communication.
6. To have a multi-host network we need to create an overlay network on host2 using the “docker network” command as shown below.
docker $(docker-machine config host2) network create -d overlay myapp
7. Now, if you check the networks on host3, you will be able to see the overlay network we created on host2. It is because our two hosts are registered with consul and the network information is shared among all the hosts which are registered with it.
docker $(docker-machine config host2) network ls docker $(docker-machine config host3) network ls
Now, if you launch containers in the differnt host, you will be able to connect them using the container name. Let test it by launching a Nginx container on host2 and test the connection by downloading the default Nginx page from host3 using a busybox container.
8. Launch a Nginx container on host2 by specifying the network “myapp” we have created.
docker $(docker-machine config host2) run -itd --name=webfront --net=myapp nginx
9. Verify the running container.
docker $(docker-machine config host2) ps
10. Now, launch a busybox container on host3 with parameters to download the homepage of nginx running on host2.
docker $(docker-machine config host3) run -it --rm --net=myapp busybox wget -qO- http://webfront
If the above command returns an HTML output, it means the containers are able to connect to hosts using the overlay network you have created.