在CentOS9上安裝Docker的完整指南
1.準(zhǔn)備安裝環(huán)境
(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)更改主機(jī)名
[root@localhost ~]# hostnamectl set-hostname host1 [root@localhost ~]# bash
2. 永久配置 DNS
CentOS Stream 9 默認(rèn)用NetworkManager管理網(wǎng)絡(luò),需通過(guò)其工具永久配置 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
假設(shè)連接名稱(chēng)為ens160,執(zhí)行以下命令配置 DNS:
[root@host1 ~]# nmcli connection modify ens160 ipv4.dns "8.8.8.8,114.114.114.114" [root@host1 ~]# nmcli connection up ens160
驗(yàn)證 DNS 是否生效:
# 查看當(dāng)前生效的DNS [root@host1 ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 8.8.8.8 nameserver 114.114.114.114 # 再次測(cè)試域名解析 [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.更換國(guó)內(nèi)鏡像源(如阿里云)
(1)備份原有倉(cāng)庫(kù)配置
mkdir -p /etc/yum.repos.d/backup mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
(2)下載阿里云的 CentOS Stream 9 倉(cāng)庫(kù)配置
# 下載BaseOS倉(cāng)庫(kù)配置 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. 設(shè)置倉(cāng)庫(kù)
(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 上次元數(shù)據(jù)過(guò)期檢查:0:00:03 前,執(zhí)行于 2025年09月05日 星期五 16時(shí)25分24秒。 元數(shù)據(jù)緩存已建立。 [root@host1 ~]# dnf install -y yum-utils 上次元數(shù)據(jù)過(guò)期檢查:0:00:02 前,執(zhí)行于 2025年09月05日 星期五 16時(shí)26分45秒。 依賴(lài)關(guān)系解決。 ===================================================================================================== 軟件包 架構(gòu) 版本 倉(cāng)庫(kù) 大小 ===================================================================================================== 安裝: yum-utils noarch 4.3.0-22.el9 baseos 39 k 事務(wù)概要 ===================================================================================================== 安裝 1 軟件包 總下載:39 k 安裝大?。?3 k 下載軟件包: yum-utils-4.3.0-22.el9.noarch.rpm 113 kB/s | 39 kB 00:00 ----------------------------------------------------------------------------------------------------- 總計(jì) 111 kB/s | 39 kB 00:00 CentOS Stream 9 - BaseOS (Aliyun) 3.1 kB/s | 1.6 kB 00:00 導(dǎo)入 GPG 公鑰 0x8483C65D: Userid: "CentOS (CentOS Official Signing Key) <security@centos.org>" 指紋: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D 來(lái)自: https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official 導(dǎo)入公鑰成功 運(yùn)行事務(wù)檢查 事務(wù)檢查成功。 運(yùn)行事務(wù)測(cè)試 事務(wù)測(cè)試成功。 運(yùn)行事務(wù) 準(zhǔn)備中 : 1/1 安裝 : yum-utils-4.3.0-22.el9.noarch 1/1 運(yùn)行腳本: yum-utils-4.3.0-22.el9.noarch 1/1 驗(yàn)證 : yum-utils-4.3.0-22.el9.noarch 1/1 已安裝: yum-utils-4.3.0-22.el9.noarch 完畢!
(2)添加倉(cāng)庫(kù)
[root@host1 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 添加倉(cāng)庫(kù)自: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 上次元數(shù)據(jù)過(guò)期檢查:0:01:31 前,執(zhí)行于 2025年09月05日 星期五 16時(shí)47分16秒。 依賴(lài)關(guān)系解決。 ===================================================================================================== 軟件包 架構(gòu) 版本 倉(cāng)庫(kù) 大小 ===================================================================================================== 安裝: 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 安裝弱的依賴(lài): docker-ce-rootless-extras x86_64 28.4.0-1.el9 docker-ce-stable 3.4 M 事務(wù)概要 ===================================================================================================== 安裝 6 軟件包 總下載:108 M 安裝大?。?35 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 ----------------------------------------------------------------------------------------------------- 總計(jì) 7.8 MB/s | 108 MB 00:13 Docker CE Stable - x86_64 5.6 kB/s | 1.6 kB 00:00 導(dǎo)入 GPG 公鑰 0x621E9F35: Userid: "Docker Release (CE rpm) <docker@docker.com>" 指紋: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35 來(lái)自: https://mirrors.aliyun.com/docker-ce/linux/centos/gpg 確定嗎?[y/N]: y 導(dǎo)入公鑰成功 運(yùn)行事務(wù)檢查 事務(wù)檢查成功。 運(yùn)行事務(wù)測(cè)試 事務(wù)測(cè)試成功。 運(yùn)行事務(wù) 準(zhǔn)備中 : 1/1 安裝 : docker-buildx-plugin-0.27.0-1.el9.x86_64 1/6 運(yùn)行腳本: docker-buildx-plugin-0.27.0-1.el9.x86_64 1/6 安裝 : docker-compose-plugin-2.39.2-1.el9.x86_64 2/6 運(yùn)行腳本: 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 運(yùn)行腳本: 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 運(yùn)行腳本: 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 運(yùn)行腳本: 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 運(yùn)行腳本: docker-ce-3:28.4.0-1.el9.x86_64 6/6 驗(yàn)證 : containerd.io-1.7.27-3.1.el9.x86_64 1/6 驗(yàn)證 : docker-buildx-plugin-0.27.0-1.el9.x86_64 2/6 驗(yàn)證 : docker-ce-3:28.4.0-1.el9.x86_64 3/6 驗(yàn)證 : docker-ce-cli-1:28.4.0-1.el9.x86_64 4/6 驗(yàn)證 : docker-ce-rootless-extras-28.4.0-1.el9.x86_64 5/6 驗(yàn)證 : 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 已安裝的軟件包 上次元數(shù)據(jù)過(guò)期檢查:0:05:43 前,執(zhí)行于 2025年09月05日 星期五 16時(shí)47分16秒。 可安裝的軟件包
選擇版本安裝
yum install docker-ce-版本號(hào) #后面一樣操作
6. 啟動(dòng)并測(cè)試
[root@host1 ~]# systemctl start docker [root@host1 ~]# docker run hello-world #運(yùn)行 hello-world 鏡像驗(yàn)證 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
報(bào)錯(cuò)修改:
(1)檢查網(wǎng)絡(luò)后發(fā)現(xiàn)自己的阿里鏡像加速器用不了了,現(xiàn)在換成華為鏡像加速器
[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)驗(yàn)證 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/太棒了!從輸出結(jié)果可以確認(rèn),Docker 已經(jīng)完全正常工作了!
“Hello from Docker!” 的歡迎信息表明:
- Docker 客戶(hù)端與守護(hù)進(jìn)程(daemon)通信正常;
- 鏡像加速器配置生效,成功拉取了
hello-world鏡像; - 容器能夠正常創(chuàng)建、運(yùn)行并輸出內(nèi)容。
7. 鏡像加速配置操作示例(阿里鏡像加速配置)
(1)創(chuàng)建配置文件
sudo mkdir -p /etc/docker
(2)寫(xiě)入阿里云加速配置
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://jnh8ca4k.mirror.aliyuncs.com"]
}
EOF
(3)重啟服務(wù)生效
sudo systemctl daemon-reload sudo systemctl restart docker
8.配置 Docker 開(kāi)機(jī)自動(dòng)啟動(dòng)
[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.
關(guān)鍵注意事項(xiàng)
生產(chǎn)環(huán)境操作前建議創(chuàng)建系統(tǒng)快照
避免直接使用sudo rm -rf清理目錄(除非確認(rèn)無(wú)重要數(shù)據(jù))
定期執(zhí)行dnf update保持組件更新
以上就是在CentOS9上安裝Docker的完整指南的詳細(xì)內(nèi)容,更多關(guān)于CentOS9安裝Docker的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Docker Overlay2磁盤(pán)空間占用過(guò)大清理的方法實(shí)現(xiàn)
隨著業(yè)務(wù)量的不斷增大,容器的不斷啟動(dòng),往往會(huì)出現(xiàn)磁盤(pán)空間不足,本文主要介紹了Docker Overlay2磁盤(pán)空間占用過(guò)大清理的方法實(shí)現(xiàn),感興趣的可以了解一下2022-03-03
分享Ubuntu19無(wú)法安裝docker源問(wèn)題
這篇文章主要介紹了Ubuntu19無(wú)法安裝docker源問(wèn)題,本文通過(guò)實(shí)例代碼給大家講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
利用Docker搭建本地https環(huán)境的完整步驟
這篇文章主要給大家介紹了關(guān)于如何利用Docker搭建本地https環(huán)境的完整步驟,文中通過(guò)示例代碼將實(shí)現(xiàn)的步驟介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2018-04-04
Docker拉取鏡像失敗解決(connect: connection refused)
最近遇到Docker拉取centos鏡像時(shí)報(bào)錯(cuò),本文主要介紹了Docker拉取鏡像失敗解決(connect: connection refused),具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
用Docker搭建nextcloud個(gè)人網(wǎng)盤(pán)教程
大家好,本篇文章主要講的是用Docker搭建nextcloud個(gè)人網(wǎng)盤(pán)教程,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下2021-12-12
spring-boot構(gòu)建docker鏡像上傳倉(cāng)庫(kù)的示例教程
這篇文章主要介紹了spring-boot構(gòu)建docker鏡像上傳倉(cāng)庫(kù),受限創(chuàng)建一個(gè)簡(jiǎn)單spring-boot-web項(xiàng)目,查看鏡像上傳倉(cāng)庫(kù)這時(shí)候有兩種解決方案,對(duì)docker鏡像上傳倉(cāng)庫(kù)相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2022-12-12
Deepin使用docker安裝mysql數(shù)據(jù)庫(kù)過(guò)程詳解
這篇文章主要介紹了Deepin使用docker安裝mysql數(shù)據(jù)庫(kù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

