【问题标题】:Windows + Docker + Port not exposed/reachableWindows + Docker + 端口未公开/无法访问
【发布时间】:2018-01-16 09:42:09
【问题描述】:

问题:

来自 Windows 主机。端口映射/暴露不起作用。

容器应用不可访问。

curl http://localhost:PORT (127.0.0.1 / 0.0.0.0)
-> Could not get any response

curl http://DOCKER-IP:PORT (127.17.0.1 / 127.17.0.2)
-> Could not get any response

curl http://DOCKER-SUBNET-ADDRESS:PORT (10.0.75.0)
-> Could not get any response

设置:

Windows10主机系统 + Docker 容器(带有简单的 helloworld python flask app)

Docker 容器正在运行。 在 docker 容器内,flask 应用程序的行为与预期相同。

复制:

  1. 获取 hello world 代码库

    git clone https://github.com/shekhargulati/python-flask-docker-hello-world

  2. 构建并运行 docker 容器

    docker build -t simple-flask-app:latest . docker run -d -p 5000:5000 simple-flask-app --name simple-flask-app

  3. 执行到容器中并检查烧瓶是否正在运行

    docker exec -it simple-flask-app apt-get install curl curl http://127.0.0.1:5000

    -> 见200 OK + Flask is running(或类似这样的文字)

  4. 退出 docker 容器

    exit

  5. 在你的 Windows 主机上卷曲

    curl http://127.0.0.1:5000

    -> 见Could not get any response(或类似的东西)

【问题讨论】:

    标签: windows docker port


    【解决方案1】:

    您需要将应用设置为在 host='0.0.0.0' 上运行。那就是:

    app.run(host='0.0.0.0')
    

    所以当你运行时:

    > docker run -d -p 5000:5000 simple-flask-app --name simple-flask-app
    * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    * Restarting with stat
    

    然后curl http://localhost:5000 工作。

    功劳归于: https://codefresh.io/docker-tutorial/hello-whale-getting-started-docker-flask/

    【讨论】:

    • 这个!经过无数次搜索和尝试修复、重新安装、路由更改、尝试使用直接 IP...这就是解决方案,并且没有人在任何地方提及它...
    • 终于!就我而言,它是在 docker 容器中使用 vue-cli-service serve --host 0.0.0.0 启动我的应用程序
    【解决方案2】:

    我刚刚更改了 name 参数的顺序,它可以工作(在 Linux 中)。

    docker run -d -p 5000:5000 --name simple-flask-app simple-flask-app
    

    【讨论】:

    • 这解决了我的问题。最后我有 -p xxxx:xxxx。
    • ARGH 为什么@#$@# 会有所作为!
    【解决方案3】:

    短解决方案

    1. 运行您的 docker 应用程序 1.1(执行到您的 docker 并确保您的应用按预期运行)
    2. 在您的 Windows 主机上

      ipconfig

    3. 其中一个公开的 IPv4 地址可以使用。一个一个试试吧。

      curl http://IPCONFIG-IPv4-TRY-OUT:PORT -> 200 OK + Flask is running

    -> Windows 上的 Docker 不会将流量转发到 linux/mac 中已知的 docker 容器。

    -> 看起来流量被转发到了 eth0。

    【讨论】:

      【解决方案4】:

      对我来说,最简单的解决方案是运行:

      docker-machine ip default
      

      这会打印一个 IP 地址。在我的例子中,ipconfig 没有显示这个 IP。 我在 Windows 7 主机上使用 Docker 18.03.0-ce 进行了测试。

      我在这里找到了这个提示:https://www.jhipster.tech/tips/020_tip_using_docker_containers_as_localhost_on_mac_and_windows.html

      【讨论】:

      • 是的,运行 docker-machine ip 然后使用提供的 ip 而不是本地主机:http://<docker-machine ip>:<exposed port>
      【解决方案5】:

      您需要连接到实际运行 docker 的 VM:

      通过运行docker-machine ip default 查找IP 地址并使用该IP 进行连接:

      curl http://<ip-address>:5000
      

      【讨论】:

        【解决方案6】:

        与烧瓶无关,但我最近遇到了同样的问题。我们的主机需要一个静态 IP 地址,因此我们的 IT 更改了一些网络配置。然后我注意到控制面板\网络和 Internet\网络连接中的 vEthernet 适配器被禁用。启用此端口转发到容器实例后,似乎又可以工作了。

        【讨论】:

          猜你喜欢
          • 2018-10-07
          • 2016-11-04
          • 1970-01-01
          • 2015-01-12
          • 2019-06-01
          • 2019-04-29
          • 2016-12-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多