默认情况下,docker容器可以调用主机的localhost UDP吗?容器、情况下、主机、UDP

2023-09-09 21:07:34 作者:人野性子浪

我有一个 docker 容器,并且还在 VM 上安装了一个守护程序,在端口 8125 上监听 UDP.容器在这个 8125 端口上使用 UDP 协议发送数据.

I have a docker container, and also installed on the VM a daemon listening for UDP on port 8125. The container sends data with UDP protocol on this 8125 port.

我试图通过使用 -p 8125:8125/udp 启动容器来打开端口,但出现以下错误:

I was trying to open the port by starting the container with the -p 8125:8125/udp, but I'm getting the following error:

Error starting userland proxy: listen udp 0.0.0.0:8125: bind: address already in use

这是有道理的,因为守护进程已经在监听这个端口.

Which makes sense because the daemon is already listening on this port.

那么如何配置 Docker 以便容器可以将 UDP 有效负载发送到外部守护进程?

So how can I configure Docker so that the container can send UDP payloads to the external daemon ?

推荐答案

只有当你想监听未发送的请求时才需要打开端口.默认情况下,Docker 为您的容器提供必要的网络命名空间,以便与主机或外部世界进行通信.

Opening ports is only needed when you want to Listen for the requests not sending. By default Docker provides the necessary network namespace for your container to communicate to the host or outside world.

所以,你可以通过两种方式:

So, you could it in two ways:

在您的 docker run 中使用 --net host 并将请求发送到 localhost:8125 在这种情况下,您的容器化应用程序是有效地共享主机的网络堆栈.所以 localhost 指向已经在你的主机中运行的守护进程.

use --net host in your docker run and send requests to localhost:8125 in this case you containerized app is effectively sharing the host's network stack. So localhost points to the daemon that's already running in your host.

与容器网络网关(通常是 172.17.0.1)或容器中主机的主机名对话.然后您就可以将数据包发送到您主机中的守护进程.

talk to the container network gateway (which is usually 172.17.0.1) or your host's hostname from your container. Then your are able to send packets to your daemon in your host.