There are three ways to interconnect and communicate between Docker containers:
1. Docker internal network Involving Docker's own network stack. After installing Docker, the system will create 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 will be assigned an IP address on this interface. Every 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 starting with veth on 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 only exists when the container is running. Disadvantages of using internal network to realize interconnection:
2. Docker networking Connections between containers are created using networks. Allow users to create their own networks through which containers communicate with each other; It can communicate across different hosts, and the network configuration is more flexible; You can stop, start, or restart containers without having to update connections. You don’t need to create a container in advance and then connect to it, nor do you need to worry about the order in which the containers run. You can obtain container name resolution and discovery within the network. Integrated with docker compose and swarm; A container started inside a docker network will be aware of all containers running in this network. And save the addresses of these containers to the local DNS through the /etc/hosts file, If any container is restarted, its IP address will be automatically updated in the /etc/hosts file. During the test, it was found that there seemed to be no new addresses of other containers in the /etc/hosts file, but they could ping each other. A container can join multiple networks at the same time, so very complex network models can be created;
3. Docker Link The name of the container must be referenced during the linking process, and it can only work on the same host machine. When starting the container with docker run, use the --link parameter to create a client-server link between the two containers. Two parameters are required, one is the name of the link container, and the other is the alias of the link, that is, --link redis:db. The linked container is the service, and the link allows the service container to communicate with the client container. The client container can directly access any public port of the service container, so the service container's port does not need to be exposed to the local host, which is relatively safer; You can link multiple client containers to the same service container, or you can link to multiple service containers by specifying --link multiple times. Docker writes the link information in the container's /etc/hosts file and in the environment variables containing the link information; Regardless of the approach you take, you can create a web application stack that includes the following components:
The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Summary of various implementation methods of mysql database backup
>>: Implementation code of using select to select elements in Vue+Openlayer
Table of contents 1. List interface display examp...
Today I have a question about configuring MySQL d...
Use JOIN instead of sub-queries MySQL supports SQ...
Table of contents Problems with resource manageme...
This article example shares the specific code of ...
Table of contents Preface Function Overloading Ma...
Table of contents 1. Lock and Latch 2. Repeatable...
Table of contents Problems Redux Toolkit solves W...
Detailed explanation of the implementation method...
The Golden Rule No matter how many people are wor...
swarm three virtual machines 132,133,134 1. Initi...
Table of contents 1. Connect to Tencent Cloud Ser...
Preface: js is a single-threaded language, so it ...
Today, this article introduces some basic concept...
Running environment, Idea2020 version, Tomcat10, ...