Docker 安装 Centos
用容器跑 CentOS 环境,以及 CentOS 7 已 EOL 后的替代选择。
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
exitEOL 之后的问题
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迁移到替代品
| From | To | Note |
|---|---|---|
| CentOS 7 | Rocky Linux 8/9 / AlmaLinux | RHEL 兼容,迁移成本最低 |
| CentOS 8 | Rocky / AlmaLinux / Stream | Stream 是滚动上游 |
| 任意 | 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.
跑一个 CentOS 容器
Bash
docker pull centos:7
docker run -it centos:7 bash
cat /etc/redhat-release
exitEOL 之后的问题
CentOS 7 的镜像源在 2024 年 6 月后陆续下线,容器里 yum install 会报 Could not resolve host: mirrorlist.centos.org 或 404。要临时救活,只能把源指到 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迁移到替代品
| 原系统 | 替代 | 说明 |
|---|---|---|
| CentOS 7 | Rocky Linux 8/9 / AlmaLinux | RHEL 兼容,迁移成本最低 |
| CentOS 8 | Rocky / AlmaLinux / Stream | Stream 是滚动上游 |
| 任意 | 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;"]生产镜像不要基于已 EOL 的发行版:没有安全更新,等于把已知漏洞一起打包上线。