在我的 docker-compose 文件中,需要多个容器来知道特定容器的主机名,包括这个特定容器。
链接不起作用,因为容器无法链接到自身。
基本上,我正在寻找的是一种在 docker-compose 中为 localhost 起别名的方法。
最佳答案
我认为正确答案来自
Aliases can be defined as part of the network declaration for a service. See aliases in Compose file reference for more details on that. – King Chung Huang Apr 24 '17 at 15:18
这是文档中的示例
version: '2'
services:
web:
build: ./web
networks:
- new
worker:
build: ./worker
networks:
- legacy
db:
image: mysql
networks:
new:
aliases:
- database
legacy:
aliases:
- mysql
networks:
new:
legacy:
你可以访问这个docker-compose中的db
,也可以使用mysql
来连接这个db
关于docker - 在 docker-compose 如何创建到 localhost 的别名/链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43579740/