为什么我的Docker应用程序可以在不发布端口的情况下接收UDP数据?
我正在学习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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过进一步的调查, nat 似乎是答案。
根据这些解释 > loopback 接口和 docker0 桥。
由于以下内容,因此Windows的Docker Desktop无法识别这一点(“ nofollow noreferrer”> source ):
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):