Develop Ingelligence - Docker Docker Networking
Develop Ingelligence - Docker Docker Networking
docker network ls will list all networks associated with Docker on the host.
docker network inspect bridge to see more details on the network associated with Docker.
Note: "Containers": {} => No container is running.
Run a Container:
docker run -it ubuntu:latest /bin/bash
Note: Now if we inspect our network name via the inspect command, you will now see that the container is
attached to the bridge.
To Install Ping:
apt-get update
apt-get install iputils-ping -y
Overlay networks
Overlay networking is a method of using software to create network abstraction that can be used to run
multiple separate virtualized network layers on top of physical network.
They are best when you need containers running on different Docker hosts/deamons to communicate, or
when multiple applications work together using swarm services.
Data when transferred over the overlay network is secured.
Works on any cloud / on-prem environments with little or no reconfiguration of existing physical network.
Develop Ingelligence – Docker Docker Networking
Walkthrough
docker swarm init
copy the swarm command and run on another node
o docker swarm <join-token> worker
docker node ls
docker network create --driver overlay my-overlay-network
docker service create --name my-svc --network my-overlay-network --replicas 2 alpine sleep 1d
docker service ps my-svc
docker inspect my-overlay-network # We can only see containers on this host.
docker exec -it a5 /bin/sh
apt-get update
apt-get install iputils-ping -y
ping 10.0.1.3
Most public cloud providers including AWS or Azure cloud providers doesn’t support MACVLAN as it requires
PROMISCUOUS MODE for the network card.
In terms of networking, a bridge network is a Link Layer device which forwards traffic between network
segments. A bridge can be a hardware device or a software device running within a host machine’s kernel.
In terms of Docker, a bridge network uses a software bridge which allows containers connected to the same
bridge network to communicate, while providing isolation from containers which are not connected to that
bridge network. The Docker bridge driver automatically installs rules in the host machine so that containers on
different bridge networks cannot communicate directly with each other.
When you start Docker, a default bridge network (also called bridge) is created automatically, and newly-
started containers connect to it unless otherwise specified. You can also create user-defined custom bridge
networks.
DNS Resolution: Containers on the default bridge network can only access each other by IP addresses. On a
user-defined bridge network, containers can resolve each other by name or alias.
Better Isolation: Using a user-defined network provides a scoped network in which only containers attached
to that network are able to communicate.
Attach and Detach: Containers can be attached and detached from user-defined networks on the fly.
o docker network connect my-network my-container
Develop Ingelligence – Docker Docker Networking
Walkthrough to show how to use the default bridge network, that Docker sets up for you automatically.
In this example, you start two different containers on the same Docker host and do some tests to understand how
they communicate with each other.
1. List current networks
docker network ls
2. Start two containers c1 and c2.
docker run -dit --name c1 ubuntu
docker run -dit --name c2 ubuntu
3. Inspect the bridge network to see what containers are connected to it.
docker network inspect bridge
Note that the information about the bridge network is listed, including the IP address of the gateway between
the Docker host and the bridge network (172.17.0.1). Under the Containers key, each connected container is
listed, along with information about its IP address (172.17.0.2 for c1 and 172.17.0.3 for c3).
4. Find the IP address of container c2
docker inspect c2
5. The containers are running in the background. Use the docker attach command to connect to
docker attach c1
6. From within container c1, note that you will be able to ping google.com (internet)
apt-get update
apt-get install iputils-ping -y
ping -c 3 google.com
7. From within container c1, note that you will be able to ping container c2 by its IP address by not by its name
ping <IP of C2> => Success
ping c2 => Fails
8. Detach from c1 without stopping it by using the detach sequence Ctrl + P + Q
9. Stop and remove both the containers
docker container stop c1 c2
docker container rm c1 c2
To configure the Docker daemon using a JSON file, create a file at /etc/docker/daemon.json on Linux
systems, or C:\ProgramData\docker\config\daemon.json on Windows.
On MacOS go to the whale in the taskbar > Preferences > Daemon > Advanced.
Develop Ingelligence – Docker Docker Networking