lesson 3: Create and manage a Network in Docker, connect the container to the Network

Tram Ho

Continuing with the series of learning and practicing Docker, today we will learn about the knowledge of Network in docker, the basics of creating network management and connection in docker, how containers connect to Each other, the way from which the host machine accesses the container through the ports.

Start to learn and practice only !!!

Concept and default Docker Network

Docker Network concept

Docker network is the place that will take charge of the container connecting to the network

  • Containers on the same network can communicate with each other through the name of the container and the port that is listened to for the container on that network.
  • Connect on 1 host or more hosts,
  • Connections between clusters (swarm) docker containers.
  • Connect the container to other networks outside of docker.
  • Can provide most of the functions that a normal network needs.

Default Networks

There are 3 types of networks that are automatically created in docker: bridge, none, the host can be viewed with the command docker network ls

bridge is the docker’s default network driver, bridge is the most suitable driver for communicating independent containers. Containers on the same network can communicate with each other over IP addresses, if a driver is not specified, the bridge will be the default network driver when initialized.

none driver provides a container networking stack and its own network space, often used with a custom network, this driver cannot be used in the swarm cluster.

host is used when the container needs to communicate with the host and directly use the running server’s network

Practice creating and managing networks

Create a connection between 2 containers

In this practice we will create 2 containers and reside in a network bridge so that these two containers can connect to each other.

  1. Download image busybox: is a small tool containing available ubuntu commandsdocker pull busybox

  1. Create container B1 from the busybox imagedocker run -it --rm --name B1 busybox

    Add the –rm parameter so that when we stop the container, it will automatically delete this container

    Once the B1 container is created, we can turn on another terminal to check the network bridge with the command

    docker network inspect bridge

    We see that B1 is connected to this network

    Or you can test a currently connected container in any network with the command: docker inspect B1

  2. Create container B2 from the busybox image similar to creating container B1Once the B2 container is created, exit the container with the keys strl + p and strl + q, then run the docker network inspect bridge command to check which containers connect to this network, we see both B2 and B1.

  3. Try ping from B1 to B2The photo above shows that B1 has IPV4:, B2 has IPV4:

    At the terminal, if standing outside B1, we attach to B1 with the command docker attach B1

    Stand in container B1 and ping to B2 with ping , the ping is successful so that container B1 has connected to container B2.

Turn off your practice containers to continue with the next part

Make connection from host machine to container

In this practice we will create a B3 container from busybox and this container has an httpd web server to run html file, this container has a port mapping to the host’s port so that from the host machine can access the web server on This busybox container

1: Create a B3 container and map its port 80 to port 8888 of the host machine

docker run -it --name B3 -p 8888:80 busybox

Turn on another terminal to check the running containers

Create container B3 and see that posts are mapped from port 8888 of the host machine to port 80 of the container, which means when we access port 8888 on the host machine, it is to access port 80 of the container.

2: Go back to the terminal of B2, go to the var / www directory and turn on the httpd web server

Then we create file index.html and edit this file

: wq! to save and exit the file

3: From the host computer to the browser, access the container via port 8888

So successfully connected from host machine to container

Create a docker network

In addition to using existing networks, you can create private networks so that containers can be used separately from those containers.

1: Create a network

docker network create --driver bridge network1

The above command creates the network1 we test with the docker network ls command

2: Delete the network

docker network rm name_network

3: Create a container to connect to a specified network

docker run -it --name B4 --network network1 busybox

Above command we create a container named B4 from the busybox image and have connections on network1 newly created above

Check with the docker network inspect network1 command and see that the container B4 is connected

4: Connect a running container to another network

For example: we have 2 networks network1 and network2 has a B5 container that is connecting to network1 and we want this container to connect to network2 then we run the command

docker network connect network2 B5

The above command is to connect the B5 container to the network2 network


Above we have learned the basics of creating and managing and connecting networks in docker. Along follow the next article of the journey to conquer docker with me. Thanks mn

Share the news now

Source : Viblo