rhel 9 系统 Docker 运行 CentOS yum 命令卡死

CentOS Stream 9、Rocky Linux 9 等 rhel 9 系统。在 Docker 容器的 CentOS7 镜像执行 yum 操作 特别慢。 是因为 open files 太大了。

先查看之前的 ulimit

docker run -it --tty --network host --name centos7 centos:centos7.9.2009 /bin/bash
ulimit -a

输出

core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 30537
max locked memory       (kbytes, -l) 8192
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1073741816
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

替换源

for f in /etc/yum.repos.d/*.repo
do
    mv -f "$f" "${f}.backup"
done
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/artifactory/os-conf/centos/centos-7.repo

yum makecache -y 
yum update -y 

直接卡死在 Updating : glibc-2.17-326.el7_9.3.x86_64

现在我们重新加上 ulimit

docker run -it --tty --network host --name centos7 --ulimit "nofile=1024:1048576" centos:centos7.9.2009 /bin/bash
ulimit -a

输出

core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 30537
max locked memory       (kbytes, -l) 8192
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

执行上面的步骤,就不会卡死了。

参考 https://github.com/moby/moby/issues/45838