Not able to connect to network inside docker container

孤街醉人 提交于 2021-02-19 01:09:05

问题


I have a CentOS 7 host on which I am running Docker. When I do a ping from my host to 8.8.8.8, ping was successful whereas same inside a docker container is not working.

From Host

[root@linux1 ~]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=47 time=31.5 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=47 time=31.6 ms
^C
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 31.592/31.617/31.643/0.179 ms

From Docker Container (I am using basic ubuntu image):

[root@linux1 ~]# docker run ubuntu ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
From 172.17.0.1 icmp_seq=1 Destination Host Unreachable
From 172.17.0.1 icmp_seq=2 Destination Host Unreachable
From 172.17.0.1 icmp_seq=3 Destination Host Unreachable
From 172.17.0.1 icmp_seq=4 Destination Host Unreachable
^C
--- 8.8.8.8 ping statistics ---
6 packets transmitted, 0 received, +4 errors, 100% packet loss, time 5000ms
pipe 4

Any suggestions would be helpful. Thanks


回答1:


Restart the Docker daemon on Debian9

service docker restart

and the connections and networks works fine




回答2:


Try this:

docker run --dns=8.8.8.8 -it ubuntu ping 8.8.8.8

Ref: DOCKER DNS




回答3:


I figured out the issue. It is not an issue with the DNS but an issue with the network connection itself inside Docker containers. Drilled down the issue is the default IP assigned to docker0 interface, which conflicted with my network address. Forced docker daemon to assign an IP so that it won't conflict and my issue is resolved.

Thanks




回答4:


Recently I faced a similar network issue. The other answers here didn't help: DNS was working fine and restarting Docker wouldn't change a thing. I've found that specifying the network as host solved it.

There are three ways of doing it:

In docker-compose:

By setting network_mode in the yaml file:

services:
  worker:
    build: .
    network_mode: host

In the image building stage for RUN commands:

docker build --network=host

In the execution stage for the application:

docker run --network=host <image>



来源:https://stackoverflow.com/questions/33780947/not-able-to-connect-to-network-inside-docker-container

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!