【问题标题】:With docker --net="host" i can access the ports使用 docker --net="host" 我可以访问端口
【发布时间】:2018-09-12 20:33:56
【问题描述】:

当我为侦听端口 8090 的 Web 应用程序运行容器时

docker run -p 8090:8090 -h=%ComputerName% mycontainer

然后我可以访问http://localhost:8090上的服务

如果我用

启动容器

docker run --net="host" -h=%ComputerName% mycontainer

然后我无法访问http://localhost:8090上的服务

为什么??

不应该 -net="host" 容器共享主机的网络,那么为什么我无法使用 --net="host" 访问http://localhost:8090

【问题讨论】:

标签: docker


【解决方案1】:

这不是--net=host 所做的。

在你的第一个例子中;您正在将容器的端口映射到您的主机 - 这允许您通过容器的服务。

在第二个例子中;您删除了 -p 选项,因此现在没有映射端口

--net=host 所做的 - 是允许您的容器查看 主机 上的端口,就好像它们是容器的本地端口一样。假设您在主机的 5000 端口上运行了一个数据库,但它不在 Docker 容器中 - 您可以通过localhost:5000 访问容器上。 (注意 - 对此有一些警告;例如 Docker for Mac 实际上需要 docker.for.mac.localhost

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多