Docker network
Trying out container networking
Using `docker network inspect bridge` after running a container, we will see the container name and IP under the ”Containers” section.
That’s because the default network for containers is the bridge network. It is used for communication between multiple containers running on the same docker host. To communicate between different hosts, we can use the overlay network.
Now trying this out.
Bridge
I have 2 nginx containers running:
Connect them to a new bridge network (instead of the default one):
docker network create -d bridge nginx-net
docker network connect nginx-net webserver1
docker network connect nginx-net webserver2And now we can use curl or send a ping between them (note: the name is resolved by docker):
docker exec webserver1 sh -c "curl webserver2"We can see the GET request in the logs using docker logs webserver2.
Overlay
Get Mika Tal’s stories in your inbox
Join Medium for free to get updates from this writer.
Now we can set up a network between containers on different hosts. I started two EC2 instances, on which I’ve opened the following ports in the security group (see here):
docker swarm init initializes a swarm, and sets the current node as manager.
sudo docker swarm init
sudo docker network create -d overlay --attachable ec2-netThe second host can join the swarm (copy the docker swarm join –token … from the swarm init output).
Next run the container and connect it to the new network (sudo docker network connect ec2-net webserver1) on the manager. On the second host, specify --network ec2-net along with the run command. After this, we can check to see the networks using docker network ls.
Now it’s time to test this. The name of the container wasn’t resolved, so I tried the IP of the second host (from inspect).
If we create a service, the name is resolved by docker:
Now we can scale this service to 2 replicas:
But the IP of the nodes above is private, by taking the public address of both or just one of the nodes from Amazon we can see the website. Nice.








