【问题标题】:Docker container localhost access issueDocker容器本地主机访问问题
【发布时间】:2018-10-02 05:25:02
【问题描述】:

我在 VM 中运行了 docker。我正在运行 2 个容器,如下所示(已消除噪音)。

[abc_dev@abclocaldev ~]$ docker ps

NAMES             PORTS                     

happy_stallman                                          

repository        0.0.0.0:30081->8081/tcp   

repository 有一个正在运行的应用程序,可以从端口 30081 访问。我可以从 VM 和主机访问它(VM 上的端口转发)。 happy_stallman 无法访问 127.0.0.1:30081 上的存储库,我收到连接被拒绝。

有人知道怎么回事吗?

我想补充一点,happy_stallman 能够访问 google 以及 Intranet 上的其他应用程序。

【问题讨论】:

    标签: docker networking


    【解决方案1】:

    默认情况下,docker 容器在bridge 网络上运行。当您尝试从容器内部访问127.0.0.1:8080 时,您正在访问容器的8080 端口。

    为了演示,让我们尝试使用它的 IP 地址访问另一个容器。启动简单服务器:

    $ docker run -it -p 8080:8080 trinitronx/python-simplehttpserver
    Serving HTTP on 0.0.0.0 port 8080 ...
    

    然后切换到另一个终端,检查8080是否暴露给主机:

    $ wget 127.0.0.1:8080
    --2018-10-02 10:51:14--  http://127.0.0.1:8080/
    Connecting to 127.0.0.1:8080... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 178 [text/html]
    Saving to: <<index.html.5>>
    
    index.html.5                               100%[=====================================================================================>]     178  --.-KB/s    in 0s
    
    2018-10-02 10:51:14 (18.9 MB/s) - <<index.html.5>> saved [178/178]
    

    容器提供了一个文件,工作正常。现在让我们尝试使用另一个容器来做同样的事情:

    $ docker run -it alpine wget 127.0.0.1:8080
    Connecting to 127.0.0.1:8080 (127.0.0.1:8080)
    wget: can't connect to remote host (127.0.0.1): Connection refused
    

    不起作用,因为127.0.0.1这里是alpine的本地地址,而不是主机地址。

    要获取容器 IP,请使用以下命令:

    $ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 4f1fe52be173
    172.17.0.3
    

    4f1fe52be173 是容器名称。指定正确的 IP 后,容器可以访问另一个容器端口:

    $ docker run -it alpine wget 172.17.0.3:8080
    Connecting to 172.17.0.3:8080 (172.17.0.3:8080)
    index.html           100% |*******************************|   178   0:00:00 ETA
    

    如果您使用 docker-compose,这可以简化:

    $ cat docker-compose.yml
    version: '3'
    services:
      web:
        image: trinitronx/python-simplehttpserver
        ports:
          - "8080:8080"
      client:
        image: alpine
        command: wget web:8080
        depends_on:
          - web
    
    $ docker-compose up
    Creating soon_web_1 ... done
    Creating soon_client_1 ... done
    Attaching to soon_web_1, soon_client_1
    web_1     | soon_client_1.soon_default - - [02/Oct/2018 05:59:16] "GET / HTTP/1.1" 200 -
    client_1  | Connecting to web:8080 (172.20.0.2:8080)
    client_1  | index.html           100% |*******************************|   178   0:00:00 ETA
    client_1  |
    soon_client_1 exited with code 0
    

    如您所见,没有直接指定容器 IP 地址。相反,您使用web:8080 访问容器端口。

    请注意,depends_on 不会等到容器“准备就绪”。为了更好地控制,请阅读本指南:https://docs.docker.com/compose/startup-order/

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 2022-01-07
      • 2020-02-17
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多