本文主要以 Ubuntu 20.04 为例讲述如何在 Debian/Ubuntu 系列 Linux 发行版上通过上游官方仓库安装 Docker CE,其他发行版的安装方式也类似,不同地方主要在源地址配置中。
一、准备
2.1 准备 GPG 证书
将以下内容保存为名为 docker-gpg.txt
文件。
注:以下内容来源于仓库中 gpg 文件。
mQINBFit2ioBEADhWpZ8/wvZ6hUTiXOwQHXMAlaFHcPH9hAtr4F1y2+OYdbtMuthlqqwp028AqyY
+PRfVMtSYMbjuQuu5byyKR01BbqYhuS3jtqQmljZ/bJvXqnmiVXh38UuLa+z077PxyxQhu5Bbqnt
TPQMfiyqEiU+BKbq2WmANUKQf+1AmZY/IruOXbnqL4C1+gJ8vfmXQt99npCaxEjaNRVYfOS8Qcix
NzHUYnb6emjlANyEVlZzeqo7XKl7UrwV5inawTSzWNvtjEjj4nJL8NsLwscpLPQUhTQ+7BbQXAwA
meHCUTQIvvWXqw0Ncmhh4HgeQscQHYgOJjjDVfoY5MucvglbIgCqfzAHW9jxmRL4qbMZj+b1XoeP
Ethtku4bIQN1X5P07fNWzlgaRL5Z4POXDDZTlIQ/El58j9kp4bnWRCJW0lya+f8ocodovZZ+Doi+
fy4D5ZGrL4XEcIQP/Lv5uFyf+kQtl/94VFYVJOleAv8W92KdgDkhTcTDG7c0tIkVEKNUq48b3aQ6
4NOZQW7fVjfoKwEZdOqPE72Pa45jrZzvUFxSpdiNk2tZXYukHjlxxEgBdC/J3cMMNRE1F4NCA3Ap
fV1Y7/hTeOnmDuDYwr9/obA8t016Yljjq5rdkywPf4JF8mXUW5eCN1vAFHxeg9ZWemhBtQmGxXnw
9M+z6hWwc6ahmwARAQABtCtEb2NrZXIgUmVsZWFzZSAoQ0UgZGViKSA8ZG9ja2VyQGRvY2tlci5j
b20+iQI3BBMBCgAhBQJYrefAAhsvBQsJCAcDBRUKCQgLBRYCAwEAAh4BAheAAAoJEI2BgDwOv82I
sskP/iQZo68flDQmNvn8X5XTd6RRaUH33kXYXquT6NkHJciS7E2gTJmq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VrGyJivSkjhSGx1uCojsWfs
TAm11P7jsruIL61ZzMUVE2aM3Pmj5G+W9AcZ58Em+1WsVnAXdUR//bMmhyr8wL/G1YO1V3JEJTRd
xsSxdYa4deGBBY/Adpsw24jxhOJR+lsJpqIUeb999+R8euDhRHG9eFO7DRu6weatUJ6suupoDTRW
tr/4yGqedKxV3qQhNLSnaAzqW/1nA3iUB4k7kCaKZxhdhDbClf9P37qaRW467BLCVO/coL3yVm50
dwdrNtKpMBh3ZpbB1uJvgi9mXtyBOMJ3v8RZeDzFiG8HdCtg9RvIt/AIFoHRH3S+U79NT6i0KPzL
ImDfs8T7RlpyuMc4Ufs8ggyg9v3Ae6cN3eQyxcK3w0cbBwsh/nQNfsA6uu+9H7NhbehBMhYnpNZy
rHzCmzyXkauwRAqoCbGCNykTRwsur9gS41TQM8ssD1jFheOJf3hODnkKU+HKjvMROl1DK7zdmLdN
zA1cvtZH/nCC9KPj1z8QC47Sxx+dTZSx4ONAhwbS/LN3PoKtn8LPjY9NP9uDWI+TWYquS2U+KHDr
BDlsgozDbs/OjCxcpDzNmXpWQHEtHU7649OXHP7UeNST1mCUCH5qdank0V1iejF6/CfTFU4MfcrG
YT90qFF93M3v01BbxP+EIY2/9tiIPbrd
执行以下命令写入仓库 GPG 证书。
$ sudo mkdir -p /etc/apt/keyrings/
$ cat gpg.txt | sudo base64 -d > /etc/apt/keyrings/docker.gpg
2.2 写入更新源
在 /etc/apt/sources.list.d/docker.list
文件中写入以下内容:
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal stable
其中:
https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu
为仓库地址,根据所在地区可以选取不同的地址。以下是一些常用镜像仓库地址:
https://download.docker.com/linux/ubuntu
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu
https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu
https://mirrors.cloud.tencent.com/docker-ce/linux/ubuntu
https://mirror.sjtu.edu.cn/docker-ce/linux/ubuntu
https://mirrors.huaweicloud.com/docker-ce/linux/ubuntu
focal
为 Ubuntu 发行版代号。以下是 Ubuntu 主要发行代号和版本号对应关系:
focal
: Ubuntu 20.04jammy
: Ubuntu 22.04mantic
: Ubuntu 23.10noble
: Ubuntu 24.04
二、安装
在GPG证书和更新源配置都准备完成之后,执行以下命令更新仓库索引并安装 docker-ce
软件包:
$ sudo apt update
$ sudo apt install -y docker-ce
三、检查
通过以下命令查看安装的 Docker CE 版本号:
$ sudo docker version
示例:
root@Lemuria:~# docker version
Client: Docker Engine - Community
Version: 27.1.1
API version: 1.46
Go version: go1.21.12
Git commit: 6312585
Built: Tue Jul 23 19:56:56 2024
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 27.1.1
API version: 1.46 (minimum version 1.24)
Go version: go1.21.12
Git commit: cc13f95
Built: Tue Jul 23 19:56:56 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.7.19
GitCommit: 2bf793ef6dc9a18e00cb12efb64355c2c9d5eb41
runc:
Version: 1.7.19
GitCommit: v1.1.13-0-g58aa920
docker-init:
Version: 0.19.0
GitCommit: de40ad0
root@Lemuria:~#
四、安装 docker-compose
通过以下命令将 Docker CE 自带的 docker-compose 插件安装至系统目录。
$ sudo cp /usr/libexec/docker/cli-plugins/docker-compose /usr/bin/docker-compose
安装完成后通过以下命令查看版本号:
$ docker-compose version
示例:
root@Lemuria:~# docker-compose version
Docker Compose version v2.29.1