Baeldung Pro – Ops – NPI EA (cat = Baeldung on Ops)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

When using Docker containers, we may get into circumstances where standard command-line tools like ping are not present in the official Docker images.

In this tutorial, we’ll understand why these utilities are not present and how to resolve it by installing the utility manually.

2. Why Is ping Missing?

The official Ubuntu Docker image is intentionally minimal to reduce size and overhead. By default, it excludes tools like ping, curl, and wget and only includes essential packages. This eventually lowers the attack surface, which results in fewer Docker image vulnerabilities.

This design forces developers to consciously choose their dependencies, leading to smaller image sizes and faster deployments.

3. Installing ping in the Container

In order to use ping, we need to install the iputils-ping package. Let’s first update the package repository cache:

$ apt-get update

Furthermore, we’ll install the iputils-ping package using the below installation command:

$ apt-get install -y iputils-ping

Finally, let’s test the ping using the Google DNS:

$ ping google.com
PING google.com (142.250.207.206): 56 data bytes
64 bytes from 142.250.207.206: icmp_seq=0 ttl=59 time=32.040 ms
64 bytes from 142.250.207.206: icmp_seq=1 ttl=59 time=30.167 ms
64 bytes from 142.250.207.206: icmp_seq=2 ttl=59 time=39.278 ms

4. Making It Persistent: Create a Custom Image

Installing ping manually works for temporary containers, but to avoid repeating this step, we can build a custom Docker image with ping pre-installed.

4.1. Create a Dockerfile

Let’s create a Dockerfile to install the ping command using the Ubuntu base image:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y iputils-ping
CMD ["/bin/bash"]

The above Dockerfile creates a custom Ubuntu-based image using the minimal ubuntu:latest base image. It updates the package repositories and installs the iputils-ping package to enable the ping command. Finally, we set /bin/bash as the default startup command.

4.2. Build the Image

Using the above Dockerfile, let’s create a Docker image:

$ docker build -t ubuntu-with-ping .

The above command will create a Docker image with the tag ubuntu-with-ping. This Docker image will contain the ping utility.

4.3. Verify the ping Command

In order to verify the ping command, we first need to run the Docker container using the Docker image ubuntu-with-ping:

$ docker run -itd ubuntu-with-ping --name ubuntu-with-ping-container

This will create a new Docker container. Exec into the container and run the ping command using the Google DNS:

$ docker exec -it ubuntu-with-ping-container bash
$ ping google.com PING google.com (142.250.207.206): 56 data bytes
64 bytes from 142.250.207.206: icmp_seq=0 ttl=59 time=32.040 ms

At last, the output of the ping command confirms that the Docker container has the ping utility installed.

5. Conclusion

In this article, we learned how to address the absence of the ping command in a Ubuntu Docker image. By understanding Docker’s emphasis on lightweight, purpose-built containers, we recognized why common utilities like ping are excluded by default. Further, we resolved the error by updating package lists and installing the iputils-ping package.

Finally, we created a custom Dockerfile to demonstrate how to extend the base image and include essential packages as per our requirements.