在我的 docker-compose 文件中,需要多个容器知道特定容器的主机名,包括这个特定容器。
链接将不起作用,因为容器无法链接到自身。
基本上,我正在寻找的是一种在 docker-compose 中别名 localhost 的方法。
Best Answer-推荐答案
您应该避免使用链接。相反,同一 Docker 网络上的服务可以通过使用服务名称作为 DNS 名称来找到彼此。使用它来引用您描述的特定容器,包括它何时引用自身。
例如,在下面组成的Docker Compose文件中,如果someservice 是在端口 80 上服务的 Web 服务器,anotherservice 服务将能够通过 http://someservice/ 连接到它,因为它们在一个公共(public)网络上 the_net .
version: '3'
services:
someservice:
image: someserviceimage
networks:
- the_net
anotherservice:
image: anotherserviceimage
networks:
- the_net
networks:
the_net:
someservice 也可以通过 http://someservice/ 联系自己.
关于docker - 在 docker-compose 中如何创建别名/链接到本地主机?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/43579740/
|