错误信息:

[root@localhost ~]# docker exec -it tomcat01 ip addr
OCI runtime exec failed: exec failed: unable to start container process: exec: "ip": executable file not found in $PATH: unknown
[root@localhost ~]# docker exec -it tomcat02 ping 172.17.0.2
OCI runtime exec failed: exec failed: unable to start container process: exec: "ping": executable file not found in $PATH: unknown

解决办法:

前言

最近在学狂神 Docker 网络时遇到的问题,查看容器内部网络地址报错信息如上。

  • 报错原因:

    我们下载的Tomcat镜像是精简版的,运行并进入 tomcat01 容器后发现没有ip addr 和 ping 命令。

  • 解决方式:

    安装 iproute2:apt install -y iproute2

    安装 ping:apt-get install -y iputils-ping

解决过程如下

  1. 宿主机测试 ip addr 命令

    # 主机没问题 那就是容器的问题
    [root@localhost ~]# ip addr
    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
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:8e:88:3a brd ff:ff:ff:ff:ff:ff
        inet 192.168.190.128/24 brd 192.168.190.255 scope global noprefixroute dynamic ens33
           valid_lft 1631sec preferred_lft 1631sec
        inet6 fe80::6ea2:3b88:e13b:3953/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever
    3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
        link/ether 52:54:00:38:dd:4a brd ff:ff:ff:ff:ff:ff
        inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
           valid_lft forever preferred_lft forever
    4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
        link/ether 52:54:00:38:dd:4a brd ff:ff:ff:ff:ff:ff
    5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
        link/ether 02:42:4a:0a:13:58 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
           valid_lft forever preferred_lft forever
        inet6 fe80::42:4aff:fe0a:1358/64 scope link 
           valid_lft forever preferred_lft forever
    15: veth9230795@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
        link/ether d6:fc:02:ee:0f:1b brd ff:ff:ff:ff:ff:ff link-netnsid 0
        inet6 fe80::d4fc:2ff:feee:f1b/64 scope link 
           valid_lft forever preferred_lft forever
    
  2. 查看运行容器系统版本

    # 进入运行的容器,查看版本
    
    root@2de81861a3cd:/usr/local/tomcat# cat /etc/os-release
    PRETTY_NAME="Ubuntu 22.04.1 LTS"
    NAME="Ubuntu"
    VERSION_ID="22.04"
    VERSION="22.04.1 LTS (Jammy Jellyfish)"
    VERSION_CODENAME=jammy
    ID=ubuntu
    ID_LIKE=debian
    HOME_URL="https://www.ubuntu.com/"
    SUPPORT_URL="https://help.ubuntu.com/"
    BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
    PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
    UBUNTU_CODENAME=jammy
    
    # 网上查阅资料得知,该系统内置 apt,并非 yum
    
  3. 安装 iproute2

    # 系统内置 apt
    root@2de81861a3cd:/usr/local/tomcat# yum -y install iproute2
    bash: yum: command not found
    
    # apt 安装失败,版本太低
    root@2de81861a3cd:/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
    
    # apt 版本升级 
    root@2de81861a3cd:/usr/local/tomcat#  apt update
    
    # 再次安装
    root@2de81861a3cd:/usr/local/tomcat# apt install -y iproute2
    
    # 测试
    root@2de81861a3cd:/usr/local/tomcat# ip addr
    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
    14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
        link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
        inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
           valid_lft forever preferred_lft forever
    # 成功解决       
    
  4. 安装 ping

    # tomcat02 容器 安装 ping
    root@e338c1a7ca22:/usr/local/tomcat# apt-get install -y iputils-ping
    
    # 测试	tomcat02 ping tomcat01 的 ip
    [root@localhost ~]# docker exec -it tomcat02 ping 172.17.0.2
    OCI runtime exec failed: exec failed: unable to start container process: exec: "ping": executable file not found in $PATH: unknown
    [root@localhost ~]# docker exec -it tomcat02 ping 172.17.0.2
    PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
    64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.995 ms
    64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.061 ms
    64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.068 ms
    64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.065 ms
    64 bytes from 172.17.0.2: icmp_seq=5 ttl=64 time=0.101 ms
    ^C
    --- 172.17.0.2 ping statistics ---
    5 packets transmitted, 5 received, 0% packet loss, time 4005ms
    rtt min/avg/max/mdev = 0.061/0.258/0.995/0.368 ms
    
    # 成功
    

结尾

由于只是在 tomcat01 容器里配置,当运行另一个容器 tomcat02 时,还是会有相同的问题,解决办法同上,麻烦!

离谱问题:

在学习狂神 Docker 自定义网络时 遇到的问题:

# 自定义网络 mynet 
[root@localhost ~]# docker run -d -P --name tomcat-net-01 --net mynet tomcat
[root@localhost ~]# docker run -d -P --name tomcat-net-02 --net mynet tomcat

# tomcat-net-01 ping tomcat-net-02
[root@bogon ~]# docker exec -it tomcat-net-01 ping 192.168.0.3
OCI runtime exec failed: exec failed: unable to start container process: exec: "ping": executable file not found in $PATH: unknown

出现的问题和昨天的一样,我以为更新一下 apt,安装 ping 包 就没事了,结果出乎意料!

具体问题如下:

  1. 交互模式进入 tomcat-net-01:

    [root@localhost ~]# docker exec -it tomcat-net-01 /bin/bash
    
  2. 安装 ping:

    root@f68749f62a90:/usr/local/tomcat# apt-get install -y iputils-ping
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    E: Unable to locate package iputils-ping
    # 安装失败,更新一下 apt
    
  3. 更新 apt:

    # 问题来了
    root@f68749f62a90:/usr/local/tomcat# apt update
    Ign:1 http://archive.ubuntu.com/ubuntu jammy InRelease                   
    Ign:2 http://security.ubuntu.com/ubuntu jammy-security InRelease         
    Ign:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease           
    Ign:2 http://security.ubuntu.com/ubuntu jammy-security InRelease         
    Ign:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease         
    Ign:2 http://security.ubuntu.com/ubuntu jammy-security InRelease         
    Ign:1 http://archive.ubuntu.com/ubuntu jammy InRelease                   
    Err:2 http://security.ubuntu.com/ubuntu jammy-security InRelease         
      Temporary failure resolving 'security.ubuntu.com'
    Ign:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease           
    Ign:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
    0% [Connecting to archive.ubuntu.com]^C
    
    # Err:2 http://security.ubuntu.com/ubuntu jammy-security InRelease         
    #  Temporary failure resolving 'security.ubuntu.com'
    

    昨天还没事今天就不行了?网上查找资料后发现要修改配置文件:/etc/resolv.conf

    我们先看一下是否有这个文件:

    root@f68749f62a90:/usr/local/tomcat# cat /etc/resolv.conf
    
    search localdomain
    nameserver 127.0.0.11
    options ndots:0
    

    tomcat 容器里确实存在,那我们 vim 修改即可!

  4. 修改 /etc/resolv.conf 配置文件

    # tomcat 容器里并没有 vi/vim 
    root@f68749f62a90:/usr/local/tomcat# vi /etc/resolv.conf
    bash: vi: command not found
    root@f68749f62a90:/usr/local/tomcat# vim /etc/resolv.conf
    bash: vim: command not found
    
  5. 安装 vim

    root@f68749f62a90:/usr/local/tomcat# apt-get install -y vim
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    E: Unable to locate package vim
    root@f68749f62a90:/usr/local/tomcat# sudo apt install vim
    bash: sudo: command not found
    
    # apt 安装 vim 失败,需要更新 apt 回到 步骤3
    

离谱问题来了:

  • apt update 失败 需要修改 /etc/resolv.conf 文件,修改文件需要安装 vim
  • 安装 vim 需要用 apt ,步骤5 安装 vim 失败需要更新 apt

死循环,目前暂未找到解决办法!

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐