Docker - Network communication between Containers

You can connect several containers with each other to communicate directly through a dedicated network. This is recommended if you want to setup a system of microservices and you want to run services separate in their own Docker containers.

It is possible to create a virtual network in Docker which will be used for the communication between the Docker containers. We will use here the hosting of an CMS website (Wordpress because of its popularity) as an example.


1. First, we need to create the virtual network

docker network create website_network

In this tutorial the network has the name "website_network".

 

 

2. Test the new created network (optional)

You can also use the created network only for a container for test puposes as an example. If you want to start a container connected with the now created network, then you can do that with this command:

docker run -it --network website_network image_name

Here an example a Docker container (without a name) with the Docker image "image_name".

If your Docker container is running, then you have to use another command.

docker network connect website_network my_container

You write the name of the created network (that you want to connect to) and then the name of the container that should be connected with that network.



3. Connect the Docker containers with each other through this created network.

We will create a container ("cmswebsite") for the Wordpress website and a container ("mysqldb") for the MySQL server and database.
This will be linked with each other through this new created network "website_network". You have to run this command in this exact order and the second container must be started one minute later after the first container.

Start the first container with a MySQL Docker image.

docker run -d --name mysqldb -e MYSQL_ROOT_PASSWORD=your_mysql_password mysql

 

Start the second container with a Wordpress Docker image.

docker run -d --name cmswebsite --link mysqldb:mysql -p 10006:80 wordpress

The containers must be started in this order to enable the linking between each other.
"--link" - This is used to connect the container of the MySQL database with the MySQL port of the Wordpress container. All data that is sent to the MySQL port in the Wordpress container will be directed to the MySQl database container.
"--p" - Link the port 80 in the container with the port 10006 on the host server. It is the equivalent to the command "EXPOSE" in a dockerfile.
"--e" - Run SHELL commands on the startup of this container. It is the equivalent to the command "CMD".

You can access now the Wordpress website through the port 10006. The new created network can be expand to other containers too.
This was an example on how to use a Docker virtual network to connect two Docker containers with each other.

 

If you want to create several Docker container, then it is recommended to use docker-compose (file: docker-compose.yml). Your containers will be grouped together and run in an isolated environment.

Here are some advanced settings for the command "docker network create":

Docker creates a bridge as default if you do not define a certain driver type for your network.
Here an example command to create a overlay network:

docker network create -d overlay my_network

 

You can also define the used ip addresses on your Docker network.
The subnet, ip adress range and the gateway ip adress are set with the commands "--subnet", "--ip-range", and "--gateway":

docker network create --driver=bridge \
  --subnet=172.29.0.0/16 \
  --ip-range=172.29.5.0/24 \
  --gateway=172.29.5.254 \
  my_network

 

Activate IPv6 in the Docker network:

docker network create --ipv6=true my_network

 

More information about "docker network":
https://docs.docker.com/engine/reference/commandline/network/

More information about "docker network create":
https://docs.docker.com/engine/reference/commandline/network_create/

Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
Ok