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

Explain in detail how Docker containers are connected to each other

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

What the editor wants to share with you this time is to explain in detail how Docker containers are connected to each other. The article is rich in content. Interested friends can learn about it. I hope you can get something after reading this article.

There are three ways to interconnect and communicate between docker containers:

Docker internal network: not flexible and powerful enough, docker networking:1.9 or newer versions are not recommended, docker links are recommended: versions prior to 1.9 are applicable.

1.docker internal network

Involves docker's own network stack.

After installing docker, the system creates a new network interface named docker0, which is used to connect the container and the host. The IP range is 172.16-172.30.

Each docker container assigns an IP address on this interface.

Each time docker creates a container, it creates a set of interconnected network interfaces. One end is the eth0 interface in the container, and the other end is named with veth in the host.

By binding each veth interface to the docker0 bridge, docker creates a virtual subnet that is shared by the host and all docker containers

Realize the communication connection between the container and the host. Note that the veth interface exists only when the container is running.

The disadvantages of using internal networks to achieve connectivity:

The IP address of another container should be hard-coded in the container's application; the IP address may change after the container is restarted; it is not convenient and flexible.

2.docker networking

Connections between containers are created with a network.

Allow users to create their own network through which containers communicate with each other

Can communicate across different hosts, and the network configuration is more flexible.

You can stop, start, or restart the container without updating the connection

You don't have to create a container in advance to connect to it, and you don't have to care about the running order of the container. You can get container name resolution and discovery within the network.

Integrated with docker compose and swarm

Containers launched inside the docker network will be aware of all containers running under this network.

And save the addresses of these containers to the local DNS through the / etc/hosts file

Any host in the network can be parsed and accessed in the form of hostname or hostname.netname

If any container is restarted, its IP address will be automatically updated in the / etc/hosts file

During the test, it is found that the addresses of other containers do not seem to be added in the / etc/hosts file, but they can also ping each other.

A container can join multiple networks at the same time, so you can create a very complex network model.

Docker network create creates a network docker network inspect to view network details docker network ls lists all networks in the current system docker network connect connects an existing container to a network docker network disconnect disconnects a container from the network docker network rm deletes one or more network docker network prune deletes all unused networks

3.docker link

The name of the container should be referenced during the link process and can only work in the same host.

Use the parameter link to create a customer-service link between the two containers when docker run starts the container

Two parameters are required, one is the name of the link container, and the other is the alias of the link, namely-- link redis:db

The linked container is the service, and the link enables the service container to communicate with the customer container

The customer container can directly access any public port of the service container, so the port of the service container does not need to be exposed to the local host, which is relatively more secure.

You can link multiple customer containers to the same service container, or you can link to multiple service containers by specifying multiple times-- link

Docker writes link information in the container's / etc/hosts file and in the environment variable that contains the link information

Either way, you can create a Web application stack that contains the following components:

A Web server container A Redis database container A secure link between two containers after reading this article on how to connect Docker containers to each other, if you think the article is well written, you can share it with more people.

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