Docker - Ubuntu - bash:ping:未找到命令[已关闭]

cdmah0mi  于 2022-11-28  发布在  Docker
关注(0)|答案(8)|浏览(188)

此问题似乎与a specific programming problem, a software algorithm, or software tools primarily used by programmers无关。如果您认为此问题与another Stack Exchange site相关,您可以留下评论,说明在何处可以找到此问题的答案。
6天前关闭。
机构群体在6天前审核了是否重新讨论此问题,并将其关闭:
原始关闭原因未解决
Improve this question
我有一个运行Ubuntu的Docker容器,我做了如下操作:

docker run -it ubuntu /bin/bash

但是它似乎没有ping

bash: ping: command not found

我需要安装吗?
似乎缺少了一个非常基本的命令。我尝试了whereis ping,它没有报告任何内容。

sgtfey8w

sgtfey8w1#

Docker镜像非常少,但是你可以通过以下方式在你的官方ubuntu docker镜像中安装ping

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

很有可能你的图像不需要ping,而只是想用它来测试。
但是如果你需要ping存在于你的映像上,你可以创建一个Dockerfilecommit容器你运行上面的命令到一个新的映像中。
确认:

docker commit -m "Installed iputils-ping" --author "Your Name <name@domain.com>" ContainerNameOrId yourrepository/imagename:tag

停靠文件:

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

请注意,创建Docker映像时有一些最佳实践,例如清除apt缓存文件等。

n7taea2i

n7taea2i2#

This是Ubuntu的Docker Hub页面,this是它的创建方式。它只安装了(有点)最小的包,因此如果你需要任何额外的东西,你需要自己安装。

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

然而,通常您会创建一个“Dockerfile”并构建它:

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

请使用Google查找教程并浏览现有的Dockerfiles,以了解它们通常是如何工作的:)例如,应该通过在apt-get install命令后运行apt-get clean && rm -rf /var/lib/apt/lists/*来最小化图像大小。

0lvr5msh

0lvr5msh3#

或者,您可以使用已安装ping的Docker映像,例如busybox

docker run --rm busybox ping SERVER_NAME -c 2
6yoyoihd

6yoyoihd4#

一般来说,人们会拉Ubuntu/CentOS的官方图片,但他们没有意识到这些图片是最小的,没有任何东西在上面。
对于Ubuntu,此映像是从Canonical提供的官方rootfs tarball构建的。由于它是Ubuntu的最小安装,因此默认情况下此映像仅包含C、C.UTF-8和POSIX语言环境。
可以在容器上安装net-tools(包括ifconfig、netstat)、ip-utils(包括ping)和其他类似curl的工具,并可以从容器创建映像,也可以编写Dockerfile,在创建映像时安装这些工具。
下面是Dockerfile示例,在创建映像时,它将包括这些工具:

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

或者从基本映像启动container并在container上安装这些实用程序,然后提交到映像。docker commit -m“任何描述性消息”container_id image_name:lattest
该映像将安装所有内容。

p1iqtdky

p1iqtdky5#

有时,Docker中Linux的最小安装不会定义路径,因此有必要使用...调用ping。

cd /usr/sbin
ping <ip>
qv7cva1a

qv7cva1a6#

我在debian 10上使用了下面的语句。

apt-get install iputils-ping
y53ybaqx

y53ybaqx7#

每次你遇到这种错误

bash: <command>: command not found
dpkg -S $(which <command>)
  • 没有安装该软件包的主机?Try this
apt-file search /bin/<command>
omhiaaxx

omhiaaxx8#

或者,您可以在进入容器的网络名称空间后在主机上运行ping
首先,在主机上找到容器的进程ID(这可以是shell或在容器中运行的应用程序),然后更改为容器的网络名称空间(在主机上以root运行):

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

修改PS1环境变量仅用于在容器的网络名称空间中显示不同的提示。
现在,您可以使用pingnetstatifconfigip等,前提是它们已安装在主机上。

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

请记住,这只会更改网络命名空间,装载命名空间(文件系统)并未更改,因此名称解析可能无法正常工作(它仍在使用主机上的/etc/hosts文件)

相关问题