docker run -it ubuntu /bin/bash
然而,它似乎没有ping
。例如:
bash: ping: command not found
我需要安装那个吗?
这似乎是一个非常基本的命令,但却不见了。我尝试了whereis ping
,但没有任何报告。
docker run -it ubuntu /bin/bash
然而,它似乎没有ping
。例如:
bash: ping: command not found
我需要安装那个吗?
这似乎是一个非常基本的命令,但却不见了。我尝试了whereis ping
,但没有任何报告。
Docker镜像相当精简,但是您可以通过以下方式在官方Ubuntu Docker镜像中安装ping
:
apt-get update -y
apt-get install -y iputils-ping
很可能您不需要在图像上使用 ping
命令,只是想用它进行测试。上面的示例将帮助您。
但如果您需要在您的图像上存在 ping
命令,则可以创建一个 Dockerfile
或将运行上述命令的容器 commit
到新的镜像中。
Commit:
docker commit -m "Installed iputils-ping" --author "Your Name <name@domain.com>" ContainerNameOrId yourrepository/imagename:tag
Dockerfile:
FROM ubuntu
RUN apt-get update && apt-get install -y iputils-ping
CMD bash
请注意,创建Docker镜像有最佳实践,例如在完成后清除apt缓存文件等。docker exec -u 0 -it <container> /bin/bash
。其中“-u 0”表示用户root。 - Aldo Inácio da SilvaE: 无法定位软件包 iputils-ping
:/ - jaques-samapt update
吗? - Farhad Farahiapt
/apt-get
失败了... - JHBonarius这里是Ubuntu的Docker Hub页面,这里是它的创建方式。它只安装了(有点)最基本的软件包,因此如果需要其他软件包,您需要自己安装。
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
apt-get install
命令后,应通过运行apt-get clean && rm -rf /var/lib/apt/lists/*
来最小化镜像大小。echo -e
实际上违反了 POSIX sh 标准(http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html),它只允许在输出中打印 -e
。即使是在某些版本的 bash 中,这也是默认行为。使用 printf
替代:printf '%s\n' "FROM ubuntu" "RUN apt-get update && apt-get install -y iputils-ping" "CMD bash"
,并查看上述链接标准文档中的 APPLICATION USAGE 部分。 - Charles Duffyecho -e
(但在POSIX模式下编译时将具有符合标准的行为),或者使用适当的环境变量或其他运行时配置。 - Charles Duffy-n
作为第一个参数或存在任何反斜杠文字时以实现定义的方式运行,但即使如此,它也是 实现定义,而不是标准保证,因此行为取决于使用的个别 shell。 - Charles Duffy或者您可以使用已经安装了ping的Docker镜像,例如busybox:
docker run --rm busybox ping SERVER_NAME -c 2
一般人会拉取Ubuntu/CentOS的官方镜像,但他们可能没有意识到这些镜像是迷你版本,并且没有其他额外的内容。
对于Ubuntu,该镜像是由Canonical提供的官方rootfs tarballs构建而成。鉴于这是一个最小的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
从基础镜像启动容器,然后在容器中安装这些工具,并提交到镜像中。
docker commit -m "任何描述性信息" 容器ID 镜像名称:最新版本我在Debian 10上使用了以下语句。
apt-get install iputils-ping
有时,在Docker中最小化安装的Linux系统没有定义路径,因此需要使用...来调用ping。
cd /usr/sbin
ping <ip>
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>
host# PS1='container# ' nsenter -t <PID> -n
PS1
环境变量仅用于在容器的网络命名空间中显示不同的提示符。ping
、netstat
、ifconfig
、ip
等命令,前提是它们已安装在主机上。container# ping <IP>
container# ip route get <IP>
....
container# exit