Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure the network connection between Docker containers under Linux

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to configure the network connection between Docker containers under Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to configure the network connection between Docker containers under Linux.

Docker containers are independent, isolated environments. However, they usually work only when they communicate with each other.

Although there are many ways to connect containers, I won't try to discuss them all. But in this series of methods, we will look at those commonly used practices.

Although it may seem obvious, it is very important for friends who deal with Docker all day to understand these technologies and underlying design concepts.

Configuration

Before we show how containers are connected, we need to create a pair of containers as an example this time.

The first image will be from a simple Ubuntu operating system installation. It will act as a client container.

First, we create the container and connect to it.

The code is as follows:

$sudo docker run-itd-- name=client_setup ubuntu / bin/bash

$sudo docker attach client_setup

Next, once we have the shell program in the container, we can run the following command:

The code is as follows:

$apt-get install curl

If you can't see the shell command prompt, click the up arrow in the keyboard direction area.

When the container is installed, execute the CTRL+P and CTRL+Q commands to exit the container.

Then we stop and submit the container.

The code is as follows:

$sudo docker stop client_setup

$sudo docker commit client_setup client_img

Now we can use the container named client_img we just created.

The second container we obtained from the previous installation of the Ubuntu operating system. But this time, we will modify it to a server container running Apache HTTP.

First, we build and connect to it as before:

The code is as follows:

$sudo docker run-itd-- name=server_setup ubuntu / bin/bash

$sudo docker attach server_setup

Then, once we can use the shell program in the container, we can install Apache's HTTP service.

The code is as follows:

$apt-get install apache2

When the container is installed, execute the CTRL+P and CTRL+Q commands to exit the container.

Now let's stop and commit the container:

The code is as follows:

$sudo docker stop server_setup

$sudo docker commit server_setup server_img

So now we have two images, client_img and server_img.

When these settings are in place, we can explore the possibility of connecting a variety of containers.

Docker bridging Bridge

A single Docker container is isolated from other containers and external networks by default. Docker provides a bridge interface called docker0, which is actually set up when Docker Engine is installed.

It can communicate between containers and between containers and hosts through the bridge interface of Docker.

We can view a Docker bridge on the Docker host with the following command:

The code is as follows:

$ifconfig docker0

You can see output similar to the following:

The code is as follows:

Docker0 Link encap:Ethernet HWaddr 02:42:a2:dc:0f:a8

Inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0

Inet6 addr: fe80::42:a2ff:fedc:fa8/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:1477 errors:0 dropped:0 overruns:0 frame:0

TX packets:2436 errors:0 dropped:0 overruns:0 carrier:0

Collisions:0 txqueuelen:0

RX bytes:83901 (83.9KB) TX bytes:3606039 (3.6MB)

This bridge interface runs on a separate local Docker host, and it is the connection mechanism behind all three methods mentioned in this article. In the next chapter, we will turn to the overlay interface, which allows network containers to be deployed on multiple Docker hosts.

Port exposes Exposing Ports

First, let's look at how to run a container service and expose its port 80 (HTTP) to other containers.

To do this, I use the expose command to run the container, which tells Docker to expose a specific port when running the container. Of course, the exposed port can be accessed by other containers.

Let's run server_img and name the container server1, exposing its port 80:

The code is as follows:

$sudo docker run-itd-expose=80-name=server1 server_img / bin/bash

Next we will name these containers (server1,server2 and others) in turn.

Then, connect to the container:

The code is as follows:

$sudo docker attach server1

Again, if you can't see the shell command prompt, you can use the up arrow of the arrow key.

Start the Apache HTTP service in the container:

The code is as follows:

$/ etc/init.d/apache2 start

Let's take a look at the obtained IP address:

The code is as follows:

$ifconfig

Eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:03

Inet addr:172.17.0.3 Bcast:0.0.0.0 Mask:255.255.0.0

So, then we have the IP address of 172.17.0.3. Let's try to see this information from a client-side container.

Open the second terminal.

Start the container named client1:

The code is as follows:

$sudo docker run-itd-- name=client1 client_img / bin/bash

Go into the container and have a look:

The code is as follows:

$sudo docker attach client1

If you can't see the shell command prompt, you can use the up arrow of the arrow key.

Let's test connectivity to server1:

The code is as follows:

$curl 172.17.0.3

If all goes well, you should be able to see the default page based on the Apache HTTP service. This indicates that the client1 container has correctly established a connection to the HTTP port of the server1 container.

Port binding Port Binding

What if we want our HTTP server to be exposed to the host network, including applications on the host and other machines on the host network? In this scenario, we need to bind the host port bind to the container port.

In order for the Apache-based HTTP server to be exposed to the host's network, we need to bind port 80 of the container to port 8080 on the host.

We can do this according to the following command:

The code is as follows:

$sudo docker run-itd-p 8080 name=server2 server_img 80-- name=server2 server_img / bin/bash

What needs to be noted here is the-p 8080VR 80 option.

Now, take a look in the container:

The code is as follows:

$sudo docker attach server2

If you don't see the prompt for shell, press the up arrow in the direction area as before, and then we start the HTTP service:

The code is as follows:

$/ etc/init.d/apache2 start

Now we can access the http://localhost:8080/ from the host system, and we should be able to see the default page based on the Apache HTTP service.

Any machine in your host network can access port 8080 released by your host.

Container Link Linking Containers

Docker refers to another method involved in connecting containers as linked linking.

When you link one container to another, Docker will correlate the information between those containers through some environment variables.

We can take a look.

First, start the server container.

The code is as follows:

$sudo docker run-itd-- name=server3 server_img / bin/bash

Then use the following command to start the client container and link to the server container.

The code is as follows:

$sudo docker run-itd-link server3-name=client3 client_img / bin/bash

Notice that we use the-- link server3 option here.

Then let's log in to the client container and take a look:

The code is as follows:

$sudo docker attach client3

Then let's check the available environment variables:

The code is as follows:

$env | grep SERVER3

SERVER3_PORT_80_TCP_PROTO=tcp

SERVER3_PORT=tcp://172.17.0.2:80

SERVER3_PORT_80_TCP_PORT=80

SERVER3_NAME=/client3/server3

SERVER3_PORT_80_TCP=tcp://172.17.0.2:80

SERVER3_PORT_80_TCP_ADDR=172.17.0.2

Docker also updates the / etc/hosts file in the client container and points server3 to the server container as a local host.

To demonstrate this, let's run the following command to take a look:

The code is as follows:

$curl server3

You should see the same default HTML page again.

At this point, I believe you have a deeper understanding of "how to configure the network connection between Docker containers under Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report