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

What are the three ways to communicate between Docker containers?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

What are the three ways of communication between Docker containers? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Containers can communicate with each other through IP,Docker DNS Server or joined containers.

IP communication

For two containers to communicate, they must have a network card that belongs to the same network.

Once this condition is met, the container can interact through IP. The way to do this is to specify the appropriate network through-- network when the container is created, or to add the existing container to the specified network through docker network connect.

Docker DNS Server

Although accessing the container through IP meets the needs of communication, it is still not flexible enough. Because we may not be able to determine the IP before deploying the application, it will be troublesome to specify the IP to be accessed after deployment. This problem can be solved through the DNS service that comes with docker.

Starting with Docker version 1.10, docker daemon implements an embedded DNS server that allows containers to communicate directly through the container name. The method is simple, as long as you name the container with-- name at startup.

Let's start two containers, bbox1 and bbox2:

Docker run-it-network=my_net2-name=bbox1 busybox

Docker run-it-network=my_net2-name=bbox2 busybox

Then bbox2 can ping directly to bbox1.

There is a limitation to using docker DNS: it can only be used in user-defined networks. In other words, DNS cannot be used on the default bridge network. Let's verify this:

Create bbox3 and bbox4, both connected to the bridge network.

Docker run-it-- name=bbox3 busybox

Docker run-it-- name=bbox4 busybox

Bbox4 cannot ping to bbox3.

Joined container

Joined containers are another way to communicate between containers.

The joined container is very special in that it enables two or more containers to share a network stack, network card and configuration information, and joined containers can communicate directly through 127.0.0.1. Take a look at the following example:

First create a httpd container named web1.

Docker run-d-it-- name=web1 httpd

Then create the busybox container and specify the jointed container as web1 with-- network=container:web1

Please pay attention to the network configuration information in the busybox container. Let's take a look at web1's network.

Look! The network card mac addresses of busybox and web1 are exactly the same as IP, and they share the same network stack. Busybox can access web1's http service directly with 127.0.0.1.

The joined container is ideal for the following scenarios:

Programs in different containers want to communicate efficiently and quickly through loopback, such as web server and app server.

You want to monitor the network traffic of other containers, such as network monitoring programs that run in separate containers.

The answers to the questions about the three ways of communication between Docker containers are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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