slysid
slysid

Reputation: 5488

Not able to connect to network inside docker container

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

Upvotes: 11

Views: 20701

Answers (6)

Harvey9111
Harvey9111

Reputation: 26

Also had this issue, for me it was due to running the image in the same context as a local k8 cluster. Disabled k8 to fix.

The way I discovered this was by checking

docker network inspect home

Upvotes: 0

Arthur Kuklenko
Arthur Kuklenko

Reputation: 21

I had the same issue when stop and start container separately. I have just rebuild and re up containers.

docker-compose down

docker-compose build

docker-compose up -d

And then problem gone.

Upvotes: 0

TheMechanic
TheMechanic

Reputation: 949

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>

Upvotes: 3

slysid
slysid

Reputation: 5488

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

Upvotes: 0

Nolwennig
Nolwennig

Reputation: 1684

Restart the Docker daemon on Debian9

service docker restart

and the connections and networks works fine

Upvotes: 2

ji-ruh
ji-ruh

Reputation: 701

Try this:

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

Ref: DOCKER DNS

Upvotes: 0

Related Questions