一,目的:
为了把网络软件仓库中的软件下载下来后分享给本地主机,或者直接把网上下载下来的rpm加入到软件仓库中,更高效便捷。搭建一个本地 HTTP 软件仓库,把 Docker 相关的 RPM 包下载下来存到仓库里,这样局域网里的其他主机就能通过 HTTP 访问这个仓库,直接安装 Docker,不用再去互联网上下载。
二,步骤:
1.准备 HTTP 服务(让其他机子能访问你的仓库,创建软件共享仓库)
[root@localhost ~]# dnf install httpd -y Updating Subscription Management repositories. Unable to read consumer identity This system is not registered with an entitlement server. You can use subscription-manager to register. Last metadata expiration check: 0:07:35 ago on Thu 15 Jan 2026 07:39:41 AM EST. Dependencies resolved. ======================================================================================================= Package Architecture Version Repository Size ======================================================================================================= Installing: httpd x86_64 2.4.62-4.el9 AppStream 51 k Installing dependencies: apr x86_64 1.7.0-12.el9_3 AppStream 126 k apr-util x86_64 1.6.1-23.el9 AppStream 97 k apr-util-bdb x86_64 1.6.1-23.el9 AppStream 14 k httpd-core x86_64 2.4.62-4.el9 AppStream 1.5 M httpd-filesystem noarch 2.4.62-4.el9 AppStream 15 k httpd-tools x86_64 2.4.62-4.el9 AppStream 86 k redhat-logos-httpd noarch 90.4-2.el9 AppStream 18 k Installing weak dependencies: apr-util-openssl x86_64 1.6.1-23.el9 AppStream 17 k mod_http2 x86_64 2.0.26-4.el9 AppStream 167 k mod_lua x86_64 2.4.62-4.el9 AppStream 60 k Transaction Summary ======================================================================================================= Install 11 Packages Total size: 2.2 M Installed size: 6.1 M Downloading Packages: Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : apr-1.7.0-12.el9_3.x86_64 1/11 Installing : apr-util-bdb-1.6.1-23.el9.x86_64 2/11 Installing : apr-util-openssl-1.6.1-23.el9.x86_64 3/11 Installing : apr-util-1.6.1-23.el9.x86_64 4/11 Installing : httpd-tools-2.4.62-4.el9.x86_64 5/11 Installing : redhat-logos-httpd-90.4-2.el9.noarch 6/11 Running scriptlet: httpd-filesystem-2.4.62-4.el9.noarch 7/11 Installing : httpd-filesystem-2.4.62-4.el9.noarch 7/11 Installing : httpd-core-2.4.62-4.el9.x86_64 8/11 Installing : mod_lua-2.4.62-4.el9.x86_64 9/11 Installing : mod_http2-2.0.26-4.el9.x86_64 10/11 Installing : httpd-2.4.62-4.el9.x86_64 11/11 Running scriptlet: httpd-2.4.62-4.el9.x86_64 11/11 Verifying : apr-1.7.0-12.el9_3.x86_64 1/11 Verifying : apr-util-1.6.1-23.el9.x86_64 2/11 Verifying : apr-util-bdb-1.6.1-23.el9.x86_64 3/11 Verifying : apr-util-openssl-1.6.1-23.el9.x86_64 4/11 Verifying : httpd-2.4.62-4.el9.x86_64 5/11 Verifying : httpd-core-2.4.62-4.el9.x86_64 6/11 Verifying : httpd-filesystem-2.4.62-4.el9.noarch 7/11 Verifying : httpd-tools-2.4.62-4.el9.x86_64 8/11 Verifying : mod_http2-2.0.26-4.el9.x86_64 9/11 Verifying : mod_lua-2.4.62-4.el9.x86_64 10/11 Verifying : redhat-logos-httpd-90.4-2.el9.noarch 11/11 Installed products updated. Installed: apr-1.7.0-12.el9_3.x86_64 apr-util-1.6.1-23.el9.x86_64 apr-util-bdb-1.6.1-23.el9.x86_64 apr-util-openssl-1.6.1-23.el9.x86_64 httpd-2.4.62-4.el9.x86_64 httpd-core-2.4.62-4.el9.x86_64 httpd-filesystem-2.4.62-4.el9.noarch httpd-tools-2.4.62-4.el9.x86_64 mod_http2-2.0.26-4.el9.x86_64 mod_lua-2.4.62-4.el9.x86_64 redhat-logos-httpd-90.4-2.el9.noarch Complete!2.打开http服务
设置开机自启并立即启动服务,确保 HTTP 服务一直运行
[root@localhost ~]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.3.建立software目录,作为共享仓库的目录
[root@localhost ~]# mkdir /var/www/html/software4.在/etc/yum.repos.d/下建立名为software.repo的仓库
[root@localhost yum.repos.d]# vim software.repo [software] name = software baseurl = http://192.168.110.129/software/ ***自己的IP!!! gpgcheck = 05.下载 Docker 相关 RPM 包
[root@localhost ~]# dnf install docker-ce --downloadonly --destdir /mnt/只下载docker-ce及其所有依赖包,不安装,并且把下载的文件都放到/mnt目录里。
[root@localhost mnt]# mv /mnt/* /var/www/html/software/把下载好的 RPM 包移动到刚才创建的 HTTP 共享目录里,这样其他主机就能通过 HTTP 访问到这些包了。
6.创建仓库元数据(让系统识别这是个软件仓库)
[root@localhost software]# dnf install createrepo -y [root@localhost ~]# createrepo -v /var/www/html/software/ 08:17:06: Version: 0.20.1 (Features: LegacyWeakdeps ) 08:17:07: Signal handler setup 08:17:07: Thread pool ready Directory walk started 08:17:07: Adding pkg: /var/www/html/software/containerd.io-2.2.1-1.el9.x86_64.rpm 08:17:07: Adding pkg: /var/www/html/software/docker-buildx-plugin-0.30.1-1.el9.x86_64.rpm 08:17:07: Adding pkg: /var/www/html/software/docker-ce-29.1.4-1.el9.x86_64.rpm 08:17:07: Adding pkg: /var/www/html/software/docker-ce-cli-29.1.4-1.el9.x86_64.rpm 08:17:07: Adding pkg: /var/www/html/software/docker-ce-rootless-extras-29.1.4-1.el9.x86_64.rpm 08:17:07: Adding pkg: /var/www/html/software/docker-compose-plugin-5.0.1-1.el9.x86_64.rpm 08:17:07: Dir to scan: /var/www/html/software/.repodata 08:17:07: Package count: 6 Directory walk done - 6 packages Temporary output repo path: /var/www/html/software/.repodata/ 08:17:07: Creating .xml.gz files 08:17:07: Setting number of packages Preparing sqlite DBs 08:17:07: Creating databases 08:17:07: Thread pool user data ready Pool started (with 5 workers) Pool finished 08:17:07: Generating repomd.xml 08:17:07: Old repodata doesn't exists: Cannot rename /var/www/html/software/repodata/ -> /var/www/html/software/repodata.old.31514.20260115081707.46238: cp: cannot stat '/var/www/html/software/repodata/': No such file or directory : Child process exited with code 1 08:17:07: Renamed /var/www/html/software/.repodata/ -> /var/www/html/software/repodata/ 08:17:07: Memory cleanup 08:17:07: All done [root@localhost ~]# cd /var/www/html/software/ [root@localhost software]# ls containerd.io-2.2.1-1.el9.x86_64.rpm docker-ce-rootless-extras-29.1.4-1.el9.x86_64.rpm docker-buildx-plugin-0.30.1-1.el9.x86_64.rpm docker-compose-plugin-5.0.1-1.el9.x86_64.rpm docker-ce-29.1.4-1.el9.x86_64.rpm repodata docker-ce-cli-29.1.4-1.el9.x86_64.rpm [root@localhost software]# cd [root@localhost ~]# cd /var/cache/dnf/ [root@localhost dnf]# ls AppStream-19fc8e1e0a4dd1ae BaseOS.solv epel-f0546a9ed4570b45 last_makecache AppStream-filenames.solvx docker-315377eead1481a2 epel-filenames.solvx packages.db AppStream.solv docker-filenames.solvx epel.solv tempfiles.json BaseOS-2133cde63ffc3a12 docker.solv epel-updateinfo.solvx BaseOS-filenames.solvx docker-updateinfo.solvx expired_repos.json [root@localhost dnf]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# ls docker.repo epel.repo redhat.repo rhel.repo [root@localhost yum.repos.d]# vim software.repo [root@localhost yum.repos.d]# dnf makecache Updating Subscription Management repositories. Unable to read consumer identity This system is not registered with an entitlement server. You can use subscription-manager to register. docker 5.1 kB/s | 2.0 kB 00:00 epel 14 kB/s | 4.0 kB 00:00 AppStream 3.1 MB/s | 3.2 kB 00:00 BaseOS 2.7 MB/s | 2.7 kB 00:00 software 602 kB/s | 4.9 kB 00:00 Metadata cache created. [root@localhost yum.repos.d]# ls /var/cache/dnf/ AppStream-19fc8e1e0a4dd1ae docker-filenames.solvx expired_repos.json AppStream-filenames.solvx docker.solv last_makecache AppStream.solv docker-updateinfo.solvx packages.db BaseOS-2133cde63ffc3a12 epel-f0546a9ed4570b45 software-4ded3c8cea30e088 *** BaseOS-filenames.solvx epel-filenames.solvx software-filenames.solvx *** BaseOS.solv epel.solv software.solv docker-315377eead1481a2 epel-updateinfo.solvx tempfiles.json扫描rpm软件包信息并记录生成
create...在共享目录里生成元数据,这样其他主机的dnf/yum就能识别这个目录是一个合法的软件仓库
#采集软件包信息
#注意在目录中不能有不是rpm类的文件
关于createrepo:
createrepo是把一堆零散的 RPM 包,变成一个系统可识别的软件仓库的核心工具。
1. 核心作用
生成仓库元数据:它会扫描你指定目录里的所有 RPM 包,分析每个包的名称、版本、依赖关系、文件列表等信息,然后生成一个叫
repodata的目录。让
dnf/yum能 “看懂” 仓库:dnf/yum安装软件时,不会直接去一个个找 RPM 包,而是先读取repodata里的元数据,这样就能快速找到需要的包,自动解决依赖问题。
2. 举个例子
假设手动下载了docker-ce、containerd.io等几个 RPM 包,直接放在/var/www/html/software/目录里:
没有
createrepo:dnf不知道这些包之间的依赖关系,也无法识别这是一个合法仓库,会提示找不到包。用了
createrepo:它会生成repodata目录,dnf读取这个目录后,就能知道 “这个仓库里有 Docker,安装它需要先装containerd.io”,然后自动帮你按顺序安装。
3. 工作原理
当你运行:
bash
运行
createrepo -v /var/www/html/software/它会在/var/www/html/software/下生成一个repodata目录,里面包含:
primary.xml.gz:所有 RPM 包的基本信息(名称、版本、依赖等)filelists.xml.gz:每个 RPM 包包含的文件列表other.xml.gz:其他辅助信息(比如包的描述、变更日志)repomd.xml:元数据的索引文件,dnf 会先读这个文件,再去解析其他元数据
![]()
7.查看验证文件是否存在
[root@localhost software]# ls containerd.io-2.2.1-1.el9.x86_64.rpm docker-ce-cli-29.1.4-1.el9.x86_64.rpm docker-buildx-plugin-0.30.1-1.el9.x86_64.rpm docker-ce-rootless-extras-29.1.4-1.el9.x86_64.rpm docker-ce-29.1.4-1.el9.x86_64.rpm docker-compose-plugin-5.0.1-1.el9.x86_64.rpmcache的作用:
这一步是为了加速后续的软件安装、搜索操作,避免每次都去远程仓库重新下载元数据。
1. 核心作用
下载并缓存仓库元数据:
dnf会从所有配置的.repo源里,下载仓库的元数据(比如包列表、依赖关系),然后把它们缓存到本地/var/cache/dnf/目录里。提升操作速度:之后你再执行
dnf install、dnf search等命令时,dnf直接读取本地缓存的元数据,不用每次都去远程仓库下载,速度会快很多。验证仓库配置:这也是一个验证仓库是否配置正确的好方法。如果执行
dnf makecache后没有报错,并且能看到各个仓库的元数据都下载成功(比如你输出里的docker、epel、software仓库),说明你的.repo文件配置是有效的。
2. 结合你的场景理解
在你搭建本地 HTTP 仓库的流程里,执行dnf makecache主要是为了:
让系统识别新配置的
software仓库:确保dnf能读取到你刚创建的本地仓库元数据。避免后续安装时重复下载:缓存元数据后,后续安装 Docker 时,
dnf直接从本地缓存获取依赖信息,不用每次都去 HTTP 仓库重新下载元数据。提前发现仓库配置问题:如果
software仓库的baseurl写错了,或者 HTTP 服务没启动,dnf makecache会立刻报错,这样你就能提前排查问题。
三,测试:
输入dnf info ... 命令可查看软件仓库
有以下结果即代表搭建共享仓库成功
[root@localhost yum.repos.d]# dnf info docker-ce.x86_64 Updating Subscription Management repositories. Unable to read consumer identity This system is not registered with an entitlement server. You can use subscription-manager to register. Last metadata expiration check: 0:05:42 ago on Thu 15 Jan 2026 08:22:17 AM EST. Available Packages Name : docker-ce Epoch : 3 Version : 29.1.4 Release : 1.el9 Architecture : x86_64 Size : 22 M Source : docker-ce-29.1.4-1.el9.src.rpm Repository : docker Summary : The open-source application container engine URL : https://www.docker.com License : Apache-2.0 Description : Docker is a product for you to build, ship and run any application as a : lightweight container. : : Docker containers are both hardware-agnostic and platform-agnostic. This means : they can run anywhere, from your laptop to the largest cloud compute instance : and everything in between - and they don't require you to use a particular : language, framework or packaging system. That makes them great building blocks : for deploying and scaling web apps, databases, and backend services without : depending on a particular stack or provider. Name : docker-ce Epoch : 3 Version : 29.1.4 Release : 1.el9 Architecture : x86_64 Size : 22 M Source : docker-ce-29.1.4-1.el9.src.rpm Repository : software Summary : The open-source application container engine URL : https://www.docker.com License : Apache-2.0 Description : Docker is a product for you to build, ship and run any application as a : lightweight container. : : Docker containers are both hardware-agnostic and platform-agnostic. This means : they can run anywhere, from your laptop to the largest cloud compute instance : and everything in between - and they don't require you to use a particular : language, framework or packaging system. That makes them great building blocks : for deploying and scaling web apps, databases, and backend services without : depending on a particular stack or provider.