如何通过名称而不是 IP 地址访问 docker 容器? [英] How to reach docker containers by name instead of IP address?

查看:37
本文介绍了如何通过名称而不是 IP 地址访问 docker 容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用名称而不是 IP 地址访问我的 docker 容器?

Is there a way I can reach my docker containers using names instead of ip addresses?

我听说过 pipework,也看过一些 docker 的 dns 和主机名类型选项,但我仍然无法将所有内容拼凑在一起.

I've heard of pipework and I've seen some dns and hostname type options for docker, but I still am unable to piece everything together.

感谢您的宝贵时间.

我不确定这是否有帮助,但这是我目前所做的:

I'm not sure if this is helpful, but this is what I've done so far:

  • 使用 docker-machine 和 vmwarevsphere 驱动程序安装了 docker 容器主机
  • 使用 docker-compose 启动所有服务
  • 我可以使用 IP 和端口从网络上的任何其他机器访问所有服务

我已将 DNS 别名条目添加到我的专用网络 DNS 服务器,它与 docker-machine 使用的机器名称相匹配.但是机器在启动并连接到网络时总是选择不同的 IP 地址.

I've added a DNS alias entry to my private network DNS server and it matches the machine name that's used by docker-machine. But the machine always picks up a different IP address when it boots and connects to the network.

我只是不知道在哪里解决这个问题:

I'm just lost as to where to tackle this:

  • 网络 DNS 服务器
  • docker-machine 主机名
  • docker 容器主机名
  • 可能是所有这些的某种组合

我可能正在寻找与此问题类似的内容:

I'm probably looking for something similar to this question:

如何让 docker 使用我的网络路由器轻松地将 dhcp ip 分配给容器而不是管道?

任何大方向都会很棒...再次感谢!

Any general direction will be awesome...thanks again!

推荐答案

Docker 1.10 有一个内置的 DNS.如果您的容器连接到同一个用户定义网络(创建一个网络 docker network create my-network 并使用 --net my-network) 他们可以使用容器名称相互引用.(文档).

Docker 1.10 has a built in DNS. If your containers are connected to the same user defined network (create a network docker network create my-network and run your container with --net my-network) they can reference each other using the container name. (Docs).

酷!

一个警告,如果您使用 Docker compose,您知道它会为您的容器名称添加前缀,即 _-#.这会使您的容器名称更难以控制,但对于您的用例来说可能没问题.您可以通过在您的撰写模板中手动设置容器名称来覆盖 docker 撰写命名功能,但是你将无法使用 compose 进行缩放.

One caveat if you are using Docker compose you know that it adds a prefix to your container names, i.e. <project name>_<service name>-#. This makes your container names somewhat more difficult to control, but it might be ok for your use case. You can override the docker compose naming functionality by manually setting the container name in your compose template, but then you wont be able to scale with compose.

这篇关于如何通过名称而不是 IP 地址访问 docker 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆