Docker - Ubuntu - bash: ping: command not found
Asked Answered
B

8

717

I've got a Docker container running Ubuntu which I did as follows:

docker run -it ubuntu /bin/bash

however it doesn't seem to have ping. E.g.

bash: ping: command not found

Do I need to install that?

Seems a pretty basic command to be missing. I tried whereis ping which doesn't report anything.

Bewick answered 6/10, 2016 at 16:33 Comment(3)
It's entirely appropriate for a Docker image to be minimal. In most cases, a container will never do anything but run a single application -- why install anything that application doesn't need? – Eye
This is not exclusive to docker images, the same issue is present in Ubuntu 22.X minimal server installer images (and maybe other versions too). I mean, who needs IP tools on their servers? πŸ˜‚ – Wearisome
my 5c: unless you really need ping specifically (e.g. to measure latency precisely), use wget instead (which is provided via /usr/bin/wget in stock Ubuntu images routinely). time wget is not that precise, but if you're in a rootless docker, it's IMVHO the simplest and fastest way to check connectivity, DNS resolution and rough speeds. – Bacchus
E
1382

Docker images are pretty minimal, but you can install ping in your official ubuntu docker image via:

apt-get update -y
apt-get install -y iputils-ping

Chances are you don't need ping on your image, and just want to use it for testing purposes. Above example will help you out.

But if you need ping to exist on your image, you can create a Dockerfile or commit the container you ran the above commands into a new image.

Commit:

docker commit -m "Installed iputils-ping" --author "Your Name <[email protected]>" ContainerNameOrId yourrepository/imagename:tag

Dockerfile:

FROM ubuntu
RUN apt-get update && apt-get install -y iputils-ping
CMD bash

Please note there are best practices on creating docker images, like clearing apt cache files afterwards and etc.

Extraction answered 6/10, 2016 at 16:41 Comment(6)
apt-get fails with Temporary failure resolving 'security.ubuntu.com' obviously because networking is not present. – Discommon
I'd got into the habit of using apt install and ended up with "package not found", but as the answer says, apt-get works just fine. – Hercules
If you need to install on a container running you need to execute with root privileges, so execute docker exec -u 0 -it <container> /bin/bash. Where -u 0 is user root. – Boomer
debian11: E: Unable to locate package iputils-ping :/ – Jermainejerman
@Jermainejerman i just tried it on debian:11.5 image and it worked. did you do apt update before hand? – Extraction
Yeah, great idea. Except when you are trying to debug why apt/apt-get is failing... – Knowable
E
82

This is the Docker Hub page for Ubuntu and this is how it is created. It only has (somewhat) bare minimum packages installed, thus if you need anything extra you need to install it yourself.

apt-get update && apt-get install -y iputils-ping

However usually you'd create a "Dockerfile" and build it:

mkdir ubuntu_with_ping
cat >ubuntu_with_ping/Dockerfile <<'EOF'
FROM ubuntu
RUN apt-get update && apt-get install -y iputils-ping
CMD bash
EOF
docker build -t ubuntu_with_ping ubuntu_with_ping
docker run -it ubuntu_with_ping

Please use Google to find tutorials and browse existing Dockerfiles to see how they usually do things :) For example image size should be minimized by running apt-get clean && rm -rf /var/lib/apt/lists/* after apt-get install commands.

Emplacement answered 6/10, 2016 at 16:41 Comment(4)
echo -e actually defies the POSIX sh standard, which doesn't allow it to do anything but print -e on its output. (Even with some versions of bash, that's the default behavior). Use printf instead: printf '%s\n' "FROM ubuntu" "RUN apt-get update && apt-get install -y iputils-ping" "CMD bash", and see the APPLICATION USAGE section of the above-linked standards document. – Eye
Even bash won't support echo -e the way you expect it to (but instead will have a standards-compliant behavior) when in POSIX mode compiled with --enable-xpg-echo-default, or with appropriate environment variables or other runtime configuration. – Eye
(POSIX allows echo to behave in an implementation-defined way when given -n as a first argument, or when any backslash literals are present -- but even then, it's implementation-defined, not standard-guaranteed, so behavior is dependent on the individual shell in use). – Eye
Thanks for comments and improvements, the "copy-and-paste" friendly example was more of an after thought. – Emplacement
R
23

Alternatively you can use a Docker image which already has ping installed, e.g. busybox:

docker run --rm busybox ping SERVER_NAME -c 2
Ryannryazan answered 13/9, 2018 at 11:18 Comment(1)
It's a solution, but creating an image just to execute ping seems overkill for me. I'd rather apt-get iputils-ping on the image which needs it. – Ecumenicalism
Y
13

Generally people pull the official image of Ubuntu/CentOS but they don't realize that these images are minimal and doesn't have any thing on the top of that.

For Ubuntu, this image is built from official rootfs tarballs provided by Canonical. Given that it is a minimal install of Ubuntu, this image only includes the C, C.UTF-8, and POSIX locales by default.

One can install net-tools (includes ifconfig, netstat), ip-utils(includes ping) andy other likes curl etc on container and can create image from container or can write Dockerfile that will install these tool while creating image.

Below is Dockerfile example, while creating image from this it will include these tools:

FROM vkitpro/ubuntu16.04
RUN     apt-get  update -y \
&& apt-get upgrade -y \
&& apt-get install iputils-ping -y \
&& apt-get install net-tools -y \
CMD bash

or launch container from base image and install these utilities on container and then commit to image. docker commit -m "any descriptive message" container_id image_name:lattest

That image will have all thing installed.

Yearly answered 21/12, 2018 at 5:44 Comment(0)
O
4

Sometimes, the minimal installation of Linux in Docker doesn't define the path and therefore it is necessary to call ping using ....

cd /usr/sbin
ping <ip>
Oof answered 11/9, 2019 at 17:22 Comment(0)
P
4

I have used the statement below on debian 10

apt-get install iputils-ping
Podite answered 23/3, 2020 at 8:9 Comment(0)
C
2

Every time you get this kind of error

bash: <command>: command not found
  • On a host with that command already working with this solution:

    dpkg -S $(which <command>)
    
  • Don't have a host with that package installed? Try this:

    apt-file search /bin/<command>
    
Candor answered 31/1, 2019 at 3:1 Comment(0)
D
1

Alternatively, you can run ping on the host after entering the container's network namespace.

First, find the container's process ID on the host (this can be the shell or the application you are running inside the container). Then change to the container's network namespace (run as root on the host):

host# PS1='container# ' nsenter -t <PID> -n

Modifying the PS1 environment variable is only used to show a different prompt while in the container's network namespace.

Now you can use ping, netstat, ifconfig, ip, etc, given that they are installed on the host.

container# ping <IP>
container# ip route get <IP>
....
container# exit

Keep in mind that this only changes the network namespace, the mount namespace (file system) has not changed and therefore name resolving may not work correctly (it still uses the /etc/hosts file on the host)

Draftsman answered 18/7, 2022 at 18:37 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.