在 Ubuntu Server 上安装 Docker

May 7, 2024 Homecat

Docker 是一个开源的应用容器引擎,通过容器的生成和使用加快构建、共享和运行应用程序的速度,帮助开发人员在任何地方构建、共享、运行和验证应用程序,而无需繁琐的环境配置或管理。本文是在 Ubuntu Server 22.04 上安装 Docker 的过程记录。

安装

Docker在 https://get.docker.com/ 提供了安装脚本,可以以非交互方式将Docker安装。

获取脚本及安装命令

1curl -fsSL https://get.docker.com -o get-docker.sh
2sudo sh get-docker.sh

安装提示信息

 1# Executing docker install script, commit: e5543d473431b782227f8908005543bb4389b8de
 2+ sh -c apt-get update -qq >/dev/null
 3+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
 4debconf: delaying package configuration, since apt-utils is not installed
 5+ sh -c install -m 0755 -d /etc/apt/keyrings
 6+ sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
 7+ sh -c chmod a+r /etc/apt/keyrings/docker.gpg
 8+ sh -c echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable" > /etc/apt/sources.list.d/docker.list
 9+ sh -c apt-get update -qq >/dev/null
10+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-ce-rootless-extras docker-buildx-plugin >/dev/null
11debconf: delaying package configuration, since apt-utils is not installed
12+ sh -c docker version
13Client: Docker Engine - Community
14 Version:           26.1.1
15 API version:       1.45
16 Go version:        go1.21.9
17 Git commit:        4cf5afa
18 Built:             Tue Apr 30 11:47:53 2024
19 OS/Arch:           linux/amd64
20 Context:           default
21
22Server: Docker Engine - Community
23 Engine:
24  Version:          26.1.1
25  API version:      1.45 (minimum version 1.24)
26  Go version:       go1.21.9
27  Git commit:       ac2de55
28  Built:            Tue Apr 30 11:47:53 2024
29  OS/Arch:          linux/amd64
30  Experimental:     false
31 containerd:
32  Version:          1.6.31
33  GitCommit:        e377cd56a71523140ca6ae87e30244719194a521
34 runc:
35  Version:          1.1.12
36  GitCommit:        v1.1.12-0-g51d5e94
37 docker-init:
38  Version:          0.19.0
39  GitCommit:        de40ad0
40
41================================================================================
42
43To run Docker as a non-privileged user, consider setting up the
44Docker daemon in rootless mode for your user:
45
46    dockerd-rootless-setuptool.sh install
47
48Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.
49
50
51To run the Docker daemon as a fully privileged service, but granting non-root
52users access, refer to https://docs.docker.com/go/daemon-access/
53
54WARNING: Access to the remote API on a privileged Docker daemon is equivalent
55         to root access on the host. Refer to the 'Docker daemon attack surface'
56         documentation for details: https://docs.docker.com/go/attack-surface/
57
58================================================================================

验证安装完成

 1docker run --rm hello-world
 2Unable to find image 'hello-world:latest' locally
 3latest: Pulling from library/hello-world
 4c1ec31eb5944: Pull complete 
 5Digest: sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
 6Status: Downloaded newer image for hello-world:latest
 7
 8Hello from Docker!
 9This message shows that your installation appears to be working correctly.
10
11To 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
20To try something more ambitious, you can run an Ubuntu container with:
21 $ docker run -it ubuntu bash
22
23Share images, automate workflows, and more with a free Docker ID:
24 https://hub.docker.com/
25
26For more examples and ideas, visit:
27 https://docs.docker.com/get-started/

查看安装版本

 1sudo docker version
 2
 3Client: Docker Engine - Community
 4 Version:           26.1.1
 5 API version:       1.45
 6 Go version:        go1.21.9
 7 Git commit:        4cf5afa
 8 Built:             Tue Apr 30 11:47:53 2024
 9 OS/Arch:           linux/amd64
10 Context:           default
11
12Server: Docker Engine - Community
13 Engine:
14  Version:          26.1.1
15  API version:      1.45 (minimum version 1.24)
16  Go version:       go1.21.9
17  Git commit:       ac2de55
18  Built:            Tue Apr 30 11:47:53 2024
19  OS/Arch:          linux/amd64
20  Experimental:     false
21 containerd:
22  Version:          1.6.31
23  GitCommit:        e377cd56a71523140ca6ae87e30244719194a521
24 runc:
25  Version:          1.1.12
26  GitCommit:        v1.1.12-0-g51d5e94
27 docker-init:
28  Version:          0.19.0
29  GitCommit:        de40ad0

启动 Docker

1sudo systemctl enable docker
2sudo systemctl start docker

授予非 root 用户特权

1sudo groupadd docker
2sudo usermod -aG docker $USER

改用国内镜像源

文件位置:/etc/docker/daemon.json

1{
2  "registry-mirrors": [
3        "https://docker.m.daocloud.io",
4        "https://docker.nju.edu.cn",
5        "https://dockerproxy.com"
6  ]
7}

删除 Docker

1sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
2sudo rm -rf /var/lib/docker
3sudo rm -rf /var/lib/containerd