Linux 安装 Docker Engine 与 Docker Compose

提供 RPM 与 Debian/Ubuntu 安装步骤、国内软件源和镜像配置,以及 Compose 插件与独立版安装方式。

适用范围

以下分别提供 RPM 与 Debian/Ubuntu 的安装步骤,按自己的系统选择一组执行。

1
cat /etc/os-release

已有 Docker 实例时,先检查官方文档列出的冲突包与已有源文件。不要删除数据目录;已存在的 docker.list 或其他 Docker 源条目需要核对,避免与新建的 docker.sources 重复。

RPM 系统:CentOS Stream

以下用于 Docker 当前支持的 CentOS Stream 9/10:

1
2
3
4
sudo dnf install -y curl \
&& sudo curl -fsSL https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo \
&& sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin \
&& sudo systemctl enable --now docker

RPM 系统:RHEL

RHEL 使用对应的 RHEL 软件源,不混用 CentOS 源。以下按官方支持的 RHEL 8/9/10 安装:

1
2
3
4
sudo dnf install -y curl \
&& sudo curl -fsSL https://download.docker.com/linux/rhel/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo \
&& sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin \
&& sudo systemctl enable --now docker

Rocky Linux 等兼容发行版需按自己的版本核对对应仓库。

Debian 软件源

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/debian
Suites: $(. /etc/os-release && echo "$VERSION_CODENAME")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF

Ubuntu 软件源

Ubuntu 用户执行这一组,不要同时添加 Debian 源:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF

可选:软件包下载使用清华源

软件包下载源与 Docker Hub 镜像加速是两件事。完成所属系统的软件源配置后,需要国内软件包源时执行对应命令。

RPM 系统:

1
sudo sed -i 's|https://download.docker.com|https://mirrors.tuna.tsinghua.edu.cn/docker-ce|g' /etc/yum.repos.d/docker-ce.repo

Debian/Ubuntu:

1
sudo sed -i 's|https://download.docker.com|https://mirrors.tuna.tsinghua.edu.cn/docker-ce|g' /etc/apt/sources.list.d/docker.sources

RPM 已安装时,替换源用于后续更新;也可以在上面下载 .repo 后、安装软件包前执行替换命令。

Debian/Ubuntu 安装与启动

完成所属系统的软件源配置后执行:

1
2
3
sudo apt-get update \
&& sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin \
&& sudo systemctl enable --now docker

验证安装

1
2
sudo docker run --rm hello-world
docker compose version

可选:配置 Docker Hub 镜像加速

下面使用 USTC 文档中的地址作为示例,使用前按 镜像站说明 确认适用于自己的网络;有其他可用镜像时替换地址即可。

使用 Python 3 自动合并 registry-mirrors 字段,保留已有的其他 Docker 配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sudo python3 - <<'PY'
import json
from pathlib import Path

path = Path('/etc/docker/daemon.json')
config = json.loads(path.read_text()) if path.exists() else {}
config['registry-mirrors'] = ['https://docker.mirrors.ustc.edu.cn']
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(config, indent=2) + '\n')
PY

配置完成后重启并查看结果:

1
2
sudo systemctl restart docker
sudo docker info

可选:一键安装 Compose 独立版

需要保留 docker-compose 命令时,可自动下载最新独立版,无需手工填写版本:

1
2
3
sudo curl -fL "https://github.com/docker/compose/releases/latest/download/docker-compose-linux-$(uname -m)" -o /usr/local/bin/docker-compose \
&& sudo chmod +x /usr/local/bin/docker-compose \
&& docker-compose version

上面的 Compose 插件使用 docker compose,独立版使用 docker-compose,按自己的调用方式选择。

Compose V1:旧环境安装记录

以下命令安装固定版本 Compose 1.29.2,适用于需要该旧版本的 x86_64 环境。Compose V1 已结束维护,新安装使用上面的方式;两种独立版安装方法二选一,都写入 /usr/local/bin/docker-compose:

1
2
sudo curl -fL -o /usr/local/bin/docker-compose https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64 \
&& sudo chmod +x /usr/local/bin/docker-compose

参考:CentOS 官方步骤、RHEL 官方步骤、Debian 官方步骤、Ubuntu 官方步骤、清华 Docker CE 源、Compose 独立版。

延伸阅读

使用 Hugo 构建
主题 Stack 由 Jimmy 设计