我创建了两个docker镜像,并使用docker compose启动了它们。
docker-compose.yaml
version: '2'
services:
dmgr:
hostname: dmgr
image: dmgr
build:
context: ./dmgr
ports:
- "9060:9060"
tty: true
custom1:
hostname: node
image: node
build:
context: ./custom
ports:
- "9080:9080"
environment:
- PROFILE_NAME=Node
- HOST_NAME=node
- NODE_NAME=Node
- DMGR_HOST=dmgr
- DMGR_PORT=8879
depends_on:
- dmgr
两个容器启动后,第二个容器“节点”可以解析并ping容器“ dmgr”,但其他方法不起作用
root@node:/# nslookup dmgr
Server: 127.0.0.11
Address: 127.0.0.11#53
Non-authoritative answer:
Name: dmgr
Address: 172.18.0.2
root@node:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.3 node
root@node:/# cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0
如果我执行到容器“ dmgr”并运行“ nslookup节点”,则无法解析。我究竟做错了什么?请有人指出我正确的方向
root@dmgr:/# nslookup node
Server: 127.0.0.11
Address: 127.0.0.11#53
** server can't find node: NXDOMAIN
答案 0 :(得分:0)
主机名将无法使用docker的嵌入式DNS进行解析(这是违反直觉的,但是可以在docker的控制范围之外进行更改,因此有些逻辑)。解决的是容器名称和服务名称custom1。更改服务名称将解决您的问题:
version: '2'
services:
dmgr:
hostname: dmgr
image: dmgr
build:
context: ./dmgr
ports:
- "9060:9060"
tty: true
node:
image: node
build:
context: ./custom
ports:
- "9080:9080"
environment:
- PROFILE_NAME=Node
- HOST_NAME=node
- NODE_NAME=Node
- DMGR_HOST=dmgr
- DMGR_PORT=8879
depends_on:
- dmgr
如果您需要解决其他问题,也可以定义网络别名:
version: '2'
services:
dmgr:
hostname: dmgr
image: dmgr
build:
context: ./dmgr
ports:
- "9060:9060"
tty: true
custom1:
image: node
build:
context: ./custom
ports:
- "9080:9080"
networks:
default:
aliases:
- node
environment:
- PROFILE_NAME=Node
- HOST_NAME=node
- NODE_NAME=Node
- DMGR_HOST=dmgr
- DMGR_PORT=8879
depends_on:
- dmgr