Docker 安装

其实网上相关的文章已经非常多了,所以这篇文章的作用只是记录和明确一条确定可行的操作路径,为以后的操作节省时间,毕竟像我一样大部分人都不是专业的系统管理员,能够快速解决问题就可以了,并不想做过多的专业研究与探索。

本操作手册是官方手册与网上手册的结合版本,集两家之所长,亲自操作可用。

Debian

 1# 安装 Docker 证书
 2sudo apt-get install -y ca-certificates curl
 3sudo install -m 0755 -d /etc/apt/keyrings
 4sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
 5sudo chmod a+r /etc/apt/keyrings/docker.asc\
 6sudo echo
 7      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian
 8      $(. /etc/os-release && echo "$VERSION_CODENAME") stable" |
 9      sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
10sudo apt-get update
11
12# 安装 Docker
13sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
14
15# 启动 Docker
16sudo systemctl enable docker.service
17sudo systemctl start docker.service
18
19# 测试
20sudo docker version
21> Client: Docker Engine - Community
22> ...
23> Server: Docker Engine - Community
24> ...
25sudo docker compose version
26> Docker Compose version v...
27
28# 使用非 root 用户管理 Docker
29# 创建 docker 用户组
30sudo groupadd docker
31# 将当前用户添加到 docker 用户组
32sudo usermod -aG docker $USER
33# 重启
34# 再次测试
35docker version
36> Client: Docker Engine - Community
37> ...
38> Server: Docker Engine - Community
39> ...
40docker compose version
41> Docker Compose version v...

CentOS | Rocky

 1# 查看系统版本
 2cat /etc/redhat-release
 3> CentOS Linux release 7.6.1810 (Core) // CentOs 7 以上版本
 4
 5# 查看系统内核版本
 6uname -r
 7> 4.10.4-1.el7.elrepo.x86_64 // 内核版本要>3.10
 8
 9# 卸载旧版本
10yum remove docker
11            docker-client
12            docker-client-latest
13            docker-common
14            docker-latest
15            docker-latest-logrotate
16            docker-logrotate
17            docker-selinux
18            docker-engine-selinux
19            docker-engine
20
21# 安装依赖包
22yum install -y yum-utils lvm2
23                device-mapper-persistent-data
24
25# 添加 Docker 软件源
26# 如果系统已切换到阿里云镜像源地址,可跳过此步。
27# 阿里云镜像自带docker源
28yum-config-manager
29    --add-repo
30    https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
31
32# 使用官方源
33yum-config-manager
34    --add-repo
35    https://download.docker.com/linux/centos/docker-ce.repo
36
37yum makecache fast
38# CentOS 8 or Rocky 9 使用timer替换fast
39yum makecache timer
40
41# 安装 Docker
42# 如果在 Rocky 9 系统上,会提示containerd.io版本过低,或下载失败,需要独立安装containerd.io
43# 可以重试几次
44yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
45
46# 启动 Docker
47# 设为开机启动
48systemctl enable docker.service
49# 启动服务
50systemctl start docker.service
51
52# 测试
53docker run hello-world
54> Unable to find image 'hello-world:latest' locally
55> latest: Pulling from library/hello-world
56> 1b930d010525: Pull complete
57> Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
58> Status: Downloaded newer image for hello-world:latest
59> 
60> Hello from Docker!
61> This message shows that your installation appears to be working correctly.
62> 
63> To generate this message, Docker took the following steps:
64>  1. The Docker client contacted the Docker daemon.
65>  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
66>     (amd64)
67>  3. The Docker daemon created a new container from that image which runs the
68>     executable that produces the output you are currently reading.
69>  4. The Docker daemon streamed that output to the Docker client, which sent it
70>     to your terminal.
71> 
72> To try something more ambitious, you can run an Ubuntu container with:
73>  docker run -it ubuntu bash
74> 
75> Share images, automate workflows, and more with a free Docker ID:
76>  https://hub.docker.com/
77> 
78> For more examples and ideas, visit:
79>  https://docs.docker.com/get-started/
80
81docker compose version
82> Docker Compose version v...

若能正常输出以上信息,则说明安装成功。

配置 Docker

1// /etc/docker/daemon.json
2{
3  // 添加官方仓库镜像地址,其实也没什么用很慢
4  "registry-mirrors": ["https://registry.docker-cn.com"]
5}

测试安装结果

 1docker run hello-world
 2> Unable to find image 'hello-world:latest' locally
 3> latest: Pulling from library/hello-world
 4> 1b930d010525: Pull complete
 5> Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
 6> Status: Downloaded newer image for hello-world:latest
 7> 
 8> Hello from Docker!
 9> This message shows that your installation appears to be working correctly.
10> 
11> To generate this message, Docker took the following steps:
12>  1. The Docker client contacted the Docker daemon.
13>  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
14>     (amd64)
15>  3. The Docker daemon created a new container from that image which runs the
16>     executable that produces the output you are currently reading.
17>  4. The Docker daemon streamed that output to the Docker client, which sent it
18>     to your terminal.
19> 
20> To try something more ambitious, you can run an Ubuntu container with:
21>  docker run -it ubuntu bash
22> 
23> Share images, automate workflows, and more with a free Docker ID:
24>  https://hub.docker.com/
25> 
26> For more examples and ideas, visit:
27>  https://docs.docker.com/get-started/
28
29docker compose version
30> Docker Compose version v...