尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

CentOS 7 容器时遇到了 yum update 报错

CentOS 7 容器时遇到了 yum update 报错
📅 发布时间:2026/6/20 4:24:43
CentOS 7 容器时遇到了 yum update 报错

你在 CentOS 容器中执行 yum update -y 时遇到的错误,

`
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"

One of the configured repositories failed (Unknown),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:

 1. Contact the upstream for the repository and get them to fix the problem.2. Reconfigure the baseurl/etc. for the repository, to point to a workingupstream. This is most often useful if you are using a newerdistribution release than is supported by the repository (and thepackages for the previous distribution release still work).3. Run the command with the repository temporarily disabledyum --disablerepo=<repoid> ...4. Disable the repository permanently, so yum won't use it by default. Yumwill then just ignore the repository until you permanently enable itagain or use --enablerepo for temporary usage:yum-config-manager --disable <repoid>orsubscription-manager repos --disable=<repoid>5. Configure the failing repository to be skipped, if it is unavailable.Note that yum will try to contact the repo. when it runs most commands,so will have to try and fail each time (and thus. yum will be be muchslower). If it is a very temporary problem though, this is often a nicecompromise:yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64
`
主要是因为容器无法解析 mirrorlist.centos.org 主机名,以及 CentOS 7 官方仓库已停止维护。下面是一些解决方案。

🧭 解决方案一览表

问题类型 解决方法 关键命令/操作

DNS 解析失败 配置容器使用有效的 DNS 服务器 echo "nameserver 8.8.8.8" > /etc/resolv.conf

镜像源失效 (主要问题) 将仓库源切换到仍在维护的存档源(如 vault.centos.org) 修改 /etc/yum.repos.d/ 下 .repo 文件的 baseurl

网络连通性 检查容器内部网络是否正常 ping -c 4 8.8.8.8

缓存问题 清理 Yum 缓存并重新生成 yum clean all & yum makecache

🔧 详细解决步骤

  1. 配置 DNS(解决“Could not resolve host”)

容器内默认的 DNS 配置可能无法解析域名。尝试以下命令:

检查当前的DNS配置

cat /etc/resolv.conf

如果配置有问题或为空,手动设置一个公共DNS

echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf

测试域名解析

nslookup mirrorlist.centos.org

如果 nslookup 命令不存在,可以尝试 ping -c 4 8.8.8.8 先测试基础网络连通性。如果 ping 也不存在,说明容器非常精简,你需要先通过其他方式(比如使用 --network host 参数启动容器或挂载宿主机的 /etc/resolv.conf)来保证网络正常。

  1. 修改 Yum 仓库源(核心步骤)

由于 CentOS 7 已在 2024 年 6 月 30 日停止维护(EOL),其官方软件仓库(包括 mirrorlist.centos.org)已关闭并移到了存档服务器 vault.centos.org。你需要修改容器中的 Yum 仓库配置。

• 备份现有的仓库配置文件:
cd /etc/yum.repos.d/
mkdir backup
mv *.repo backup/

• 创建新的仓库配置文件(例如 CentOS-Base.repo):
cat << 'EOF' > /etc/yum.repos.d/CentOS-Base.repo
[base]
name=CentOS-7 - Base
baseurl=http://vault.centos.org/centos/7/os/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
enabled=1

[updates]
name=CentOS-7 - Updates
baseurl=http://vault.centos.org/centos/7/updates/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
enabled=1[extras]
name=CentOS-7 - Extras
baseurl=http://vault.centos.org/centos/7/extras/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
enabled=1
EOF注意:vault.centos.org 是官方存档,软件包不会更新。

• 如果你想使用国内镜像源加速(例如阿里云),可以将 baseurl 替换为:

baseurl=http://mirrors.aliyun.com/centos-vault/7.x.x.x/os/x86_64/
请根据你的 CentOS 7 小版本号(通过 cat /etc/redhat-release 查看)替换其中的 x.x.x,例如 7.9.2009。请注意,国内镜像也可能存在同步问题或不再维护 CentOS 7 源的情况。
  1. 清理缓存并重新测试

修改仓库配置后,执行:
yum clean all # 清理旧缓存
yum makecache # 生成新缓存
yum update -y # 再次尝试更新

🐳 给使用 Docker 的建议

如果你是在 Docker 容器中遇到此问题,并且需要持久化这些修改,可以考虑以下方法:

  1. 在运行容器时直接指定 DNS:
    docker run --dns 8.8.8.8 -it --rm centos:7 /bin/bash

  2. 创建自定义 Dockerfile:
    将修复步骤写入 Dockerfile,一劳永逸。
    FROM centos:7

    备份原有repo文件(可选)

    RUN mkdir -p /etc/yum.repos.d/backup && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/

    添加新的repo文件

    ADD CentOS-Base.repo /etc/yum.repos.d/ # 需要你事先准备好新的repo文件放在构建上下文

    或者直接使用sed命令修改(如果网络畅通,也可尝试此方式下载)

    RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

    清理缓存并安装所需软件

    RUN yum clean all && yum makecache

    然后构建新镜像:docker build -t my-centos:7 .

  3. 检查容器网络模式:
    使用 docker run --network host -it --rm centos:7 让容器共享主机网络,有时可以避免 DNS 问题。

💡 注意事项

• vault.centos.org 的限制:迁移到 vault.centos.org 后,你无法再获得任何安全更新或软件包更新,因为它是存档镜像,只提供历史版本的软件包。

• 最终手段:如果所有方法都失败,并且你只是需要安装少量软件,可以尝试从其他来源手动下载 RPM 包并使用 rpm -ivh 命令安装。

• 长远考虑:CentOS 7 已停止维护,强烈建议将应用迁移到受支持的操作系统,如 CentOS Stream, Rocky Linux, AlmaLinux 或 Ubuntu LTS 等,以获得安全更新和支持。

本文来自博客园,作者:茄子_2008,转载请注明原文链接:https://www.cnblogs.com/xd502djj/p/19102832

相关新闻

  • MIT新论文:数据即上限,扩散模型的关键能力来自图像统计规律,而非复杂架构
  • 基于MATLAB的视频动态目标跟踪检测搭建方案
  • 第三篇:Windows10/11软件集成与系统优化 - 教程

最新新闻

  • 2026年靠谱的上海特种电缆/上海PU电缆优质厂家推荐榜 - 品牌宣传支持者
  • 2026年靠谱的pvc给水管/安徽pvc管/pvc排水管可靠供应商推荐 - 行业平台推荐
  • 2026年口碑好的激光切管/济宁激光切管/激光切管代工/济宁激光切管代工精选厂家推荐 - 品牌宣传支持者
  • 青岛即墨区靠谱的空调清洗公司咨询电话(2026最新) - 品牌排行榜
  • 2026年质量好的医药合规卷筒不干胶/食品包装卷筒不干胶/定制卷筒不干胶厂家哪家好 - 行业平台推荐
  • 2026年可靠的青岛办公工学椅/青岛人体工学椅/工学椅/商务久坐工学椅公司哪家好 - 行业平台推荐

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号