Docker下CentOS镜像中yum命令无效的解决办法

本文详细介绍了在Docker环境下CentOS镜像中遇到yum命令无效的问题,并提供了有效的解决方案,包括清除缓存等步骤,确保了后续操作的顺利进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

问题概述

如下图:

 

具体内容如下:

[root@localhost tomcat]# docker build -f /opt/docker/tomcat/dockerfilebytomcat -t huazai/web/runtime/tomcat:v1.0 .
Sending build context to Docker daemon  199.7MB
Step 1/16 : FROM centos
 ---> 75835a67d134
Step 2/16 : MAINTAINER alibaba_huazai<who.seek.me@java98k.vip>
 ---> Running in 0964c1d94546
Removing intermediate container 0964c1d94546
 ---> fc3103b53aeb
Step 3/16 : COPY b2c_data.db /opt/data/b2c_data.db
 ---> 1fa3b98fbb82
Step 4/16 : ADD jdk-8u151-linux-x64.tar.gz /usr/local/
 ---> f22664a4a5c1
Step 5/16 : ADD apache-tomcat-9.0.12.tar.gz /usr/local/
 ---> 7f30d9675eda
Step 6/16 : RUN yum -y install net-tools
 ---> Running in f4f8321831a8
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors


 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 working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-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 much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
12: Timeout on http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
The command '/bin/sh -c yum -y install net-tools' returned a non-zero code: 1

 

 

解决办法

yum 帮助提示的解决方案如下:

存储库配置失败的(未知)之一,而且yum没有足够的缓存数据来继续(在这一点上唯一的)。导致安全的 yum 可能会出现使用失败的情况,有以下列出了一些方法可以解决这个问题:

1、联系以上存储库源,让他们修复该问题。

 

2、重新配置 baseurl /etc 。对于存储库,要指向一个 working upstream。如果您使用的是一个更新的存储库,这通常非常有用的。

 

3、在临时禁用存储库的情况下运行该命令

 yum --disablerepo=<repoid> ...

 

4、永久禁用存储库,因此 yum 在默认情况下不会使用它。然后 yum 将忽略存储库,直到永久启用它再次或者使用 —— enablerepo用于临时使用:

    yum-config-manager --disable <repoid>
或者
    subscription-manager repos --disable=<repoid>

 

5、配置要跳过的失败存储库(如果该存储库不可用)。请注意,当它运行大多数命令时,yum 将尝试回购。所以每次都会尝试失败(因此 yum 将会是非常的慢)。如果这是一个非常暂时的问题,则可以通过以下方法解决:

yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

 

这儿博主使用的方式:

# 清除缓存
rm /var/lib/rpm/__db*

在错误信息中已经看到了, yum 中没有足够的缓存空间,这儿只需要清理以下就可以了。

 

 

 


好了,关于 Docker下CentOS镜像中yum命令无效的解决办法  就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。 
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。


作       者:华    仔
联系作者:who.seek.me@java98k.vip
来       源:优快云 (Chinese Software Developer Network)
原       文:https://blog.youkuaiyun.com/Hello_World_QWP/article/details/84631197
版权声明:本文为博主原创文章,请在转载时务必注明博文出处!
### 修改 DockerCentOSYum配置方法 在 Docker 容器中运行 CentOS 并修改其 Yum配置文件是一项常见的操作,可以显著提升软件包下载速度和稳定性。以下是具体实现方式: #### 进入容器并编辑 Yum 配置文件 1. **启动容器** 使用 `docker run` 命令创建并进入一个交互式的 CentOS 容器,指定网络模式为桥接(bridge),以便容器能够正常访问外部网络资源。 ```bash docker run -it --name centos-container --network bridge centos /bin/bash ``` 2. **备份原始配置文件** 在修改之前建议先备份 `/etc/yum.repos.d/` 下的所有 `.repo` 文件,以防出现问题时可快速恢复原状。 ```bash cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup.repo ``` 3. **注释原有镜像源地址** 将默认的镜像列表路径替换为注释状态,避免冲突。 ```bash sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*.repo ``` 4. **更改基础 URL 地址至稳定仓库** 替换默认的基础 URL 到更稳定的 CentOS Vault 站点或其他可信第三方源。 ```bash sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*.repo ``` 5. **更新缓存并验证新源可用性** 清除旧的元数据缓存,并重新构建索引以应用最新的变更。 ```bash yum clean all && yum makecache ``` 6. **测试安装任意软件包确认功能正常** 执行简单的命令来检验是否成功切换到新的 Yum 源。 ```bash yum install -y nano ``` 以上步骤综合了多个参考资料的内容[^1][^2][^3]。 如果目标操作系统版本较新(如 CentOS 8 或更高版),需注意部分工具已被替代,例如由 DNF 取代传统 YUM 工具链的情况,则应按照特定指导调整流程[^5]。 对于进一步优化体验或者解决特殊场景下的依赖关系问题,还可以考虑引入阿里云、腾讯云或者其他国内知名厂商提供的加速服务链接作为自定义 Yum 源的一部分[^4]。 ```python # 示例 Python 脚本展示如何通过编程批量处理 repo 文件 (仅作演示用途) import os def modify_yum_sources(): repos_dir = '/etc/yum.repos.d/' for filename in os.listdir(repos_dir): if not filename.endswith('.repo'): continue with open(os.path.join(repos_dir, filename), 'r+') as f: content = f.read() updated_content = ( content.replace('mirrorlist=', '#mirrorlist=') .replace('#baseurl=http://mirror.centos.org', 'baseurl=http://vault.centos.org') ) f.seek(0) f.write(updated_content) f.truncate() modify_yum_sources() ```
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TechBro华仔

日拱一卒无有尽,功不唐捐终入海

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值