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:

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

Step 2.1.2: Get the IP info for the container:

Step 2.1.3 Get the gateway info the container:

Step 2.1.4 Get the DNS info for the container:

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

Get the IP info for the container:

4. Networking two containers

Step 1: Create an internal network:

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

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

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

Step 5: Restart and attach to container:

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

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

Step 8: Inspect private-nginx:

Last updated