在 docker-compose 中如何创建别名/链接到本地​​主机? [英] In docker-compose how to create an alias / link to localhost?

查看:68
本文介绍了在 docker-compose 中如何创建别名/链接到本地​​主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 docker-compose 文件中,需要多个容器知道特定容器的主机名,包括这个特定容器.

In my docker-compose file there is a need for several containers to know the hostname of a specific container, including this specific container.

链接将不起作用,因为容器无法链接到自身.

Links will not work, since a container can not link to itself.

基本上,我正在寻找一种在 docker-compose 中别名 localhost 的方法.

Basically, what I am looking for is a way to alias localhost in docker-compose.

推荐答案

您应该避免使用链接.相反,同一 Docker 网络上的服务可以通过使用服务名称作为 DNS 名称来找到彼此.使用它来引用您描述的特定容器,包括它何时引用自身.

You should avoid using links. Instead, services on the same Docker network can find each other by using service names as DNS names. Use that to reference the specific container you described, including when it references itself.

例如,在以下组成的 Docker Compose 文件中,如果 someservice 是在端口 80 上提供服务的 Web 服务器,则 anotherservice 服务将能够在以下位置连接到它http://someservice/,因为它们位于公共网络 the_net 上.

For example, in the following made up Docker Compose file, if someservice was a web server serving on port 80, anotherservice service would be able to connect to it at http://someservice/, because they're on a common network the_net.

version: '3'

services:
  someservice:
    image: someserviceimage
    networks:
      - the_net

  anotherservice:
    image: anotherserviceimage
    networks:
      - the_net

networks:
  the_net:

someservice 也可以通过 http://someservice/ 访问自身.

someservice can also reach itself at http://someservice/.

这篇关于在 docker-compose 中如何创建别名/链接到本地​​主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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