Docker 容器 localhost 访问问题

标签 docker networking

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

[abc_dev@abclocaldev ~]$ docker ps

NAMES             PORTS                     

happy_stallman                                          

repository        0.0.0.0:30081->8081/tcp   

存储库 有一个应用程序正在运行,可以从端口 30081 访问。我可以从 VM 和主机访问它(VM 上的端口转发)。
happy_stallman 无法访问 127.0.0.1:30081 上的存储库,我的连接被拒绝。
有谁知道这是怎么回事?
我想补充一点 happy_stallman 能够访问谷歌以及内联网上的其他应用程序。

最佳答案

默认情况下,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 s 本地地址,而不是主机地址。

要获取容器 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/

关于Docker 容器 localhost 访问问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52602464/

相关文章:

git - 如何制作 git 存储库 'pull only'

c# - Entity Framework 数据读取性能

docker - 使用$()时,Docker rm命令抛出错误

docker - 为什么是 http ://localhost:9021/not opening the Confluent Control Center?

docker - npm install 在 Docker 中不起作用

android - 下载管理器在使用平台默认值的 Android Pie 9.0 NetworkSecurityConfig : No Network Security Config specified, 中不起作用

c# - 应用程序如何在不需要端口转发的情况下连接到服务器?

c - memcpy 和 ntohl 不同的输出

docker - Docker 容器卷在没有运行的情况下如何工作?

Azure Kubernetes 容器环境变量