When I use Docker, I create container from image (Ex: Ubuntu:14.04) and ssh to this container by command:
docker run -it <image_id> bash
When I ssh to container, I used command ping google.com
but I get error: ping: unknown host google.com
To fix it, I have 2 solutions, but first, you must get DNS of your machine by command
nm-tool
Result in my machine:
DNS: 192.168.30.29
DNS: 192.168.30.66=> I’ll use DNS: 192.168.30.29
1. Solution 1
– Go to file sudo nano /etc/docker/daemon.json and add your DNS to this file
{ "dns": ["192.168.30.29", "8.8.8.8", "8.8.4.4"] }
Save it and restart docker by command: sudo service docker restart
2. Solution 2
solution 1- Go to file sudo nano /etc/default/docker, uncomment and add your DNS to this row:
DOCKER_OPTS="--dns 192.168.30.29 --dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true"
Save it and restart docker by command: sudo service docker restart
If when restart, you get error:
docker stop/waiting start: Job failed to start
or error:
stop: Unknown instance: start: Job failed to start
– You shoud go to file sudo nano /etc/default/docker, and comment row
DOCKER_OPTS="--dns 192.168.30.29 --dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true"
and try again with solution 1.
==> After you try adding DNS successfully, you can ssh to container and run
ping google.com once again. And this is my result after try fixing with solution 1
root@2beb109bc7b3:~# ping google.com PING google.com (113.171.239.123) 56(84) bytes of data. 64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=1 ttl=56 time=1.06 ms 64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=2 ttl=56 time=1.51 ms 64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=3 ttl=56 time=2.32 ms 64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=4 ttl=56 time=1.19 ms 64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=5 ttl=56 time=1.55 ms .....
That’s all. Thank you for reading. 🙂