Detailed explanation of three ways to connect Docker containers to each other

Detailed explanation of three ways to connect Docker containers to each other

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

  • Docker internal network: not flexible and powerful enough, not recommended
  • Docker networking: 1.9 or later, recommended
  • Docker link: Applicable to versions before 1.9.

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:

  • To hardcode the IP address of another container in the container's application;
  • After the container is restarted, 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 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,
Any host in the network can be resolved and accessed using the format 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 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;

  • docker network create creates a network
  • docker network inspect 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 removes one or more networks
  • Docker network prune removes all unused networks

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:

  • A web server container
  • A Redis database container
  • A secure link between two containers

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:
  • Docker SSH sample code to connect to docker container
  • Introduction to Docker connection spring boot and mysql container method
  • Detailed explanation of docker: Allow host ssh to connect to docker container
  • How does Docker use links to establish connections between containers?
  • Docker multi-container connection (taking Tomcat+MySQL as an example)
  • Detailed explanation of building a Mysql container + Tomcat container connection environment through Docker
  • Detailed explanation of creating a MySQL container with Docker and connecting to the container through the command line
  • Docker uses Link to establish connections between containers
  • Docker container connection implementation steps analysis

<<:  Summary of various implementation methods of mysql database backup

>>:  Implementation code of using select to select elements in Vue+Openlayer

Recommend

Docker network mode and configuration method

1. Docker Network Mode When docker run creates a ...

Docker enables seamless calling of shell commands between container and host

As shown below: nsenter -t 1 -m -u -n -i sh -c &q...

Detailed steps for installing and configuring MySQL 8.0 on CentOS

Preface Here are the steps to install and configu...

Installation process of zabbix-agent on Kylin V10

1. Download the installation package Download add...

Rounding operation of datetime field in MySQL

Table of contents Preface 1. Background 2. Simula...

How to use vs2019 for Linux remote development

Usually, there are two options when we develop Li...

How to implement a multi-terminal bridging platform based on websocket in JS

Table of contents 1. What to debug 2. Features of...

Detailed steps to install and uninstall Apache (httpd) service on centos 7

uninstall First, confirm whether it has been inst...

Use Python to connect to MySQL database using the pymysql module

Install pymysql pip install pymysql 2|0Using pymy...

MYSQL updatexml() function error injection analysis

First, understand the updatexml() function UPDATE...

mysql8.0.23 linux (centos7) installation complete and detailed tutorial

Table of contents What is a relational database? ...

The "3I" Standards for Successful Print Advertising

For many domestic advertisers, the creation and ev...

CSS text alignment implementation code

When making forms, we often encounter the situati...

Analysis of the principles and usage of Docker container data volumes

What is a container data volume If the data is in...