【问题标题】:From inside one Docker container, how to connect to the port of another Docker container on the same Docker host?从一个 Docker 容器内部,如何连接到同一 Docker 主机上另一个 Docker 容器的端口?
【发布时间】:2020-12-28 22:50:19
【问题描述】:

在 Ubuntu 20.04 上运行新的 Docker 20.10.1。此版本包括support for host.docker.internal in dockerd on Linux

root@docker04:~# docker --version
Docker version 20.10.1, build 831ebea

我在使用 SSL 证书的容器中运行一堆服务,我想使用 Telegraf 检查证书是否过期(以便稍后发送到 Grafana 仪表板)。

所有带有 Web 界面和 Telegraf 的服务都运行在同一个 Docker 主机上。所有服务都在自己的桥接网络中。我想在端口 9000 上连接 Telegraf Portainer(见下文)。

----------VM running docker (10.10.1.32)----------
|                                                |
|    --------------------                        |
|    | Gitea            |                        |     
|    | Ports: 3000:3000 |                        |
|    | Network: gitea   |                        |
|    --------------------                        |
|                                                |
|    ----------------------                      |
|    | Portainer          |                      |
|    | Ports: 9000:9000   |<--|                  |
|    | Network: portainer |   |                  |
|    ----------------------   |                  |
|                             |                  |
|    ---------------------    |                  |
|    | Telegraf          |    |                  |
|    | Network: telegraf |-->-                   |
|    |                   |                       |
|    ---------------------                       |
|                                                |
--------------------------------------------------

我认为有了对 host.docker.internal 的新支持,我将能够从一个容器连接到另一个容器,但这似乎不起作用。 Ping 可以,但我无法连接到端口 9000。

root@docker04:~# docker run -it --add-host host.docker.internal:host-gateway alpine /bin/sh
/ # ping host.docker.internal
PING host.docker.internal (172.17.0.1): 56 data bytes
64 bytes from 172.17.0.1: seq=0 ttl=64 time=0.102 ms
64 bytes from 172.17.0.1: seq=1 ttl=64 time=0.100 ms
^C
/ # telnet host.docker.internal 9000
telnet: can't connect to remote host (172.17.0.1): Operation timed out

最后一个 telnet 命令挂起并最终断开连接。我在这里错过了什么吗?这仅适用于主机网络吗?我显然在这里误解了关于 Docker 网络的一些东西......

如果我也尝试使用主机的 IP,它也会失败。

/ # telnet 10.10.1.32 9000
telnet: can't connect to remote host (10.10.1.32): Operation timed out

【问题讨论】:

    标签: docker docker-networking


    【解决方案1】:

    尝试在同一网络上的所有容器上运行,或者在一个容器上映射两个网络,例如在具有 network1、network2 的 1 个容器上,为什么您会遇到这个问题,因为您为每个容器使用不同的私有桥接网络。

    【讨论】:

      猜你喜欢
      • 2019-02-24
      • 1970-01-01
      • 2015-01-07
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 2019-09-04
      相关资源
      最近更新 更多