Detailed explanation of three implementation methods of Docker container interconnection

  • 2021-08-17 01:24:04
  • OfStack

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

docker internal network: Not flexible enough, not recommended docker networking: 1.9 or later, recommended docker Link: Prior to 1.9 applies.

1. docker internal network

Involves docker's own network stack.

When docker is installed, a new network interface named docker0 is created to connect the container and host, the IP range is 172.16-172.30,

Each docker container assigns an IP address to this interface.

Every time docker creates a container, it will create a set of interconnected network interfaces, one end is the eth0 interface in the container, and the other end is named after veth in the host machine.

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

Implement the communication connection between the container and the host machine, noting that only the veth interface exists when the container is running.

Disadvantages of using internal network to realize interconnection;

The IP address of another container should be hard-coded in the application of the container; After the container restarts, the IP address may change; Not convenient and flexible enough.

2.docker networking

Connections between containers are created using networks.

Allow users to create their own network 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 the container without updating the connection;

There is no need to create a container to connect it in advance, and there is no need to care about the running sequence of the container, and the container name resolution and discovery can be obtained within the network;

Integrated with docker, compose and swarm;

Containers started 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 using hostname or hostname. netname,

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

In the test, it was found that the/etc/hosts file did not seem to add the address of other containers, but it could also communicate with each other ping;

One container can join multiple networks at the same time, so it can create a very complex network model;

docker network create Create 1 Network docker network inspect View Network Details docker network ls Lists all networks in the current system docker network connect Connect an existing container to 1 network docker network disconnect interrupts 1 container from the network docker network rm Delete 1 or more networks docker network prune Remove All Unused Networks

3. docker link

The name of the container should be referenced during the linking process, and it can only work on the same host.

Create a customer-service link between the two containers with the parameter link when docker run starts the container,

Two parameters are required, one is the name of the link container and one 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 client container,

The client container can directly access any open port of the service container, so the port of the service container does not need to be open to the local host, which is relatively safer;

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

docker writes link information in the/etc/hosts file of the container and the environment variable containing the link information;

Either way, you can create an Web application stack with the following components:

1 Web Server Container 1 Redis database container 1 secure link between two containers

Related articles: