Docker container not able to ping host

25,989

I had a similar problem, where I wanted to connect from a docker container to its host for accessing a postfix and an apache server.

It helped to add such rules to the iptables of the host:

iptables -A INPUT -i docker0 -p tcp -m tcp --dport 25 -j ACCEPT
iptables -A INPUT -i docker0 -p tcp -m tcp --dport 80 -j ACCEPT

These ports were open before, but only for eth0 interface, not for docker0 ;)

Share:
25,989

Related videos on Youtube

Mirage
Author by

Mirage

Updated on September 18, 2022

Comments

  • Mirage
    Mirage over 1 year

    I am following this tutorial

    https://docs.docker.com/articles/networking/#between-containers

    I have used following commands

    $ sudo brctl addbr bridge0
    $ sudo ip addr add 192.168.0.160/24 dev bridge0
    $ sudo ip link set dev bridge0 up
    
    # Confirming that our bridge is up and running
    
    $ ip addr show bridge0
    4: bridge0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state UP group default
        link/ether 66:38:d0:0d:76:18 brd ff:ff:ff:ff:ff:ff
        inet 192.168.0.160/24 scope global bridge0
           valid_lft forever preferred_lft forever
    
    # Tell Docker about it and restart (on Ubuntu)
    
    $ echo 'DOCKER_OPTS="-b=bridge0"' >> /etc/default/docker
    $ sudo service docker start
    

    It looks to work fine as i can see that my all conatiner are getting ip in that range.

    Now as i have 3 conatiners

    mysql 192.168.0.3
    redis 192.168.0.5
    webserver 192.168.0.6
    

    Now if i ping redis from webserver it works

    but i cma not able to ping the host 192.168.0.160 it says destination host unrecable

    INFO

    vagrant@ubuntu-docker-base:/mydocker/containers$ brctl show
    bridge name bridge id       STP enabled interfaces
    bridge0  8000.7e8945d   no  veth1a8b959
                                veth404a3b2
                                veth7959157
    docker0     8000.5689799    no
    

    container

    root@286329032cd5:/# ip route
    default via 192.168.0.160 dev eth0
    192.168.0.0/24 dev eth0  proto kernel  scope link  src 192.168.0.5
    root@286329032cd5:/# ping 192.168.0.3
    PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
    64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.125 ms
    64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.130 ms
    64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.072 ms
    ^C
    --- 192.168.0.3 ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 1998ms
    rtt min/avg/max/mdev = 0.072/0.109/0.130/0.026 ms
    root@286329032cd5:/# ping 192.168.0.160
    PING 192.168.0.160 (192.168.0.160) 56(84) bytes of data.
    From 192.168.0.5 icmp_seq=1 Destination Host Unreachable
    From 192.168.0.5 icmp_seq=2 Destination Host Unreachable
    From 192.168.0.5 icmp_seq=3 Destination Host Unreachable
    ^C
    --- 192.168.0.160 ping statistics ---
    6 packets transmitted, 0 received, +3 errors, 100% packet loss, time 5032ms
    pipe 3
    root@286329032cd5:/#
    
    • Admin
      Admin over 9 years
      @rjt The host ip is 192.168.0.8 which is docker host, which is connected to my router on eth0
  • rjt
    rjt over 9 years
    What is the output of $ cat /proc/sys/net/ipv4/ip_forward on the host?
  • Mirage
    Mirage over 9 years
    I checked that its 1. i will try your fix when i go home. i was doing remotely and i think i meesed up with eth0 and now i have lost connectivity :)
  • rjt
    rjt over 9 years
    Now that i have reread more of the tutorial, this answer most likely does not apply.