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

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

问题描述

有没有一种方法可以使用名称而不是IP地址来访问我的docker容器?

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

我听说过管道,并且看到了一些dns和主机名键入docker的选项,但我仍然无法将所有内容拼凑起来。

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安装了Docker容器主机-machine和vmwarevsphere驱动程序

  • 使用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容器主机名

  • 可能是所有这些的某种组合

  • network DNS server
  • docker-machine hostname
  • docker container hostname
  • probably some combination of all of them

我可能正在寻找与这个问题:

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网络创建my-network 并使用<$ c $运行容器c>-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天全站免登陆