OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: “ip”: executable file not found in $PATH: unknown (Docker容器沒有ip addr命令:exec ip addr 報錯)

一、報錯

1、報錯信息1:

OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: “ip”: executable file not found in $PATH: unknown

2、報錯原因:

我們下載的某個鏡像(例如tomcat鏡像)是精簡版的,利用這個鏡像去打開一個容器的時候發現沒有ip addr這個命令。

3、解決報錯1的方法:安裝工具 iproute2

# 進入容器內部(比如tomcat01容器)
docker exec -it --name tomcat01 -P tomcat:9.0 /bin/bash
apt install -y iproute2

注意:查看一下容器的系統版本信息:

cat /etc/os-release

★ Linux系統分為兩種:

1.RedHat系列:Redhat、Centos、Fedora等

2.Debian系列:Debian、Ubuntu等

  • RedHat系列的包管理工具是yum

  • Debian系列的包管理工具是apt-get

● 查看系統版本命令:cat /etc/os-release

二、又報錯:

1、報錯信息2:

Reading package lists… Done

Building dependency tree… Done

Reading state information… Done

E: Unable to locate package iproute2

2、報錯原因:

包管理工具apt的鏡像是國外的導致,下載速度過慢導致的。

3、解決報錯2的方法:更換apt 配置文件中的鏡像

# 進入配置文件
cd /etc/apt
# 查看目錄信息
ls
cat sources.list
# 備份
mkdir cat sources.list.backup
cp sources.list ./sources.list.backup
cd ../
# 以覆蓋+追加的方式替換掉sources.list文件
echo 'deb //mirrors.aliyun.com/debian bullseye main'>sources.list
echo 'deb //mirrors.aliyun.com/debian-security bullseye-security main'>>sources.list
echo 'deb //mirrors.aliyun.com/debian bullseye-updates main'>>sources.list
# 執行一下更新命令:
apt-get update -y
# 執行下載 iproute2命令:
apt install -y iproute2

三、問題解決,測試一下,在docker 容器內使用ip 命令

# 測試1:-it 與容器進行交換
docker exec -it --name tomcat01 -P tomcat:9.0 ip addr
# 測試2:先進入容器,然後測試ip命令
docker exec -it --name tomcat01 -P tomcat:9.0 /bin/bash
ip a

以下,是我,搜索本問題時,被誤導產生的一些無效折騰

■ 被誤導在centos7(我的宿主機)中安裝 iproute2,實則是centos新版版(比如centso7/centos8早已內置網絡工具iproute2)

■ 然後又為了解決Docker容器沒有ip addr命令,在宿主機安裝 epel-release,結果報錯,沒有找到這個包,是因為網上的解決方案,命令不全,沒有先執行下載 epel-release安裝包,就直接來個yum install,導致找不到包。

■ 同樣,對於 iproute2,也是沒有給出下載命令,卻直接來個安裝命令yum install iproute2,導致找不到包iproute2。

■ 最重要的是 centos新版本已經內置有了iproute2,沒必要安裝呀

■ 最最最重要的是跑題了,咱需要考慮的是命令ip是在docker容器執行失敗,而在宿主機執行正常!解決起點應該回到容器內安裝網絡工具 iproute2。因為下載的鏡像是精簡版的,默認不自帶網絡工具iproute2。


● Docker容器沒有ip addr命令:exec ip addr 報錯:

OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: “ip”: executable file not found in $PATH: unknown

  • 報錯原因:我們下載的Tomcat鏡像是精簡版的,利用這個鏡像去打開一個容器的時候發現沒有ip addr這個命令。

  • 解決方式:安裝 iproute2:apt install -y iproute2

● 又錯誤:

-bash: apt: command not found

  • 問題原因:linux的版本造成的,我這個版本使用的是yum,而不是apt

  • 解決:yum install -y iproute2

● 又錯誤:No package iproute2 available. Error: Nothing to do

★ 解決方式1:安裝yum的擴展包epel-release

  • 查看linux版本,下載對應版本的epel-release

    # 查看系統的命令:
    cat /etc/redhat-release
    

● 又錯誤:安裝epel-release報錯

執行命令 yum -y install //mirrors.aliyun.com/epel/7/x86_64/Packages/e/epel-release-7-14.noarch.rpm

  • 報錯:

    Loaded plugins: fastestmirror
    epel-release-7-14.noarch.rpm | 15 kB 00:00:00
    Examining /var/tmp/yum-root-OIjg1L/epel-release-7-14.noarch.rpm: epel-release-7-14.noarch
    /var/tmp/yum-root-OIjg1L/epel-release-7-14.noarch.rpm: does not update installed package.
    Error: Nothing to do

  • 解決:先使用wget 命令下載,然後再執行yum 命令安裝

    # 查看 epel 版本信息
    命令:rpm -qa|grep epel
    epel-release-7-14.noarch
    # 卸載老版本的epel-release(若是最新版,這不用版本可以不寫,我的剛好就是最新版本的,不然就需要寫明卸載epel-release-某個版本)
    命令:yum remove epel-release  或者 yum remove epel-release-7-14.noarch
    # 安裝 epel-release
    命令:yum install -y epel-release-7-14.noarch
    

● 此時,安裝iproute2,錯誤依舊:yum install -y iproute2

No package iproute2 available.
Error: Nothing to do

—更換解決方式

★ 方式2:更新yum 源,記得先備份原先的yum源(結果依然無效)

# 進入目錄
cd /etc/yum.repos.d/
# 備份:
備份方式1:
mkdir yum.repos.d.backup
cp -r yum.repos.d ./yum.repos.d.backup
備份方式2:mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 根據centos版本下載對應的新源【我的centos是版本7的】
wget -O /etc/yum.repos.d/CentOS-Base.repo //mirrors.aliyun.com/repo/Centos-7.repo 
# 清空yum緩存
yum clean all
# 生成緩存,會把新下載CentOS-Base.repo源生效
yum makecache 
# 更新
yum -y update

☺ 我明白了:No package iproute2 available.

Error: Nothing to do。

意思是,找不到可以利用的包(安裝包),解決:就是先下載下來 iproute2的安裝包,然再安裝。

# 下載iproute2的安裝包
# 安裝iproute2

本題解決方法,早已跑偏,還記得咱的問題是什麼嗎?

1、了解網絡配置工具net-tools與iproute2,發現:net-tools 是老版本linux的網絡工具,而iproute2是linux新版本(例如centos7、centos8)的網絡工具,且已經內置,不用手動安裝“至此,恍然大悟,跑題了,咱是docker 內部沒有iproute2,而不是centos系統沒有”。

可以直接使用網絡命令,例如ip addr 等等,查看iproute2的版本命令: ip -V

[root@iZwz9535z41cmgcpkm7i81Z ~]# ip -V
ip utility, iproute2-ss170501

2、iproute2的下載鏡像://mirrors.edge.kernel.org/pub/linux/utils/net/iproute2/

Docker容器沒有ip addr命令:exec ip addr 報錯.

  • 報錯原因:我們下載的Tomcat鏡像是精簡版的,利用這個鏡像去打開一個容器的時候發現沒有ip這個命令。

  • 解決方式:

    • [先解決安裝yum 的問題,再通過yum 安裝iproute2] (不對,通過查詢docker 容器的系統版本,發現版本是debain,內置的是apt,不是yum)

    • 安裝 iproute2:apt install -y iproute2

(1)查看docker 容器的系統版本:cat /etc/os-release

# 系統版本
root@f1cfb81dedfd:/usr/local/tomcat# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="//www.debian.org/"
SUPPORT_URL="//www.debian.org/support"
BUG_REPORT_URL="//bugs.debian.org/"

# 系統版本詳情,redhat的命令:cat /etc/redhat-release   debain的命令:cat /etc/debian_version

(2)安裝 iproute2:

root@f1cfb81dedfd:/usr/local/tomcat# apt install -y iproute2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package iproute2
  • 問題:Unable to locate package:無法找到包。

  • 解決:升級一下 apt,再安裝 iproute2

     apt update
    
  • 又報錯:

    18 packages can be upgraded. Run ‘apt list –upgradable’ to see them.
    W: Failed to fetch //deb.debian.org/debian/dists/bullseye/InRelease Could not connect to debian.map.fastlydns.net:80 (151.101.74.132), connection timed out Could not connect to deb.debian.org:80 (151.101.110.132), connection timed out
    W: Failed to fetch //deb.debian.org/debian/dists/bullseye-updates/InRelease Unable to connect to deb.debian.org:http:
    W: Some index files failed to download. They have been ignored, or old ones used instead.

如果本文對你有幫助的話記得給一樂點個贊哦,感謝!