我在docker-compose.yml
中声明我的服务:
wildfly:
image: "myrepo/wildfly:latest"
hostname: wildfly
因此,compose将条目添加到/ etc / hosts:
172.18.0.4 wildfly
docker自动生成172.18.0.4
。
但我需要将some.host.name
绑定到/etc/hosts
中生成的IP,以便在/ etc / hosts中有多个条目:
172.18.0.4 wildfly
172.18.0.4 some.host.name
我该怎么做?
我尝试使用extra_hosts
语法,但在docker-compose.yml
我不知道将为容器分配什么IP。
有人遇到过类似的问题吗?
答案 0 :(得分:3)
你应该为支持别名的compose版本'3'格式。由于您已使用支持的泊坞窗版本
version: '3'
services:
wildfly:
image: "myrepo/wildfly:latest"
hostname: wildfly
networks:
default:
aliases:
- some.host.name
这不会添加主机条目,因为docker现在使用自己的内部DNS服务器。如果您需要使用多个撰写文件才能访问这些服务。然后你应该创建一个外部网络
docker network create commonnet
然后在你的作品中使用以下
version: '3'
services:
wildfly:
image: "myrepo/wildfly:latest"
hostname: wildfly
networks:
commonnet:
aliases:
- some.host.name
networks:
commonnet:
external: true