Docker – 4. Network

Network allows to connect ports from the inside of the container to the host system and create a network between the containers.

Hint: Docker is running perfectly on Active24 VPS. Try out the premium virtual server today!

Opening the port for the host system

In the previous part we learned how to share the data (volume). We can also open a port at which the network communication is running. For example the web server Nginx receives the inquiries on a port 80, but so far only in the container. Let’s move to the directory ~/docker-test (first line) and there with an unchanged configuration file Dockerfile we build (second line) and run (third line) a container called nginx-test, similarly to the previous part.

But now we will add in the third line another setting -p 80:80, that will publish the communication on the port 80 in the container (number before colon) on the port 8080 on the host system (number behind colon):

cd ~/docker-test
docker build -t nginx-test .
docker run -d -v ~/docker-test/html:/usr/share/nginx/html -p 80:8080 nginx-test

From now on, we do not need to log in to the container to load the content of the index.html file in the directory ~/docker-test/html via web server Nginx. Now we can simply run a command on the host system:

curl localhost:8080

Content of the index.html file is now accessible from the container and also from the host system.

If you need to secure the communication from the container with other containers (not only with the host system), download the network configuration.

Network between containers

Thanks to the network we can isolate a channel where the containers are able to communicate between each other. It is usual to create such channel if you are running a web server in one container and a database server on another. If the web server from the first container needs to access the data from the database, both containers must align to the same network. We will simplify this and we will work only with the containers with web server Nginx. Create a network called network-test:

docker network create network-test

Check if the network is created:

docker network ls

Build and run 2 containers called nginx-test-1 (first two lines) and nginx-test-2 (lst 2 lines) and both add to the site network-test by a command --net=network-test. If we haven’t changed the configuration file Dockerfile, run these 4 commands:

docker build -t nginx-test-1 .
docker run -d --net=network-test nginx-test-1
docker build -t nginx-test-2 .
docker run -d --net=network-test nginx-test-2

Identify the IP address of the containers assigned to the network network-test:

docker network inspect network-test

In the part Containers we are looking for IPv4Address of both containers (here 172.18.0.2 and 172.18.0.3, the may differ for you). First IP address belongs to the container nginx-test-1 and the second to nginx-test-2.

Check for ID of the containers (first line; yours will differ) and log in to the container nginx-test-1 with IP address 172.18.0.2 (second line):

docker ps
docker exec -it d5cef9ab1115 bash

Now all that is left to check is if the file index.html loads via web server Nginx in the container nginx-test-1 with the IP addresss 172.18.0.2 from the nginx-test-2 with IP address 172.18.0.3:

curl 172.18.0.3

If the content of the file index.html is dislayed, it means , that both of the containers can communicate via network. Container nginx-test-1 as well as nginx-test-2 are successfully connected ti an isolated site network-test, where they see each other and can exchange information.

Summary

We learned how to open container on a port and how we can make the containers communicate between each other on a private internal network.

Updated on December 18, 2024

Was this article helpful?

Related Articles