在 Ubuntu20.04 上安装 Docker Engine 的详细步骤如下:

1. 卸载旧版本

首先,卸载可能与 Docker Engine 冲突的非官方包:

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

2. 设置 Docker 的 apt 仓库

  • 更新包列表:
1
sudo apt-get update
  • 安装依赖包:
1
sudo apt-get install ca-certificates curl
  • 添加 Docker 官方 GPG 密钥:
1
2
3
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
  • 添加 Docker 仓库到 apt 源:
1
2
3
4
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  • 更新包列表:
1
sudo apt-get update

3. 安装Docker包

安装最新版本的 Docker Engine:

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

如果安装过程中有部分下载失败,可以手动在hosts文件中添加download.docker.com的IP地址,并配置resolv.conf文件。

4.配置镜像加速器

  • 打开daemon.json文件
1
sudo nano /etc/docker/daemon.json

更改为以下内容并保存(ctrl+o保存,ctrl+x退出):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"registry-mirrors": [
"https://hub.rat.dev",       
"https://docker.1panel.live",
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn",
"https://iju9kaj2.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://cr.console.aliyun.com",
"https://hub.docker.com",
"http://mirrors.ustc.edu.cn"
]
}
  • 重启docker服务
1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

5. (可选)可以避免每次都添加sudo

  • 创建 docker用户组(如果尚未创建):
1
sudo groupadd docker
  • 将当前用户添加到 `docker` 组:
1
sudo usermod -aG docker $USER
  • 重新加载用户组:
1
newgrp docker

6. 验证安装

运行 `hello-world` 镜像:

1
2
sudo docker pull hello-world
sudo docker run hello-world

也可以直接sudo docker run hello-world,此命令会自动下载测试镜像并在容器中运行它。

出现以下内容即成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
xxx@xxxx:~$ 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 theexecutable that produces the output you are currently reading.
4.The Docker daemon streamed that output to the Docker client, which sent itto your terminal.

To try something more ambitious, you can run an ubuntu container with:
S 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/