Lab4 Docker Networking
Lab4 Docker Networking
In this lab you will learn about key Docker Networking concepts. You will get your hands dirty by going through examples of a few basic
networking concepts, learn about Bridge networking, and finally Overlay networking.
Tasks:
docker network
Manage networks
Options:
--help Print usage
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
docker network ls
NETWORK ID NAME DRIVER SCOPE
3430ad6f20bf bridge bridge local
1
a7449465c379 host host local
06c349b9cc77 none null local
The output above shows the container networks that are created as part of a standard installation of Docker.
New networks that you create will also show up in the output of the docker network ls command.
You can see that each network gets a unique ID and NAME. Each network is also associated with a single driver. Notice that the “bridge” network
and the “host” network have the same name as their respective drivers.
2
]
NOTE: The syntax of the docker network inspect command is docker network inspect <network>,
where <network> can be either network name or network ID. In the example above we are showing the
configuration details for the network called “bridge”. Do not confuse this with the “bridge” driver.
docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.03.1-ee-3
Storage Driver: aufs
<Snip>
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
<Snip>
The output above shows the bridge, host,macvlan, null, and overlay drivers.
docker network ls
NETWORK ID NAME DRIVER SCOPE
3430ad6f20bf bridge bridge local
a7449465c379 host host local
06c349b9cc77 none null local
The output above shows that the bridge network is associated with the bridge driver. It’s important to note that the network and the driver are
connected, but they are not the same. In this example the network and the driver have the same name - but they are not the same thing!
The output above also shows that the bridge network is scoped locally. This means that the network only exists on this Docker host. This is true of
all networks using the bridge driver - the bridge driver provides single-host networking.
All networks created with the bridge driver are based on a Linux bridge (a.k.a. a virtual switch).
Install the brctl command and use it to list the Linux bridges on your Docker host. You can do this by running sudo apt-get install bridge-
utils.
apk update
apk add bridge
Then, list the bridges on your Docker host, by running brctl show.
3
brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.024252ed52f7 no
The output above shows a single Linux bridge called docker0. This is the bridge that was automatically created for the bridge network. You can
see that it has no interfaces currently connected to it.
You can also use the ip a command to view details of the docker0 bridge.
ip a
<Snip>
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
group default
link/ether 02:42:52:ed:52:f7 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
846af8479944 ubuntu "sleep infinity" 55 seconds ago Up
54 seconds heuristic_boyd
As no network was specified on the docker run command, the container will be added to the bridgenetwork.
Run the brctl show command again.
brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.024252ed52f7 no vethd630437
Notice how the docker0 bridge now has an interface connected. This interface connects the docker0bridge to the new container just created.
You can inspect the bridge network again, by running docker network inspect bridge, to see the new container attached to it.
4
docker network inspect bridge
<Snip>
"Containers": {
"846af8479944d406843c90a39cba68373c619d1feaa932719260a5f5afddbf71": {
"Name": "heuristic_boyd",
"EndpointID":
"1265c418f0b812004d80336bafdc4437eda976f166c11dbcc97d365b2bfa91e5",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
<Snip>
docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
846af8479944 ubuntu "sleep infinity" 7 minutes ago Up
7 minutes heuristic_boyd
Next, lets run a shell inside that ubuntu container, by running docker exec -it <CONTAINER ID> /bin/bash.
5
apt-get update && apt-get install -y iputils-ping
Lets ping www.github.com by running ping -c5 www.github.com
exit
We should also stop this container so we clean things up from this test, by running docker stop <CONTAINER ID>.
NOTE: If you start a new container from the official NGINX image without specifying a command to run, the
container will run a basic web server on port 80.
Start a new container based off the official NGINX image by running docker run --name web1 -d -p 8080:80 nginx.
docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
4e0da45b0f16 nginx "nginx -g 'daemon ..." 2 minutes ago
Up 2 minutes 443/tcp, 0.0.0.0:8080->80/tcp web1
6
The top line shows the new web1 container running NGINX. Take note of the command the container is running as well as the port mapping -
0.0.0.0:8080->80/tcp maps port 8080 on all host interfaces to port 80 inside the web1 container. This port mapping is what effectively makes
the containers web service accessible from external sources (via the Docker hosts IP address on port 8080).
Now that the container is running and mapped to a port on a host interface you can test connectivity to the NGINX web server.
To complete the following task you will need the IP address of your Docker host. This will need to be an IP address that you can reach (e.g. your
lab is hosted in Azure so this will be the instance’s Public IP - the one you SSH’d into). Just point your web browser to the IP and port 8080 of your
Docker host. Also, if you try connecting to the same IP address on a different port number it will fail.
If for some reason you cannot open a session from a web broswer, you can connect from your Docker host using the curl
127.0.0.1:8080 command.
curl 127.0.0.1:8080
<!DOCTYPE html>
<html>
<Snip>
<head>
<title>Welcome to nginx!</title>
<Snip>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
If you try and curl the IP address on a different port number it will fail.