Docker Network Exercise

In this lesson, we will dig deeper into container networking by supplying our own subnet and gateway when creating a new network. We will then move on to networking two different containers using an internal network. This will allow one container to be publicly accessible while the other one is not.

1. Creating a network and defining a Subnet and Gateway

Step 1: Create a bridge network with a subnet and gateway:

docker network create --subnet 10.1.0.0/24 --gateway 10.1.0.1 br02

Step 2: Run ifconfig to view the bridge interface for br02:

ifconfig

Step 3: Inspect the br02 network:

docker network inspect br02

Step 4: Prune all unused networks:

docker network prune

2. Create a network with an IP range:

docker network create --subnet 10.1.0.0/16 --gateway 10.1.0.1 \
--ip-range=10.1.4.0/24 --driver=bridge --label=host4network br04

Step 1: Inspect the br04 network:

docker network inspect br04

Step 2: Create a container using the br04 network:

docker container run --name network-test01 -it --network br04 centos /bin/bash

You may not find any troubleshooting tools like ping, netstat, or any other tools inside the centos container. You will also not find yum.

try to google them and fix the issues. mainly if you cant access yum, please refer to google on how to add repo packages list to yum repo and then follow the below commands.

2.1 Install Net Tools inside the Centos Container

yum update -y
yum install -y net-tools

Step 2.1.2: Get the IP info for the container:

ifconfig

Step 2.1.3 Get the gateway info the container:

netstat -rn

Step 2.1.4 Get the DNS info for the container:

cat /etc/resolv.conf

3. Create a new container and assign an IP to it:

docker container run -d --name network-test02 --ip 10.1.4.102 --network br04 nginx

Get the IP info for the container:

docker container inspect network-test02 | grep IPAddr

4. Networking two containers

Step 1: Create an internal network:

docker network create -d bridge --internal localhost

Step 2: Create a MySQL container that is connected to localhost:

docker container run -d --name test_mysql \
-e MYSQL_ROOT_PASSWORD=P4sSw0rd0 \
--network localhost mysql:5.7

Step 3: Create a container that can ping the MySQL container:

docker container run -it --name ping-mysql \
--network bridge \
centos

Step 4: Connect ping-mysql to the localhost network:

docker network connect localhost ping-mysql

Step 5: Restart and attach to container:

docker container start -ia ping-mysql

Step 6: Create a container that can't ping the MySQL container:

docker container run -it --name cant-ping-mysql \
centos

Step 7: Create a Nginx container that is not publicly accessible:

docker container run -d --name private-nginx -p 8081:80 --network localhost nginx

Step 8: Inspect private-nginx:

docker container inspect private-nginx

Last updated