使用主机名而不是 localhost 访问 docker 容器

Omt*_*guy 5 docker docker-compose

我正在使用 docker-compose 和 docker。一切都很好。但是从主机访问服务的唯一方法是通过http://localhost:port。我希望能够使用容器名称进行访问,例如:http://elastic:9020

It works well inside the docker. if one container needs to access another i can use the http://elastic:9020 but not from the Host. Of Course that i am exposing the ports and using port mapping and it works using localhost as i said. Docker

UPDATE: I was looking for a native solution without the needs of installing DNS Server or updating the .host file.

Yas*_*sen 3

有一个旨在解析 Docker 主机名的 OSS 项目:

\n

DPS :: DNS 代理服务器

\n

DPS 是一个最终用户(开发人员、服务器管理员)DNS 服务器工具,用于使用 docker 开发系统并解决 docker 容器主机名问题。

\n

Github 仓库:mageddo/dns-proxy-server

\n

主要特点:

\n
\n
    \n
  • 从本地配置数据库解析主机名
  • \n
  • 使用 docker hostname选项或HOSTNAMES解析 docker 容器中的主机名 env解析 docker 容器中的主机名
  • \n
  • 如果以上两个都没有答案,则从配置的 DNS 服务器(作为代理)列表中解析主机名
  • \n
  • 使用通配符解决主机名问题
  • \n
  • 用于创建/列出/更新/删除A/CNAME 的图形界面记录的
  • \n
  • 使用解决主机IPhost.docker使用主机名
  • \n
  • 通过it\xe2\x80\x99s容器名/服务名访问容器
  • \n
  • 指定从哪个网络解析容器IP
  • \n
\n
\n

用法:

\n

docker run --rm --hostname dns.mageddo \\ -v /var/run/docker.sock:/var/run/docker.sock \\ -v /etc/resolv.conf:/etc/resolv.conf \\ defreitas/dns-proxy-server

\n