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

news/2025/9/20 20:53:26/文章来源:https://www.cnblogs.com/xd502djj/p/19102832

你在 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 等,以获得安全更新和支持。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/908538.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MIT新论文:数据即上限,扩散模型的关键能力来自图像统计规律,而非复杂架构

现在的文生图模型已经十分强大了,例如我们在输入框敲下 “a photorealistic astronaut riding a horse on the moon”,几秒钟后屏幕生成从未出现过的图像,细节丰富,几近完美。扩散模型(diffusion models)推动了这…

实用指南:光学神经网络与人工智能应用

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

Zabbix 企业级监控架构实战指南:从搭建、可视化到智能告警

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

基于MATLAB的视频动态目标跟踪检测搭建方案

基于MATLAB的视频动态目标跟踪检测搭建方案pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "…

第三篇:Windows10/11软件集成与系统优化 - 教程

第三篇:Windows10/11软件集成与系统优化 - 教程2025-09-20 20:21 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display…

U522155 数据生成(小心电脑)

// code by 樓影沫瞬_Hz17 #include <bits/stdc++.h> using namespace std;#define getc() getchar_unlocked() #define putc(a) putchar_unlocked(a) #define en_ putc(\n) #define e_ putc( )using pii = pair…

Windows-Appx

Windows-Appx导航 (返回顶部)1. PS_modules 2. Appx 3. Get-AppxPackage3.1 Syntax 语法 3.2 Description 描述 3.3 Examples 3.4 Parameters 参数4. Remove-AppxPackage4.1 Syntax 4.2 Description 4.3 Parameters5. …

实用指南:OSG中osgFX库

实用指南:OSG中osgFX库pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", &qu…

2025.9.20——1橙

终于结束初赛,可以好好写写编程题了。 普及- P10108 [GESP202312 六级] 闯关游戏 很简单的一道dp题,但我因为没考虑到答案可能是负数,卡了好几次。

CSP 2025 游记

前言 被迫报了 J 组,不好评价。 初赛 J 组 上午坐大巴在车上睡着了,然后正好在考点门口被颠醒了…… 提前半个小时到了考点,又在桌子上趴了一会,不过没睡着。两个监考老师左右脑互搏,至今不知道准考证号前四位要填…

配置Spring框架以连接SQL Server数据库

Spring框架是一个开源的企业级应用框架,用于简化Java开发工作,通过依赖注入(DI)和面向切面编程(AOP)等核心功能支持程序的健壮性和易维护性。要配置Spring框架以连接SQL Server数据库,需要遵循几个关键步骤,从…

这一辈子大多数日子是无聊的

本文纯属个人观点,搏您一笑,请勿上升至道德高度今天晚上去食堂吃饭的路上拍下了这张照片(图一):密密麻麻的不知道什么植物,如同校园里的学生一样多,它们占领了照片的下部。岸边的树、教学楼,乃至于远方的天空,天…

Elasticsearch面试精讲 Day 11:索引模板与动态映射 - 指南

Elasticsearch面试精讲 Day 11:索引模板与动态映射 - 指南2025-09-20 19:44 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !importan…

Go 实现验证码识别

步骤 1:安装 Go 语言 首先,确保你的系统已经安装了 Go 语言。如果没有安装,可以从 Go 官网 下载并安装。 安装后,验证是否成功: 更多内容访问ttocr.com或联系1436423940 go version 步骤 2:安装 Tesseract OCR 我…

跳出 AI 编程的「兔子洞」,4 个实战策略帮你解决90%的死循环

在和 AI 协作编程的时候,你肯定遇到过这样一种情况: 使用 Claude Code 或者 Codex 信心满满的实现一个功能之后,结果你一运行,直接报错。 于是你把错误信息直接复制粘贴回给 AI,它态度好的一笔,立马道歉:非常抱…

用 PHP 和 Tesseract OCR 识别英文数字验证码

验证码是网页中常见的防止自动化攻击的工具,通常它们由一串字母和数字组成,目的是确认用户是人类而不是机器人。很多情况下,验证码都是扭曲、加噪音的图像,这让计算机很难直接读取。幸运的是,借助 OCR(Optical C…

凝望深渊时,深渊也凝望着你(黑洞与摇钱树)

/dev/null与/dev/zero在 Linux 系统中,/dev/null 和 /dev/zero 是两个特殊的设备文件,由内核提供,用于特定的数据处理目的。 /dev/null 是“只进不出”的黑洞,用于丢弃数据。 /dev/zero 是“只出不进”的零源,用于…

详细介绍:《Vuejs设计与实现》第 16 章(解析器) 中

详细介绍:《Vuejs设计与实现》第 16 章(解析器) 中pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&qu…

spring项目部署后为什么会生成 logback-spring.xml记录

spring项目部署后为什么会生成 logback-spring.xml记录pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&q…