容器的DNS和主机名

2020-08-23  本文已影响0人  欧耶90

容器启动时会有3个和DNS、主机名相关的文件会被覆盖掉,/etc/hosts、/etc/hostname、/etc/resolv.conf

  1. 容器内三个文件的挂载信息
root@ff3b6822a0f4:/data# mount
...
dev/sda5 on /etc/resolv.conf type ext4 (rw,relatime,errors=remount-ro)
/dev/sda5 on /etc/hostname type ext4 (rw,relatime,errors=remount-ro)
/dev/sda5 on /etc/hosts type ext4 (rw,relatime,errors=remount-ro)
...

Docker容器启动时会从宿主机复制/etc/resolv.conf文件到容器,并删除到无法连接的DNS服务器
/etc/hosts文件中只记录容器自身的主机名和ip:

root@ff3b6822a0f4:/data# 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.17.0.2  ff3b6822a0f4

/etc/hostname文件记录容器的主机名:

root@ff3b6822a0f4:/data# cat /etc/hostname
ff3b6822a0f4
  1. 通过指定参数修改dns等配置
➜  ~ docker run --name test-nginx -h nginx01 --dns=8.8.8.8 nginx:latest -d
➜  ~ docker exec -it test-nginx bash
root@nginx01:/# cat /etc/resolv.conf 
search localdomain
nameserver 8.8.8.8
root@nginx01:/# hostname
nginx01
上一篇 下一篇

猜你喜欢

热点阅读