【问题标题】:Docker container not accessible through localhost, but accessible through 127.0.0.1Docker 容器无法通过 localhost 访问,但可以通过 127.0.0.1 访问
【发布时间】:2019-07-18 08:54:25
【问题描述】:

问题

我有一个暴露给 *:8080 的 Docker 服务容器。

  • 我无法通过localhost:8080 访问容器。 Chrome / curl 无限期挂断。
  • 但是如果我使用任何其他本地IP,例如127.0.0.1,我可以访问容器

这让我大吃一惊,因为在我的 hosts 文件中,localhost 重定向到 127.0.0.1

为什么会这样?和 IPv4/IPv6 双栈有什么关系吗?

环境

我使用的是启用了 Docker Swarm 的 PopOS(基于 Ubuntu)。 我正在使用这个测试堆栈文件,traefik.docker-compose.yml:

version: '3'
services:
  reverse-proxy:
    image: traefik # The official Traefik docker image
    command: --api --docker # Enables the web UI and tells Traefik to listen to docker
    ports:
      - "80:80"     # The HTTP port
      - "8080:8080" # The Web UI (enabled by --api)
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events

要运行堆栈,我使用:

docker stack deploy -c traefik.docker-compose.yml traefik

一旦服务启动,我通过2种方式确认它正在收听8080

  1. docker service ls
ID                  NAME                    MODE                REPLICAS            IMAGE               PORTS
4ejfsvenij3p        traefik_reverse-proxy   replicated          1/1                 traefik:latest      *:80->80/tcp, *:8080->8080/tcp
  1. sudo ss -pnlt | grep 8080
LISTEN   3         128                       *:8080                   *:*        users:(("dockerd",pid=2119,fd=45))

供参考,这里是我/etc/hosts的内容:

127.0.0.1   localhost
::1     localhost ipv6-localhost
127.0.1.1   pop-os.localdomain  pop-os pop1810x220

我只是使用curl 进行测试:

  • 作品:curl http://127.0.0.1:8080
  • 挂断:curl http://localhost:8080

【问题讨论】:

  • curl 'http://[::1]:8080'localhost 的 IPv6 版本)怎么样?
  • 没有骰子,http://[::1]:8080/ 也挂了
  • 这实际上是我希望的——它告诉我们localhost 有什么问题。请记住,/etc/hosts 并不是名称解析中涉及的唯一部分——您需要评估系统的 NSS 模块以了解所有组件在起作用,因此您不能相信 /etc/hosts 会覆盖其他所有内容无需调查您的发行版(或您的系统管理员,如果在本地被覆盖)是如何设置名称解析的。
  • 这为调查开辟了一条新途径,我将对此进行更多挖掘,谢谢!
  • 您找到解决方案了吗?

标签: docker networking ipv6 docker-swarm ipv4


【解决方案1】:

尝试在正在运行的容器中编辑 /etc/hosts 以删除第二行中的第二个 'localhost' 引用,并在 '::1' 之后跟随几个空格,使其显示为:

::1 ipv6-localhost

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 2018-09-07
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    相关资源
    最近更新 更多