为什么我的Docker应用程序可以在不发布端口的情况下接收UDP数据?

发布于 2025-02-10 07:04:17 字数 1321 浏览 0 评论 0原文

我正在学习Docker网络。我正在Windows上使用Docker桌面。

我正在尝试了解以下观察值:

图片中的简短版本:

”在此处输入图像描述

更长版本:

第一个设置(来自容器到主机的数据)

  • 我有一个简单的在A 容器中运行的应用。它将一个UDP-Datagram发送到主机上的特定端口(使用“ host.docker.internal”

  • i有一个在 host 上运行的相应应用。它听到端口,应该接收UDP-Datagram。

它在不发布Docker中任何端口的情况下起作用(预期行为!)。

第二个设置(从主机到容器的数据)

  • 我在 host 上有一个简单的应用。它将一个UDP-Datagram发送到Loopback网络上的特定端口(使用“ localhost”

  • 我有一个在 containe 的相应应用程序。它听到端口,应该接收UDP-Datagram。

仅当容器是使用选项-p端口:port/udp(预期行为!)运行的情况下,才能工作。

第三个设置(其他两个组合)

  • 我有一个在容器中运行的应用程序“请求者”。它将UDP请求消息发送到主机上的特定端口,然后要接收响应消息。

  • 我有一个在 host 上运行的相应应用程序“响应者”。它听到端口,应该接收请求消息。然后,它将UDP响应消息发送到请求 - 消息的终点。

这也有效,而且 - 这就是我不明白的 - 如果不发布响应消息的端口!

这是如何运作的?我很确定有一些基本的网络知识,我根本没有解释这一点。我很高兴学习一些背景。

Sidenote:

由于我可以从容器内部成功进行curl www.google.com,所以我意识到容器绝对不能发布端口以接收任何数据。但是这里涉及建立连接的TCP。另一方面,UDP是“无连接”,因此不能是(整体)解释。

I'm learning Docker networking. I'm using Docker Desktop on Windows.

I'm trying to understand the following observations:

Short version in a picture:

enter image description here

Longer version:

First setup (data from container to host)

  • I have a simple app running in a container. It sends one UDP-datagram to a specific port on the host (using "host.docker.internal")

  • I have a corresponding app running on the host. It listens to the port and is supposed to receive the UDP-datagram.

That works without publishing any ports in docker (expected behavior!).

Second setup (data from host to container)

  • I have a simple app on the host. It sends one UDP-datagram to a specific port on the loopback network (using "localhost")

  • I have a corresponding app running in a container. It listens to the port and is supposed to receives the UDP-datagram.

That works only if the container is run with option -p port:port/udp (expected behavior!).

Third setup (combination of the other two)

  • I have an app "Requestor" running in a container. It sends a UDP request-message to a specific port on the host and then wants to receive a response-message.

  • I have a corresponding app "Responder" running on the host. It listens to the port and is supposed to receive the request-message. Then it sends a UDP response-message to the endpoint of the request-message.

This works as well, and - that's what I don't understand - without publishing the port for the response-message!

How does this work? I'm pretty sure there's some basic networking-knowledge that I simply don't have already to explain this. I would be pleased to learn some background on this.

Sidenote:

Since I can do curl www.google.com successfully from inside a container, I realize that a container definitely must not publish ports to receive any data. But there's TCP involved here to establish a connection. UDP on the other hand is "connectionless", so that can't be the (whole) explanation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2025-02-17 07:04:17

经过进一步的调查, nat 似乎是答案。

根据这些解释 > loopback 接口和 docker0 桥。

由于以下内容,因此Windows的Docker Desktop无法识别这一点(“ nofollow noreferrer”> source ):

由于Windows的Docker桌面中实现了网络的方式,因此您在主机上看不到Docker0接口。该接口实际上在虚拟机中。

After further investigation, NAT seems to be the answer.

According to these explanations, a NAT is involved between the loopback interface and the docker0 bridge.

This is less recognizable with Docker Desktop for Windows because of the following (source):

Because of the way networking is implemented in Docker Desktop for Windows, you cannot see a docker0 interface on the host. This interface is actually within the virtual machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文