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 br02Step 2: Run ifconfig to view the bridge interface for br02:
ifconfigStep 3: Inspect the br02 network:
docker network inspect br02Step 4: Prune all unused networks:
docker network prune2. 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 br04Step 1: Inspect the br04 network:
docker network inspect br04Step 2: Create a container using the br04 network:
docker container run --name network-test01 -it --network br04 centos /bin/bashYou 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 -yyum install -y net-toolsStep 2.1.2: Get the IP info for the container:
ifconfigStep 2.1.3 Get the gateway info the container:
netstat -rnStep 2.1.4 Get the DNS info for the container:
cat /etc/resolv.conf3. Create a new container and assign an IP to it:
docker container run -d --name network-test02 --ip 10.1.4.102 --network br04 nginxGet the IP info for the container:
docker container inspect network-test02 | grep IPAddr4. Networking two containers
Step 1: Create an internal network:
docker network create -d bridge --internal localhostStep 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.7Step 3: Create a container that can ping the MySQL container:
docker container run -it --name ping-mysql \
--network bridge \
centosStep 4: Connect ping-mysql to the localhost network:
docker network connect localhost ping-mysqlStep 5: Restart and attach to container:
docker container start -ia ping-mysqlStep 6: Create a container that can't ping the MySQL container:
docker container run -it --name cant-ping-mysql \
centosStep 7: Create a Nginx container that is not publicly accessible:
docker container run -d --name private-nginx -p 8081:80 --network localhost nginxStep 8: Inspect private-nginx:
docker container inspect private-nginxLast updated