关于宿主机功能正常docker容器重启后dns失效的解决办法
问题描述:centos8中创建docker后运行正常,也能ping通外网域名。但是容器重启后dns失效,但能ping通ip。网上有很多办法,都很麻烦,而且不见得有效。如果说安全级别不高,只是部署环境我找到了一个快速解决的办法。
在生成容器的时候,直接将宿主机的/etc映射到容器,比如:
docker run -itd -e “container=docker” --privileged=true -v /sys/fs/cgroup:/sys/fs/cgroup -v /wutool:/wutool -v /mnt:/mnt -v /etc:/etc --net mywifi --ip 192.168.0.188 --name test01 centos /usr/sbin/init
这样重启后,dns保持有效。下面是运行情况
[root@8dba1adca160 /]# exit
exit
[root@wuzz etc]# docker exec -it test01 bash
[root@8dba1adca160 /]# ping www.baidu.com -c 2
PING www.wshifen.com (103.235.46.96) 56(84) bytes of data.
64 bytes from 103.235.46.96 (103.235.46.96): icmp_seq=2 ttl=47 time=250 ms
--- www.wshifen.com ping statistics ---
2 packets transmitted, 1 received, 50% packet loss, time 1051ms
rtt min/avg/max/mdev = 249.957/249.957/249.957/0.000 ms
[root@8dba1adca160 /]# exit
exit
[root@wuzz etc]# docker exec -it test01 bash
[root@8dba1adca160 /]# ping www.baidu.com -c 2
PING www.a.shifen.com (103.235.46.96) 56(84) bytes of data.
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1031ms
[root@8dba1adca160 /]# ping www.baidu.com -c 2
PING www.wshifen.com (103.235.47.188) 56(84) bytes of data.
--- www.wshifen.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1046ms
[root@8dba1adca160 /]# ping www.sina.com -c 2
PING ww1.sinaimg.cn.w.alikunlun.com (219.153.159.211) 56(84) bytes of data.
64 bytes from 219.153.159.211 (219.153.159.211): icmp_seq=1 ttl=56 time=5.10 ms
64 bytes from 219.153.159.211 (219.153.159.211): icmp_seq=2 ttl=56 time=5.45 ms