在学习Docker的网络工作原理的时候,跟着教程查看容器内的ip,被提示找不到命令:

[root@docker ~]# docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
692c352adcf2: Pull complete 
97058a342707: Pull complete 
2821b8e766f4: Pull complete 
4e643cc37772: Pull complete 
Digest: sha256:55cd38b70425947db71112eb5dddfa3aa3e3ce307754a3df2269069d2278ce47
Status: Downloaded newer image for ubuntu:latest
root@5d0107c52f43:/# ip a
bash: ip: command not found
root@5d0107c52f43:/# ping www.baidu.com
bash: ping: command not found

原因是docker的base镜像只包含简单的操作系统,命令需要自己安装

root@5d0107c52f43:/# apt update && apt -y install iproute2

更新并安装iproute2后,就可以查看docker容器的网络配置情况了

root@5d0107c52f43:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 17x.1x.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
root@5d0107c52f43:/#   

注:

  • 在命令行安装的时候需要:apt-get update,作用是:同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。
  • 等更新完之后:apt-get install iproutes命令即可。


 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐