一、环境准备
系统准备
| 发行版 | 支持版本 |
|---|---|
| Ubuntu | 20.04 / 22.04 / 24.04 LTS |
| Debian | 11 (Bullseye) / 12 (Bookworm) |
| CentOS | 7 / 8 / 9 Stream |
| RHEL | 8 / 9 |
| Fedora | 39 / 40 |
| openSUSE / SLES | 15.x |
| Raspberry Pi OS | Bullseye / Bookworm(ARM) |
⚠️ 注意:CentOS 7 已于 2024 年 6 月 30 日停止官方支持,建议迁移到 CentOS Stream 9 或 Rocky Linux 9。
最低硬件配置
| 资源 | 最低要求 | 推荐配置 |
|---|---|---|
| CPU | 1 核 | 2 核+ |
| 内存 | 512 MB | 2 GB+ |
| 磁盘 | 5 GB(系统盘) | 20 GB+(含镜像存储) |
| 内核版本 | Linux 3.10+ | Linux 5.x+ |
| 架构 | x86_64 / ARM64 / ARMv7 | --- |
检查当前系统信息
# 查看发行版名称和版本号
cat /etc/os-release
# 查看内核版本,必须 >= 3.10,否则 Docker 无法运行
uname -r
# 查看系统架构(x86_64 / aarch64 / armv7l)
uname -m
# 查看当前可用内存(单位 MB)
free -m
# 查看磁盘剩余空间
df -h /
[root@docker-nzl home]# cat /etc/os-release
NAME="Rocky Linux"
VERSION="9.3 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.3"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.3 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.3"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.3"
[root@docker-nzl home]# uname -r
5.14.0-362.8.1.el9_3.x86_64
[root@docker-nzl home]# uname -m
x86_64
[root@docker-nzl home]# free -m
total used free shared buff/cache available
Mem: 7673 514 7168 10 242 7158
Swap: 2047 0 2047
[root@docker-nzl home]# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 28G 4.6G 24G 17% /
检查是否已经安装了 Docker
# 检查 docker 命令是否存在
which docker
# 如果存在,查看当前版本
docker --version
# 查看 Docker 服务状态
systemctl status docker
# 查看系统里所有 docker 相关的包(Ubuntu/Debian)
dpkg -l | grep -i docker
# 查看系统里所有 docker 相关的包(CentOS/RHEL)
rpm -qa | grep -i docker
二、两种方法安装详细教程
1. 官方脚本一键安装(测试和个人推荐)
这是 Docker 官方提供的懒人安装方式,脚本托管在 get.docker.com,每次执行都会拉取最新的稳定版。
1.1 更新系统包并安装curl
# CentOS / RHEL / Fedora 系
yum update -y # 更新所有已安装的包
yum install -y curl # 安装 curl
1.2 执行安装脚本
sh install-docker.sh
脚本执行期间会自动完成以下工作:
- 识别你的 Linux 发行版和版本
- 添加 Docker 官方软件源
- 安装 Docker Engine、containerd、Docker CLI
- 启动 Docker 服务并设置开机自启
整个过程大约 2~5 分钟,取决于你的网络速度。
国内服务器网速慢怎么办? 使用阿里云镜像加速:
指定使用阿里云镜像站下载,速度会快很多
sudo sh install-docker.sh --mirror Aliyun
1.3 设置 Docker 开机自启
# 启用 Docker 服务,设置为开机自动启动
systemctl enable docker
# 立即启动 Docker 服务(如果安装脚本没有自动启动的话)
systemctl start docker
# 确认服务正在运行(看到 Active: active (running) 就对了)
systemctl status docker
2. YUM 软件源头手动安装
2.1 安装 yum-utils 工具
# yum-utils 提供了 yum-config-manager 命令
# 用于方便地管理 yum 软件源配置
yum install -y yum-utils
2.2 添加 Docker 软件源
# 添加 Docker 官方 YUM 仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
国内访问慢?换成阿里云镜像:
sudo yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2.3 安装 Docker Engine
# 安装最新版本
# --nobest:允许安装非最优解,避免依赖冲突导致安装失败(CentOS 8+ 推荐加)
sudo yum install -y docker-ce docker-ce-cli containerd.io \
docker-buildx-plugin docker-compose-plugin
# 启动并设置开机自启(CentOS 安装完不会自动启动,需要手动 start)
sudo systemctl enable --now docker
指定版本:
# 列出可用版本
yum list docker-ce --showduplicates | sort -r | head -20
# 安装指定版本,例如 docker-ce-26.1.4
sudo yum install -y docker-ce-<VERSION> docker-ce-cli-<VERSION> containerd.io