在CentOS9上安装Docker的完整指南

 更新时间:2025年10月13日 10:23:13   作者:Lin_Aries_0421  
这篇文章主要为大家详细介绍了在CentOS9上安装Docker的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下

1.准备安装环境

(1)禁用防火墙与SELinux

[root@localhost ~]# systemctl disable --now firewalld.service 
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@localhost ~]# sudo setenforce 0

(2)更改主机名

[root@localhost ~]# hostnamectl set-hostname host1
[root@localhost ~]# bash

2. 永久配置 DNS

CentOS Stream 9 默认用NetworkManager管理网络,需通过其工具永久配置 DNS:

[root@host1 ~]# nmcli connection show
NAME    UUID                                  TYPE      DEVICE 
ens160  19689312-b1bb-3a43-8c2d-d316680ab4a5  ethernet  ens160 
lo      86df2843-123d-41cc-b6ac-1cf41ce141e8  loopback  lo 

假设连接名称为ens160,执行以下命令配置 DNS:

[root@host1 ~]# nmcli connection modify ens160 ipv4.dns "8.8.8.8,114.114.114.114"
[root@host1 ~]# nmcli connection up ens160

验证 DNS 是否生效:

# 查看当前生效的DNS
[root@host1 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 114.114.114.114
# 再次测试域名解析
[root@host1 ~]# nslookup mirrors.centos.org
Server:8.8.8.8
Address:8.8.8.8#53

Non-authoritative answer:
mirrors.centos.orgcanonical name = mirrors.fedoraproject.org.
mirrors.fedoraproject.orgcanonical name = wildcard.fedoraproject.org.
Name:wildcard.fedoraproject.org
Address: 38.145.32.21
Name:wildcard.fedoraproject.org
Address: 152.2.23.103
Name:wildcard.fedoraproject.org
Address: 140.211.169.196
Name:wildcard.fedoraproject.org
Address: 8.43.85.67
Name:wildcard.fedoraproject.org
Address: 34.211.44.206
Name:wildcard.fedoraproject.org
Address: 8.43.85.73
Name:wildcard.fedoraproject.org
Address: 152.2.23.104
Name:wildcard.fedoraproject.org
Address: 67.219.144.68
Name:wildcard.fedoraproject.org
Address: 38.145.32.20
Name:wildcard.fedoraproject.org
Address: 2620:52:6:1121:bead:cafe:feed:fed5
Name:wildcard.fedoraproject.org
Address: 2606:f640:6000:651::7
Name:wildcard.fedoraproject.org
Address: 2606:f640:6000:651::8
Name:wildcard.fedoraproject.org
Address: 2620:52:3:1:dead:beef:cafe:fed6
Name:wildcard.fedoraproject.org
Address: 2605:bc80:3010:600:dead:beef:cafe:fed9
Name:wildcard.fedoraproject.org
Address: 2600:1f14:fad:5c02:5270:306c:35d6:1f81
Name:wildcard.fedoraproject.org
Address: 2620:52:3:1:dead:beef:cafe:fed7
Name:wildcard.fedoraproject.org
Address: 2604:1580:fe00:0:dead:beef:cafe:fed1
Name:wildcard.fedoraproject.org
Address: 2620:52:6:1121:bead:cafe:feed:fed6

3.更换国内镜像源(如阿里云)

(1)备份原有仓库配置

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

(2)下载阿里云的 CentOS Stream 9 仓库配置

# 下载BaseOS仓库配置
curl -o /etc/yum.repos.d/CentOS-Stream-BaseOS.repo https://mirrors.aliyun.com/repo/Centos-stream-9.repo

(3)清理缓存并重建

yum clean all
yum makecache  # 生成新的缓存

4. 设置仓库

(1)安装 yum-utils 包

[root@host1 ~]# dnf makecache
CentOS Stream 9 - BaseOS (Aliyun)                                    3.0 MB/s | 8.8 MB     00:02    
CentOS Stream 9 - AppStream (Aliyun)                                 3.8 MB/s |  25 MB     00:06    
上次元数据过期检查:0:00:03 前,执行于 2025年09月05日 星期五 16时25分24秒。
元数据缓存已建立。
[root@host1 ~]# dnf install -y yum-utils
上次元数据过期检查:0:00:02 前,执行于 2025年09月05日 星期五 16时26分45秒。
依赖关系解决。
=====================================================================================================
 软件包                  架构                 版本                        仓库                  大小
=====================================================================================================
安装:
 yum-utils               noarch               4.3.0-22.el9                baseos                39 k

事务概要
=====================================================================================================
安装  1 软件包

总下载:39 k
安装大小:23 k
下载软件包:
yum-utils-4.3.0-22.el9.noarch.rpm                                    113 kB/s |  39 kB     00:00    
-----------------------------------------------------------------------------------------------------
总计                                                                 111 kB/s |  39 kB     00:00     
CentOS Stream 9 - BaseOS (Aliyun)                                    3.1 kB/s | 1.6 kB     00:00    
导入 GPG 公钥 0x8483C65D:
 Userid: "CentOS (CentOS Official Signing Key) <security@centos.org>"
 指纹: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 来自: https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
导入公钥成功
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                      1/1 
  安装    : yum-utils-4.3.0-22.el9.noarch                                                        1/1 
  运行脚本: yum-utils-4.3.0-22.el9.noarch                                                        1/1 
  验证    : yum-utils-4.3.0-22.el9.noarch                                                        1/1 

已安装:
  yum-utils-4.3.0-22.el9.noarch                                                                      

完毕!

(2)添加仓库

[root@host1 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
添加仓库自:https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

5. 安装 Docker Engine

[root@host1 ~]# yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
上次元数据过期检查:0:01:31 前,执行于 2025年09月05日 星期五 16时47分16秒。
依赖关系解决。
=====================================================================================================
 软件包                           架构          版本                   仓库                     大小
=====================================================================================================
安装:
 containerd.io                    x86_64        1.7.27-3.1.el9         docker-ce-stable         44 M
 docker-buildx-plugin             x86_64        0.27.0-1.el9           docker-ce-stable         17 M
 docker-ce                        x86_64        3:28.4.0-1.el9         docker-ce-stable         21 M
 docker-ce-cli                    x86_64        1:28.4.0-1.el9         docker-ce-stable        8.6 M
 docker-compose-plugin            x86_64        2.39.2-1.el9           docker-ce-stable         15 M
安装弱的依赖:
 docker-ce-rootless-extras        x86_64        28.4.0-1.el9           docker-ce-stable        3.4 M

事务概要
=====================================================================================================
安装  6 软件包

总下载:108 M
安装大小:435 M
确定吗?[y/N]: y
下载软件包:
(1/6): docker-buildx-plugin-0.27.0-1.el9.x86_64.rpm                  2.4 MB/s |  17 MB     00:06    
(2/6): docker-ce-28.4.0-1.el9.x86_64.rpm                             2.8 MB/s |  21 MB     00:07    
(3/6): docker-ce-rootless-extras-28.4.0-1.el9.x86_64.rpm             2.4 MB/s | 3.4 MB     00:01    
(4/6): docker-ce-cli-28.4.0-1.el9.x86_64.rpm                         2.7 MB/s | 8.6 MB     00:03    
(5/6): docker-compose-plugin-2.39.2-1.el9.x86_64.rpm                 3.7 MB/s |  15 MB     00:04    
(6/6): containerd.io-1.7.27-3.1.el9.x86_64.rpm                       3.2 MB/s |  44 MB     00:13    
-----------------------------------------------------------------------------------------------------
总计                                                                 7.8 MB/s | 108 MB     00:13     
Docker CE Stable - x86_64                                            5.6 kB/s | 1.6 kB     00:00    
导入 GPG 公钥 0x621E9F35:
 Userid: "Docker Release (CE rpm) <docker@docker.com>"
 指纹: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
 来自: https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
确定吗?[y/N]: y
导入公钥成功
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                      1/1 
  安装    : docker-buildx-plugin-0.27.0-1.el9.x86_64                                             1/6 
  运行脚本: docker-buildx-plugin-0.27.0-1.el9.x86_64                                             1/6 
  安装    : docker-compose-plugin-2.39.2-1.el9.x86_64                                            2/6 
  运行脚本: docker-compose-plugin-2.39.2-1.el9.x86_64                                            2/6 
  安装    : docker-ce-cli-1:28.4.0-1.el9.x86_64                                                  3/6 
  运行脚本: docker-ce-cli-1:28.4.0-1.el9.x86_64                                                  3/6 
  安装    : containerd.io-1.7.27-3.1.el9.x86_64                                                  4/6 
  运行脚本: containerd.io-1.7.27-3.1.el9.x86_64                                                  4/6 
  安装    : docker-ce-rootless-extras-28.4.0-1.el9.x86_64                                        5/6 
  运行脚本: docker-ce-rootless-extras-28.4.0-1.el9.x86_64                                        5/6 
  安装    : docker-ce-3:28.4.0-1.el9.x86_64                                                      6/6 
  运行脚本: docker-ce-3:28.4.0-1.el9.x86_64                                                      6/6 
  验证    : containerd.io-1.7.27-3.1.el9.x86_64                                                  1/6 
  验证    : docker-buildx-plugin-0.27.0-1.el9.x86_64                                             2/6 
  验证    : docker-ce-3:28.4.0-1.el9.x86_64                                                      3/6 
  验证    : docker-ce-cli-1:28.4.0-1.el9.x86_64                                                  4/6 
  验证    : docker-ce-rootless-extras-28.4.0-1.el9.x86_64                                        5/6 
  验证    : docker-compose-plugin-2.39.2-1.el9.x86_64                                            6/6 

已安装:
  containerd.io-1.7.27-3.1.el9.x86_64                 docker-buildx-plugin-0.27.0-1.el9.x86_64       
  docker-ce-3:28.4.0-1.el9.x86_64                     docker-ce-cli-1:28.4.0-1.el9.x86_64            
  docker-ce-rootless-extras-28.4.0-1.el9.x86_64       docker-compose-plugin-2.39.2-1.el9.x86_64      

完毕!

查看版本:

[root@host1 ~]# docker --version 
Docker version 28.4.0, build d8eb465

列出所有版本

[root@host1 ~]# yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64               3:28.4.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.4.0-1.el9                  @docker-ce-stable
docker-ce.x86_64               3:28.3.3-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.3.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.3.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.3.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.2.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.2.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.2.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.1.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.1.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.0.4-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.0.3-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.0.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.0.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:28.0.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.5.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.5.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.4.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.4.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.3.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.3.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.2.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.2.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.1.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.1.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.1.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.0.3-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.0.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:27.0.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:26.1.4-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:26.1.3-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:26.1.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:26.1.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:26.1.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:26.0.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:26.0.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:26.0.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:25.0.5-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:25.0.4-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:25.0.3-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:25.0.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:25.0.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:25.0.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.9-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.8-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.7-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.6-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.5-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.4-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.3-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:24.0.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:23.0.6-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:23.0.5-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:23.0.4-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:23.0.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:23.0.1-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:23.0.0-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:20.10.24-3.el9                docker-ce-stable 
docker-ce.x86_64               3:20.10.23-3.el9                docker-ce-stable 
docker-ce.x86_64               3:20.10.22-3.el9                docker-ce-stable 
docker-ce.x86_64               3:20.10.21-3.el9                docker-ce-stable 
docker-ce.x86_64               3:20.10.20-3.el9                docker-ce-stable 
docker-ce.x86_64               3:20.10.19-3.el9                docker-ce-stable 
docker-ce.x86_64               3:20.10.18-3.el9                docker-ce-stable 
docker-ce.x86_64               3:20.10.17-3.el9                docker-ce-stable 
docker-ce.x86_64               3:20.10.16-3.el9                docker-ce-stable 
docker-ce.x86_64               3:20.10.15-3.el9                docker-ce-stable 
已安装的软件包
上次元数据过期检查:0:05:43 前,执行于 2025年09月05日 星期五 16时47分16秒。
可安装的软件包

选择版本安装

yum install docker-ce-版本号 #后面一样操作

6. 启动并测试

[root@host1 ~]# systemctl start docker
[root@host1 ~]# docker run hello-world    #运行 hello-world  镜像验证
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded

Run 'docker run --help' for more information

报错修改:

(1)检查网络后发现自己的阿里镜像加速器用不了了,现在换成华为镜像加速器

[root@host1 ~]# vi /etc/docker/daemon.json
[root@host1 ~]# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://9fbd5949cbc94e4a9581e33b9077c811.mirror.swr.myhuaweicloud.com"]
}

(2)重启 Docker 生效

[root@host1 ~]# sudo systemctl daemon-reload
[root@host1 ~]# sudo systemctl restart docker

(3)验证 Docker 是否完全正常工作:

[root@host1 ~]# docker pull hello-world     #可以不用这步操作
Using default tag: latest
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete 
Digest: sha256:a0dfb02aac212703bfcb339d77d47ec32c8706ff250850ecc0e19c8737b18567
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@host1 ~]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

太棒了!从输出结果可以确认,Docker 已经完全正常工作了!

“Hello from Docker!” 的欢迎信息表明:

  • Docker 客户端与守护进程(daemon)通信正常;
  • 镜像加速器配置生效,成功拉取了 hello-world 镜像;
  • 容器能够正常创建、运行并输出内容。

7. 镜像加速配置操作示例(阿里镜像加速配置)

(1)创建配置文件

sudo mkdir -p /etc/docker

(2)写入阿里云加速配置

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://jnh8ca4k.mirror.aliyuncs.com"]
}
EOF

(3)重启服务生效

sudo systemctl daemon-reload
sudo systemctl restart docker

8.配置 Docker 开机自动启动

[root@host1 ~]# sudo systemctl enable docker.service
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@host1 ~]# sudo systemctl enable containerd.service
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /usr/lib/systemd/system/containerd.service.

关键注意事项

生产环境操作前建议创建系统快照

避免直接使用sudo rm -rf清理目录(除非确认无重要数据)

定期执行dnf update保持组件更新

以上就是在CentOS9上安装Docker的完整指南的详细内容,更多关于CentOS9安装Docker的资料请关注脚本之家其它相关文章!

相关文章

  • Docker Overlay2磁盘空间占用过大清理的方法实现

    Docker Overlay2磁盘空间占用过大清理的方法实现

    随着业务量的不断增大,容器的不断启动,往往会出现磁盘空间不足,本文主要介绍了Docker Overlay2磁盘空间占用过大清理的方法实现,感兴趣的可以了解一下
    2022-03-03
  • 分享Ubuntu19无法安装docker源问题

    分享Ubuntu19无法安装docker源问题

    这篇文章主要介绍了Ubuntu19无法安装docker源问题,本文通过实例代码给大家讲解的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11
  • Docker 网络模式及配置方式

    Docker 网络模式及配置方式

    这篇文章主要介绍了Docker 网络模式及配置方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Docker服务迁移的实现

    Docker服务迁移的实现

    本文详细介绍了如何备份和迁移Docker服务数据,包括停止Docker服务、备份Docker数据和DockerCompose文件,使用SCP或Rsync传输备份文件,以及在新服务器上恢复Docker数据和设置权限,文中提供了详细的命令和步骤,确保数据迁移过程顺利进行
    2024-10-10
  • 如何隔离docker容器中的用户的方法

    如何隔离docker容器中的用户的方法

    这篇文章主要介绍了如何隔离docker容器中的用户的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 利用Docker搭建本地https环境的完整步骤

    利用Docker搭建本地https环境的完整步骤

    这篇文章主要给大家介绍了关于如何利用Docker搭建本地https环境的完整步骤,文中通过示例代码将实现的步骤介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2018-04-04
  • Docker拉取镜像失败解决(connect: connection refused)

    Docker拉取镜像失败解决(connect: connection refused)

    最近遇到Docker拉取centos镜像时报错,本文主要介绍了Docker拉取镜像失败解决(connect: connection refused),具有一定的参考价值,感兴趣的可以了解一下
    2024-07-07
  • 用Docker搭建nextcloud个人网盘教程

    用Docker搭建nextcloud个人网盘教程

    大家好,本篇文章主要讲的是用Docker搭建nextcloud个人网盘教程,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2021-12-12
  • spring-boot构建docker镜像上传仓库的示例教程

    spring-boot构建docker镜像上传仓库的示例教程

    这篇文章主要介绍了spring-boot构建docker镜像上传仓库,受限创建一个简单spring-boot-web项目,查看镜像上传仓库这时候有两种解决方案,对docker镜像上传仓库相关知识感兴趣的朋友跟随小编一起看看吧
    2022-12-12
  • Deepin使用docker安装mysql数据库过程详解

    Deepin使用docker安装mysql数据库过程详解

    这篇文章主要介绍了Deepin使用docker安装mysql数据库过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06

最新评论