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

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

问题描述

在我的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服务器,另一个服务服务将可以通过 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:

一些服务也可以通过 http:// someservice /

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

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