gpt4 book ai didi

docker - 在Docker中,如果我将网络设置为主机,是否需要发布端口?

转载 作者:行者123 更新时间:2023-12-02 19:11:21 27 4
gpt4 key购买 nike

我今天遇到一个问题,我有一个Dockert文件,它用EXPOSEs几个端口,我想用--net=host标志运行它。

但是,到该容器应该监听的端口的所有连接均被拒绝。

在容器上运行docker inspect,我注意到了这一点:

        "Ports": {
"8000/tcp": {},
}

越来越生气,我一起删除了 --net标志,并转到默认的 bridge网络。惊喜!
    "Ports": {
"8000/tcp": null,
}

除了现在,它具有这种奇怪的 null设置。这有什么区别?另外,画出我在一个VM内部运行,试图与另一个VM通信。可能有百万个原因无法解决。

最佳答案



Is the publish option needed when the network mode is host?



回答

否,容器直接使用主机网络堆栈:

'host': use the Docker host network stack. Note: the host mode gives the container full access to local system services such as D-bus and is therefore considered insecure.



证明

使用 netcat启动一个容器:
user@host:~$ docker run -it --rm --net host nc:1.10-41
root@container:/# nc -l -p 9999

返回主机:
user@host:~$ nc 127.0.0.1 9999
Sending a message for test <enter>

该消息将通过在容器内执行的 netcat命令显示。

监控方式

来自主机的 netstat将显示已建立的连接:
user@host:~$ netstat latuep |grep 9999
tcp 0 0 localhost:38600 localhost:9999 ESTABLISHED
tcp 0 0 localhost:9999 localhost:38600 ESTABLISHED

至于你的问题

该错误可能源于另一个配置/网络环境。虚拟机可以互相ping通吗?他们共享同一局域网吗?是否设置了防火墙?

关于docker - 在Docker中,如果我将网络设置为主机,是否需要发布端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36319973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com