Docker 安装 Centos

用容器跑 CentOS 环境,以及 CentOS 7 已 EOL 后的替代选择。

Beginner
Key PointsCentOS 7 已于 2024-06 停止维护,官方镜像的 yum 源已失效;新项目用 Rocky / AlmaLinux / Ubuntu。

跑一个 CentOS 容器

Bash
docker pull centos:7
docker run -it centos:7 bash
cat /etc/redhat-release
exit

EOL 之后的问题

CentOS 7 mirrors went offline after June 2024, so yum install fails with 404 or DNS errors. The only stopgap is pointing at vault.centos.org.

Bash
sed -i 's|^mirrorlist=|#mirrorlist=|g' /etc/yum.repos.d/CentOS-*.repo
sed -i 's|^#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' \
  /etc/yum.repos.d/CentOS-*.repo
yum clean all && yum makecache

迁移到替代品

FromToNote
CentOS 7Rocky Linux 8/9 / AlmaLinuxRHEL 兼容,迁移成本最低
CentOS 8Rocky / AlmaLinux / StreamStream 是滚动上游
任意Ubuntu LTS / Debian生态与镜像最全

用 Rocky 代替

Dockerfile
FROM rockylinux:9
RUN dnf install -y --setopt=install_weak_deps=False nginx \
 && dnf clean all
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Never base production images on an EOL distribution: no security updates means shipping known vulnerabilities.

Friends & Partners