docker tutorials

How could I ping inside the docker container from my host ?

This tutorial guides you on how to ping inside the docker container from host. Let’s use the standard ubuntu container image for our exercise.

Ping inside the docker container from host

First, ensure ubuntu container image is pulled.

$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              adafef2e596e        9 days ago          73.9MB

Now, let’s ping google.com inside the docker container from the host using the following command.

$ docker run --rm ubuntu ping google.com

docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"ping\": executable file not found in $PATH": unknown.

If the above error is observed, it means that ping utility is not installed in the docker container. So we have to installed t using apt-get commands as shown below. First, get to bash of your docker container using docker run -i -t <image> , then run apt-get update as shown below.

$ docker run -i -t ubuntu

root@a565af62d0a6:/# apt-get update

Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [1077 B]
Get:4 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [44.5 kB]
Get:5 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [164 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]        
Get:7 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [33.9 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:10 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [157 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [33.9 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [310 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [4224 B]
Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [3209 B]
Fetched 14.2 MB in 5s (2774 kB/s)
Reading package lists... Done

Then, install iputils-ping utility using the following apt-get command.

root@a565af62d0a6:/# apt-get install iputils-ping

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libcap2 libcap2-bin libpam-cap
The following NEW packages will be installed:
  iputils-ping libcap2 libcap2-bin libpam-cap
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 90.5 kB of archives.
After this operation, 333 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libcap2 amd64 1:2.32-1 [15.9 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 libcap2-bin amd64 1:2.32-1 [26.2 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 iputils-ping amd64 3:20190709-3 [40.1 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal/main amd64 libpam-cap amd64 1:2.32-1 [8352 B]
Fetched 90.5 kB in 1s (76.2 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libcap2:amd64.
(Reading database ... 4122 files and directories currently installed.)
----
----
----
Setting up iputils-ping (3:20190709-3) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...

Once you had installed the ping utility, we can say changes are made to the file system of the docker container and it has to be committed, so that the changes can be persisted to a new image using the following command docker commit <container-id> <new-image-name>  as shown below.

$ docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
49621f91b9e9        ubuntu              "/bin/bash"         2 hours ago         Up 2 hours                              hungry_cartwright

Commit and save the new image “ubuntu_ping

$ docker commit 49621f91b9e9 ubuntu_ping

sha256:42eadb93f08335872397f4213cb64e3f368ecf3071cc13d308122b0f82bbcdf2

Now, after committing the changes to a new image docker images command should show the new image ubuntu_ping in the list as shown below.

$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu_ping         latest              42eadb93f083        7 minutes ago       97.7MB
ubuntu              latest              adafef2e596e        9 days ago          73.9MB

Finally, ping google.com inside the docker container which is run from the new image “ubuntu_ping” with option “–rm” to automatically remove it when it’s done. The ping is not the system ping and the ping is inside the docker container isolated from your host system. And long running commands like this can be closed using “CTRL + C“.

$ docker run --rm ubuntu_ping ping google.com

PING google.com (172.217.194.101) 56(84) bytes of data.
64 bytes from 172.217.194.101: icmp_seq=1 ttl=113 time=0.814 ms
64 bytes from 172.217.194.101: icmp_seq=2 ttl=113 time=0.880 ms
64 bytes from 172.217.194.101: icmp_seq=3 ttl=113 time=0.876 ms
64 bytes from 172.217.194.101: icmp_seq=4 ttl=113 time=0.813 ms
64 bytes from 172.217.194.101: icmp_seq=5 ttl=113 time=0.868 ms
^C
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4077ms
rtt min/avg/max/mdev = 0.813/0.850/0.880/0.030 ms

Hope it helped 🙂

Also See:

References:

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Carl Stermann-Lücke
Carl Stermann-Lücke
3 years ago

On the last step, when I ping google from within the container and stop the pings using Ctrl+C, I don’t get any ping statistics. It just brings me back straight to the command prompt: PS C:\develop\git\minimal\minimal> docker run –rm ubuntu_ping ping google.com PING google.com (142.250.74.206) 56(84) bytes of data. 64 bytes from 142.250.74.206 (142.250.74.206): icmp_seq=1 ttl=37 time=12.4 ms 64 bytes from 142.250.74.206 (142.250.74.206): icmp_seq=2 ttl=37 time=14.2 ms 64 bytes from 142.250.74.206 (142.250.74.206): icmp_seq=3 ttl=37 time=13.5 ms 64 bytes from 142.250.74.206 (142.250.74.206): icmp_seq=4 ttl=37 time=13.5 ms 64 bytes from 142.250.74.206 (142.250.74.206): icmp_seq=5 ttl=37 time=13.4 ms 64 bytes from 142.250.74.206 (142.250.74.206): icmp_seq=6… Read more »