背景

Docker——容器化技术,作为现代化运维技术的一环,其重要性不言而喻。最近我在优化个人网站的过程中,发现之前使用twikoo云函数部署的方式不太能满足我对网站的性能要求,每次发送获取twikoo云函数的请求时,耗时都在一秒多甚至两秒以上。于是考虑使用docker来进行私有化部署twikoo,另外,我的腾讯云服务器快到期了,到时候续费如果涨价太厉害,我也考虑迁移服务器,届时如果把我的nginx服务也托管到docker,到时候服务器迁移起来应该会比较轻松(仅是我的想象);

系统环境

系统版本:Ubuntu 22.04 LTS

过程记录

1.首先打开Docker官方文档,找到了docker安装部分的教程:

https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

2.遵照教程,卸载可能冲突的包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
ubuntu@VM-16-3-ubuntu:~$ for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'docker.io' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 329 not upgraded.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'docker-doc' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 329 not upgraded.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'docker-compose' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 329 not upgraded.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'docker-compose-v2' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 329 not upgraded.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'podman-docker' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 329 not upgraded.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'containerd' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 329 not upgraded.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'runc' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 329 not upgraded.

3.开始安装Docker Engine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ubuntu@VM-16-3-ubuntu:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package docker-ce is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'docker-ce' has no installation candidate
E: Unable to locate package docker-ce-cli
E: Unable to locate package containerd.io
E: Couldn't find any package by glob 'containerd.io'
E: Couldn't find any package by regex 'containerd.io'
E: Unable to locate package docker-buildx-plugin
E: Unable to locate package docker-compose-plugin

果不其然出错了,提示docker-ce无法获取,查了下资料说是apt的镜像源配置错误;

咨询Deepseek老师后,按照以下步骤解决:

1. 安装必要依赖包
1
2
3
4
5
6
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
2 创建 GPG 密钥目录并设置权限
1
sudo mkdir -m 0755 -p /etc/apt/keyrings
3. 下载并添加阿里云 Docker GPG 密钥
1
2
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker-aliyun.gpg
sudo chmod a+r /etc/apt/keyrings/docker-aliyun.gpg
4. 添加阿里云 Docker 软件源
1
2
3
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker-aliyun.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
5. 更新软件包列表
1
sudo apt-get update

以上准备工作做完后,继续尝试用Docker官方文档中的安装命令安装Docker,输出如下,应该是安装成功了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
ubuntu@VM-16-3-ubuntu:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
docker-ce-rootless-extras libslirp0 pigz slirp4netns
Suggested packages:
cgroupfs-mount | cgroup-lite docker-model-plugin
The following NEW packages will be installed:
containerd.io docker-buildx-plugin docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin libslirp0 pigz slirp4netns
0 upgraded, 9 newly installed, 0 to remove and 315 not upgraded.
Need to get 103 MB of archives.
After this operation, 431 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.tencentyun.com/ubuntu jammy/universe amd64 pigz amd64 2.6-1 [63.6 kB]
Get:2 http://mirrors.tencentyun.com/ubuntu jammy/main amd64 libslirp0 amd64 4.6.1-1build1 [61.5 kB]
Get:3 http://mirrors.tencentyun.com/ubuntu jammy/universe amd64 slirp4netns amd64 1.0.1-2 [28.2 kB]
Get:4 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 containerd.io amd64 1.7.27-1 [30.5 MB]
Get:5 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-ce-cli amd64 5:28.3.3-1~ubuntu.22.04~jammy [16.5 MB]
Get:6 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-ce amd64 5:28.3.3-1~ubuntu.22.04~jammy [19.7 MB]
Get:7 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-buildx-plugin amd64 0.26.1-1~ubuntu.22.04~jammy [15.8 MB]
Get:8 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-ce-rootless-extras amd64 5:28.3.3-1~ubuntu.22.04~jammy [6,478 kB]
Get:9 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-compose-plugin amd64 2.39.1-1~ubuntu.22.04~jammy [14.3 MB]
Fetched 103 MB in 8s (13.2 MB/s)
Selecting previously unselected package containerd.io.
(Reading database ... 89383 files and directories currently installed.)
Preparing to unpack .../0-containerd.io_1.7.27-1_amd64.deb ...
Unpacking containerd.io (1.7.27-1) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../1-docker-ce-cli_5%3a28.3.3-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-ce-cli (5:28.3.3-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../2-docker-ce_5%3a28.3.3-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-ce (5:28.3.3-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package pigz.
Preparing to unpack .../3-pigz_2.6-1_amd64.deb ...
Unpacking pigz (2.6-1) ...
Selecting previously unselected package docker-buildx-plugin.
Preparing to unpack .../4-docker-buildx-plugin_0.26.1-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-buildx-plugin (0.26.1-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package docker-ce-rootless-extras.
Preparing to unpack .../5-docker-ce-rootless-extras_5%3a28.3.3-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-ce-rootless-extras (5:28.3.3-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package docker-compose-plugin.
Preparing to unpack .../6-docker-compose-plugin_2.39.1-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-compose-plugin (2.39.1-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package libslirp0:amd64.
Preparing to unpack .../7-libslirp0_4.6.1-1build1_amd64.deb ...
Unpacking libslirp0:amd64 (4.6.1-1build1) ...
Selecting previously unselected package slirp4netns.
Preparing to unpack .../8-slirp4netns_1.0.1-2_amd64.deb ...
Unpacking slirp4netns (1.0.1-2) ...
Setting up docker-buildx-plugin (0.26.1-1~ubuntu.22.04~jammy) ...
Setting up containerd.io (1.7.27-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Setting up docker-compose-plugin (2.39.1-1~ubuntu.22.04~jammy) ...
Setting up docker-ce-cli (5:28.3.3-1~ubuntu.22.04~jammy) ...
Setting up libslirp0:amd64 (4.6.1-1build1) ...
Setting up pigz (2.6-1) ...
Setting up docker-ce-rootless-extras (5:28.3.3-1~ubuntu.22.04~jammy) ...
Setting up slirp4netns (1.0.1-2) ...
Setting up docker-ce (5:28.3.3-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.
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.6) ...
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.

hello,world验证一下,发现又报错了,咨询deepseek老师,说是国内用户必须需要配置Docker镜像加速器

1
2
ubuntu@VM-16-3-ubuntu:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally

问题处理——配置Docker镜像加速器

1.创建docker配置目录
1
sudo mkdir -p /etc/docker
2.创建配置文件:
1
sudo touch /etc/docker/daemon.json
3.将如下配置写入上面的daemon.json配置文件中
1
2
3
4
5
6
7
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"https://mirror.ccs.tencentyun.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
4.重启Docker使服务生效
1
2
sudo systemctl daemon-reload
sudo systemctl restart docker
5.手动拉取hello-world镜像
1
sudo docker pull hello-world

做完以上操作,执行验证的命令再次尝试,输出如下,说明Docker安装成功!🥳🎉✨

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@VM-16-3-ubuntu:/etc/docker# sudo 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/

4.docker可视化管理工具配置

出于对国产的支持,先尝试了一下国内的dockerClouds,配了半天,配好了发现官网服务器的ssl证书过期了,导致docker连不上,我真的没绷住:

image-20250902171130103

退而求其次,我选择了官网的docker desktop,好像是官方推出的可视化工具

安装Docker Desktop

首先来到官网:https://www.docker.com/products/docker-desktop/,下载你系统对应的安装包,然后双击安装,默认配置不变直接点ok

image-20250902172343106

安装完成后重启windows,打开docker,提示我更新wsl image-20250902173504689

管理员方式打开windows powershell,执行命令显示指定服务未安装

1
2
3
4
5
6
PS C:\Windows\system32> wsl --update
正在安装: 适用于 Linux 的 Windows 子系统
指定的服务未安装。 0.0% ]
PS C:\Windows\system32> wsl --list --verbose
适用于 Linux 的 Windows 子系统没有已安装的分发版。
可以通过访问 Microsoft Store 来安装分发版:

咨询deepseek老师后,说要安装一个linux发行版,执行下面的命令,还是报错

1
2
3
PS C:\Windows\system32> wsl --install -d Ubuntu
正在安装: Ubuntu
安装过程中出现错误。分发名称: 'Ubuntu' 错误代码: 0x80070424]

咨询deepseek后,说是wsl2内核文件缺失或损坏,建议手动更新WSL内核

到官网下载更新包,点击msi文件安装更新:https://learn.microsoft.com/zh-cn/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package

重启电脑,然后重新尝试
未完待续…