Ubuntu安装Docker引擎指南

一、介绍

本指南以Docker官方文档《在Ubuntu上安装Docker引擎》为基础整理,并演示在Unbuntu中安装Docker的全过程。

二、Ubuntu安装Docker引擎指南

以下是在Ubuntu上安装Docker引擎的详细方案:


安装前的准备工作

1. 检查系统要求

  • 支持的Ubuntu版本
    • Ubuntu Oracular 24.10
    • Ubuntu Noble 24.04 (LTS)
    • Ubuntu Jammy 22.04 (LTS)
    • Ubuntu Focal 20.04 (LTS)
  • 架构支持:x86_64 (或 amd64)、armhf、arm64、s390x、and ppc64le (ppc64el)。

2. 卸载旧版本

运行以下命令移除冲突的软件包:

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
  • 若系统中没有这些包,会提示未安装,可忽略。

3. 防火墙注意事项

  • 参考官方文档


安装方法(推荐使用APT仓库)

1. 添加Docker官方APT仓库

# 安装依赖工具
sudo apt-get update
sudo apt-get install ca-certificates curl

# 添加Docker GPG密钥
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

# 添加APT源
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 更新软件源
sudo apt-get update

2. 安装Docker引擎

  • 安装最新版本
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    
  • 安装特定版本

要安装特定版本的Docker Engine,请首先在存储库中列出可用版本:

# 列出可用版本
apt-cache madison docker-ce | awk '{ print $3 }'

5:28.0.4-1~ubuntu.24.04~noble
5:28.0.3-1~ubuntu.24.04~noble
...

选择所需版本并安装:

# 选择版本并安装(替换为实际版本号)
VERSION_STRING=5:28.0.4-1~ubuntu.24.04~noble
sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin

3. 验证安装

运行测试容器:

sudo docker run hello-world
  • 若输出确认信息,则安装成功。

其他安装方式

1. 手动安装DEB包

2. 使用便利脚本(仅限测试环境)

请参考官方文档。


按照以上步骤操作,即可在Ubuntu系统上顺利安装和启动Docker引擎。

三、安装过程示例

卸载旧版本

官方文档

在安装 Docker Engine 之前,您需要卸载所有冲突的软件包。

您的 Linux 发行版可能提供了非官方的 Docker 软件包,这些软件包可能与 Docker 提供的官方软件包冲突。在安装官方版本的 Docker Engine 之前,必须卸载这些软件包。

需要卸载的非官方软件包包括:

  • docker.io
  • docker-compose
  • docker-compose-v2
  • docker-doc
  • podman-docker

此外,Docker Engine 依赖 containerdrunc。Docker Engine 将这些依赖项捆绑为一个组合包 containerd.io。如果之前安装过 containerdrunc,请卸载它们以避免与 Docker Engine 捆绑的版本冲突。

运行以下命令卸载所有冲突的软件包:

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

apt-get 可能会报告未安装这些软件包。

存储在 /var/lib/docker/ 中的镜像、容器、卷和网络不会在卸载 Docker 时自动删除。如果您希望进行全新安装,并清理所有现有数据,请阅读 卸载 Docker Engine 部分。

执行结果


developer@developer:/$ for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
软件包 docker.io 未安装,所以不会被卸载
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
软件包 docker-doc 未安装,所以不会被卸载
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
软件包 docker-compose 未安装,所以不会被卸载
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
软件包 docker-compose-v2 未安装,所以不会被卸载
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
软件包 podman-docker 未安装,所以不会被卸载
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
软件包 containerd 未安装,所以不会被卸载
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
软件包 runc 未安装,所以不会被卸载
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。

使用 apt 仓库安装

首次在新主机上安装 Docker Engine 之前,您需要设置 Docker 的 apt 仓库。之后,您可以从仓库安装和更新 Docker。

1. 设置 Docker 的 apt 仓库

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install 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

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
第一次执行命令:警告

执行结果,两个警告:

W: 无法下载 https://download.docker.com/linux/ubuntu/dists/jammy/InRelease Could not handshake: Error in the pull function. [IP: 3.170.229.111 443]
W: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。


# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install 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

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
命中:1 http://repo.huaweicloud.com/ubuntu jammy InRelease
获取:2 http://repo.huaweicloud.com/ubuntu jammy-updates InRelease [128 kB]
获取:3 http://repo.huaweicloud.com/ubuntu jammy-backports InRelease [127 kB]
获取:4 http://repo.huaweicloud.com/ubuntu jammy-security InRelease [129 kB]
获取:5 http://repo.huaweicloud.com/ubuntu jammy-updates/main amd64 Packages [2,461 kB]
获取:6 http://repo.huaweicloud.com/ubuntu jammy-updates/main Translation-en [404 kB]                
获取:7 http://repo.huaweicloud.com/ubuntu jammy-updates/main amd64 c-n-f Metadata [18.5 kB]                         
获取:8 http://repo.huaweicloud.com/ubuntu jammy-updates/restricted amd64 Packages [3,259 kB]        
获取:9 http://repo.huaweicloud.com/ubuntu jammy-updates/restricted Translation-en [577 kB]                        
获取:10 http://repo.huaweicloud.com/ubuntu jammy-updates/restricted amd64 c-n-f Metadata [676 B]                    
获取:11 http://repo.huaweicloud.com/ubuntu jammy-updates/universe amd64 Packages [1,198 kB]
获取:12 http://repo.huaweicloud.com/ubuntu jammy-updates/universe Translation-en [295 kB]
获取:13 http://repo.huaweicloud.com/ubuntu jammy-updates/universe amd64 c-n-f Metadata [28.7 kB]
获取:14 http://repo.huaweicloud.com/ubuntu jammy-updates/multiverse amd64 Packages [46.5 kB]
获取:15 http://repo.huaweicloud.com/ubuntu jammy-updates/multiverse Translation-en [11.8 kB]
获取:16 http://repo.huaweicloud.com/ubuntu jammy-updates/multiverse amd64 c-n-f Metadata [592 B]
获取:17 http://repo.huaweicloud.com/ubuntu jammy-backports/main amd64 Packages [68.4 kB]
获取:18 http://repo.huaweicloud.com/ubuntu jammy-backports/main Translation-en [11.1 kB]
获取:19 http://repo.huaweicloud.com/ubuntu jammy-backports/main amd64 c-n-f Metadata [392 B]
获取:20 http://repo.huaweicloud.com/ubuntu jammy-backports/universe amd64 Packages [30.0 kB]
获取:21 http://repo.huaweicloud.com/ubuntu jammy-backports/universe amd64 c-n-f Metadata [672 B]
获取:22 http://repo.huaweicloud.com/ubuntu jammy-security/main amd64 Packages [2,212 kB]
获取:23 http://repo.huaweicloud.com/ubuntu jammy-security/main Translation-en [340 kB]
获取:24 http://repo.huaweicloud.com/ubuntu jammy-security/main amd64 c-n-f Metadata [13.6 kB]
获取:25 https://packages.microsoft.com/repos/code stable InRelease [3,590 B]           
获取:26 http://repo.huaweicloud.com/ubuntu jammy-security/restricted amd64 Packages [3,127 kB]
获取:27 http://repo.huaweicloud.com/ubuntu jammy-security/restricted Translation-en [556 kB]
获取:28 http://repo.huaweicloud.com/ubuntu jammy-security/restricted amd64 c-n-f Metadata [624 B]
获取:29 http://repo.huaweicloud.com/ubuntu jammy-security/universe amd64 Packages [970 kB]
获取:30 http://repo.huaweicloud.com/ubuntu jammy-security/universe Translation-en [208 kB]
获取:31 http://repo.huaweicloud.com/ubuntu jammy-security/universe amd64 c-n-f Metadata [21.7 kB]
获取:32 http://repo.huaweicloud.com/ubuntu jammy-security/multiverse amd64 Packages [39.6 kB]
获取:33 http://repo.huaweicloud.com/ubuntu jammy-security/multiverse Translation-en [8,716 B]
获取:34 http://repo.huaweicloud.com/ubuntu jammy-security/multiverse amd64 c-n-f Metadata [368 B]
获取:35 https://packages.microsoft.com/repos/code stable/main arm64 Packages [18.5 kB]
获取:36 https://packages.microsoft.com/repos/code stable/main armhf Packages [18.6 kB]
获取:37 https://packages.microsoft.com/repos/code stable/main amd64 Packages [18.3 kB]
已下载 16.4 MB,耗时 3(6,343 kB/s)                       
正在读取软件包列表... 完成
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
ca-certificates 已经是最新版 (20240203~22.04.1)。
ca-certificates 已设置为手动安装。
curl 已经是最新版 (7.81.0-1ubuntu1.20)。
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 64 个软件包未被升级。
命中:1 http://repo.huaweicloud.com/ubuntu jammy InRelease
命中:2 http://repo.huaweicloud.com/ubuntu jammy-updates InRelease                                          
命中:3 http://repo.huaweicloud.com/ubuntu jammy-backports InRelease                                        
命中:4 http://repo.huaweicloud.com/ubuntu jammy-security InRelease                                         
忽略:5 https://download.docker.com/linux/ubuntu jammy InRelease                                            
命中:6 https://packages.microsoft.com/repos/code stable InRelease                                          
忽略:5 https://download.docker.com/linux/ubuntu jammy InRelease
忽略:5 https://download.docker.com/linux/ubuntu jammy InRelease
错误:5 https://download.docker.com/linux/ubuntu jammy InRelease
  Could not handshake: Error in the pull function. [IP: 3.170.229.111 443]
正在读取软件包列表... 完成
W: 无法下载 https://download.docker.com/linux/ubuntu/dists/jammy/InRelease  Could not handshake: Error in the pull function. [IP: 3.170.229.111 443]
W: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。
developer@developer:/$ 

第一次执行命令:成功

developer@developer:/$ # Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install 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

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
命中:1 http://repo.huaweicloud.com/ubuntu jammy InRelease
命中:2 http://repo.huaweicloud.com/ubuntu jammy-updates InRelease
命中:3 http://repo.huaweicloud.com/ubuntu jammy-backports InRelease
命中:4 http://repo.huaweicloud.com/ubuntu jammy-security InRelease
忽略:5 https://download.docker.com/linux/ubuntu jammy InRelease            
命中:6 https://packages.microsoft.com/repos/code stable InRelease          
忽略:5 https://download.docker.com/linux/ubuntu jammy InRelease
忽略:5 https://download.docker.com/linux/ubuntu jammy InRelease
获取:5 https://download.docker.com/linux/ubuntu jammy InRelease [48.8 kB]
获取:7 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages [47.6 kB]
已下载 96.5 kB,耗时 8(11.5 kB/s)
正在读取软件包列表... 完成
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
ca-certificates 已经是最新版 (20240203~22.04.1)curl 已经是最新版 (7.81.0-1ubuntu1.20)。
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 64 个软件包未被升级。
curl: (35) OpenSSL SSL_connect: 连接被对方重置 in connection to download.docker.com:443 
命中:1 http://repo.huaweicloud.com/ubuntu jammy InRelease
命中:2 http://repo.huaweicloud.com/ubuntu jammy-updates InRelease                                         
命中:3 http://repo.huaweicloud.com/ubuntu jammy-backports InRelease                                                            
命中:4 http://repo.huaweicloud.com/ubuntu jammy-security InRelease                                                             
命中:5 https://packages.microsoft.com/repos/code stable InRelease                                                              
命中:6 https://download.docker.com/linux/ubuntu jammy InRelease
正在读取软件包列表... 完成

2. 安装 Docker 软件包

要安装最新版,请运行:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

输入 y ,然后回车,表示确定。

developer@developer:/$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
下列软件包是自动安装的并且现在不需要了:
  linux-headers-5.15.0-119 linux-headers-5.15.0-119-generic linux-image-5.15.0-119-generic linux-modules-5.15.0-119-generic linux-modules-extra-5.15.0-119-generic
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  docker-ce-rootless-extras libslirp0 pigz slirp4netns
建议安装:
  cgroupfs-mount | cgroup-lite
下列【新】软件包将被安装:
  containerd.io docker-buildx-plugin docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin libslirp0 pigz slirp4netns
升级了 0 个软件包,新安装了 9 个软件包,要卸载 0 个软件包,有 64 个软件包未被升级。
需要下载 121 MB 的归档。
解压缩后会消耗 442 MB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 http://repo.huaweicloud.com/ubuntu jammy/universe amd64 pigz amd64 2.6-1 [63.6 kB]
获取:2 http://repo.huaweicloud.com/ubuntu jammy/main amd64 libslirp0 amd64 4.6.1-1build1 [61.5 kB]
获取:3 http://repo.huaweicloud.com/ubuntu jammy/universe amd64 slirp4netns amd64 1.0.1-2 [28.2 kB]
忽略:4 https://download.docker.com/linux/ubuntu jammy/stable amd64 containerd.io amd64 1.7.27-1
忽略:5 https://download.docker.com/linux/ubuntu jammy/stable amd64 docker-buildx-plugin amd64 0.22.0-1~ubuntu.22.04~jammy
获取:6 https://download.docker.com/linux/ubuntu jammy/stable amd64 docker-ce-cli amd64 5:28.0.4-1~ubuntu.22.04~jammy [15.8 MB]
获取:7 https://download.docker.com/linux/ubuntu jammy/stable amd64 docker-ce amd64 5:28.0.4-1~ubuntu.22.04~jammy [19.1 MB]
获取:8 https://download.docker.com/linux/ubuntu jammy/stable amd64 docker-ce-rootless-extras amd64 5:28.0.4-1~ubuntu.22.04~jammy [6,092 kB]
获取:9 https://download.docker.com/linux/ubuntu jammy/stable amd64 docker-compose-plugin amd64 2.34.0-1~ubuntu.22.04~jammy [14.1 MB]
获取:4 https://download.docker.com/linux/ubuntu jammy/stable amd64 containerd.io amd64 1.7.27-1 [30.5 MB]
获取:5 https://download.docker.com/linux/ubuntu jammy/stable amd64 docker-buildx-plugin amd64 0.22.0-1~ubuntu.22.04~jammy [35.3 MB]
已下载 121 MB,耗时 4(27.5 MB/s)             
正在选中未选择的软件包 pigz。
(正在读取数据库 ... 系统当前共安装有 229673 个文件和目录。)
准备解压 .../0-pigz_2.6-1_amd64.deb  ...
正在解压 pigz (2.6-1) ...
正在选中未选择的软件包 containerd.io。
准备解压 .../1-containerd.io_1.7.27-1_amd64.deb  ...
正在解压 containerd.io (1.7.27-1) ...
正在选中未选择的软件包 docker-buildx-plugin。
准备解压 .../2-docker-buildx-plugin_0.22.0-1~ubuntu.22.04~jammy_amd64.deb  ...
正在解压 docker-buildx-plugin (0.22.0-1~ubuntu.22.04~jammy) ...
正在选中未选择的软件包 docker-ce-cli。
准备解压 .../3-docker-ce-cli_5%3a28.0.4-1~ubuntu.22.04~jammy_amd64.deb  ...
正在解压 docker-ce-cli (5:28.0.4-1~ubuntu.22.04~jammy) ...
正在选中未选择的软件包 docker-ce。
准备解压 .../4-docker-ce_5%3a28.0.4-1~ubuntu.22.04~jammy_amd64.deb  ...
正在解压 docker-ce (5:28.0.4-1~ubuntu.22.04~jammy) ...
正在选中未选择的软件包 docker-ce-rootless-extras。
准备解压 .../5-docker-ce-rootless-extras_5%3a28.0.4-1~ubuntu.22.04~jammy_amd64.deb  ...
正在解压 docker-ce-rootless-extras (5:28.0.4-1~ubuntu.22.04~jammy) ...
正在选中未选择的软件包 docker-compose-plugin。
准备解压 .../6-docker-compose-plugin_2.34.0-1~ubuntu.22.04~jammy_amd64.deb  ...
正在解压 docker-compose-plugin (2.34.0-1~ubuntu.22.04~jammy) ...
正在选中未选择的软件包 libslirp0:amd64。
准备解压 .../7-libslirp0_4.6.1-1build1_amd64.deb  ...
正在解压 libslirp0:amd64 (4.6.1-1build1) ...
正在选中未选择的软件包 slirp4netns。
准备解压 .../8-slirp4netns_1.0.1-2_amd64.deb  ...
正在解压 slirp4netns (1.0.1-2) ...
正在设置 docker-buildx-plugin (0.22.0-1~ubuntu.22.04~jammy) ...
正在设置 containerd.io (1.7.27-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
正在设置 docker-compose-plugin (2.34.0-1~ubuntu.22.04~jammy) ...
正在设置 docker-ce-cli (5:28.0.4-1~ubuntu.22.04~jammy) ...
正在设置 libslirp0:amd64 (4.6.1-1build1) ...
正在设置 pigz (2.6-1) ...
正在设置 docker-ce-rootless-extras (5:28.0.4-1~ubuntu.22.04~jammy) ...
正在设置 slirp4netns (1.0.1-2) ...
正在设置 docker-ce (5:28.0.4-1~ubuntu.22.04~jammy) ...
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
正在处理用于 man-db (2.10.2-1) 的触发器 ...
正在处理用于 libc-bin (2.35-0ubuntu3.9) 的触发器 ...
Scanning processes...                                                                                                                                                                                             
Scanning linux images...                                                                                                                                                                                          

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

3. 验证安装

验证安装是否成功,通过运行 hello-world 镜像:

sudo docker run hello-world

此命令下载一个测试镜像并在容器中运行它。当容器运行时,它会打印一条确认消息并退出。

第一次运行:报错

developer@developer:/$ sudo docker run 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

本处的错误是由于 Docker 无法从 Docker Hub(默认镜像仓库)拉取 hello-world 镜像导致的网络连接超时。

配置国内镜像

通过国内镜像源加速下载。修改 Docker 配置文件:

  1. 编辑 /etc/docker/daemon.json(如果文件不存在,直接创建):
sudo vim /etc/docker/daemon.json

  1. 添加以下内容:
{
    "registry-mirrors": [
        "https://<镜像地址>"
    ]
}

https://<镜像地址> 为示例地址,请将<镜像地址>替换为实际可用的镜像地址。

  1. 查看修改效果
cat /etc/docker/daemon.json

  1. 使配置生效

重新启动一下docker

sudo systemctl daemon-reload
sudo systemctl restart docker

第二次运行:成功

developer@developer:/$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete 
Digest: sha256:7e1a4e2d11e2ac7a8c3f768d4166c2defeb09d2a750b010412b6ea13de1efb19
Status: Downloaded newer image for hello-world:latest

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 Engine。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋冠巡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值