在 Ubuntu 系统安装 Docker

  |   0 评论   |   0 浏览

安装环境

Ubuntu 20.04.2 LTS (GNU/Linux 5.8.0-53-generic x86_64)

Docker 有三种安装方式:

  • 通过 Docker 仓库安装
  • 手动安装
  • 通过脚本安装

在安装前,首先卸载旧版本:

1sudo apt-get remove docker docker-engine docker.io containerd runc

通过 Docker 仓库安装

配置 Docker 仓库

1、更新 apt ,并安装支持 https 的软件包

1sudo apt-get update
2sudo apt-get install \
3    apt-transport-https \
4    ca-certificates \
5    curl \
6    gnupg \
7    lsb-release

2、添加官方 GPG key

1curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

3、设置 stable 仓库

1echo \
2  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
3  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

安装 Docker

1、安装最新版本

1sudo apt-get update
2sudo apt-get install docker-ce docker-ce-cli containerd.io

安装成功后,它会自动启动 Docker 服务。

2、安装指定版本

若需要安装指定版本,首先用下面的命令查询仓库中有哪些版本:

1apt-cache madison docker-ce

然后,用下面的命令格式进行安装:

1sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

其中,<VERSION_STRING> 要替换成需要安装的版本,比如:5:20.10.0~3-0~ubuntu-focal

1sudo apt-get install docker-ce='5:20.10.0~3-0~ubuntu-focal' docker-ce-cli='5:20.10.0~3-0~ubuntu-focal' containerd.io

手动安装

1、打开 下载 页面,选择 Ubuntu 版本(我这里用的是 Ubuntu 20.04.2 LTS,其代号是 [Focal Fossa],因此点击 focal链接)。然后打开 focal 下的 pool/stable/amd64/ 页面,下载安装包。

我这里下载的是当前最新版本:docker-ce_20.10.7_3-0_ubuntu-focal_amd64.debdocker-ce-cli_20.10.7_3-0_ubuntu-focal_amd64.debcontainerd.io_1.4.6-1_amd64.deb

2、将下载的安装包上传到 /tmp 目录,然后运行下面的命令进行安装:

1sudo dpkg -i /tmp/docker-ce-cli_20.10.7_3-0_ubuntu-focal_amd64.deb
2sudo dpkg -i /tmp/containerd.io_1.4.6-1_amd64.deb
3sudo dpkg -i /tmp/docker-ce_20.10.7_3-0_ubuntu-focal_amd64.deb

安装成功后,它会自动启动 Docker 服务。

通过脚本安装

Docker 提供了一个便捷的 安装脚本,该脚本用来在开发环境快速安装 Docker(官方不建议把该脚本应用于生产环境)。

下载脚本,然后运行脚本安装 Docker:

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

该脚本会安装 Docker 的最新版本。

这个方法可能不太靠谱,我安装时报错了。报错信息如下(最后一行):

 1zk@ubuntu:~$ sudo sh get-docker.sh
 2# Executing docker install script, commit: 7cae5f8b0decc17d6571f9f52eb840fbc13b2737
 3+ sh -c apt-get update -qq >/dev/null
 4+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
 5+ sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | apt-key add -qq - >/dev/null
 6Warning: apt-key output should not be parsed (stdout is not a terminal)
 7+ sh -c echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable" > /etc/apt/sources.list.d/docker.list
 8+ sh -c apt-get update -qq >/dev/null
 9+ [ -n  ]
10+ sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
11E: Sub-process /usr/bin/dpkg returned an error code (1)

检查 Docker 已经安装了:

1zk@ubuntu:~$ apt list --installed | grep docker
2
3WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
4
5docker-ce-cli/focal,now 5:20.10.7~3-0~ubuntu-focal amd64 [installed,automatic]
6docker-ce-rootless-extras/focal,now 5:20.10.7~3-0~ubuntu-focal amd64 [installed,automatic]
7docker-ce/focal,now 5:20.10.7~3-0~ubuntu-focal amd64 [installed]
8docker-scan-plugin/focal,now 0.8.0~ubuntu-focal amd64 [installed,automatic]

但是 Docker 服务没有自动启动:

 1zk@ubuntu:~$ systemctl status docker
 2● docker.service - Docker Application Container Engine
 3     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
 4     Active: failed (Result: exit-code) since Thu 2021-06-03 16:22:58 CST; 7min ago
 5TriggeredBy: ● docker.socket
 6       Docs: https://docs.docker.com
 7    Process: 77752 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=1/FAILURE)
 8   Main PID: 77752 (code=exited, status=1/FAILURE)
 9
106月 03 16:22:58 ubuntu systemd[1]: docker.service: Scheduled restart job, restart counter is at 5.
116月 03 16:22:58 ubuntu systemd[1]: Stopped Docker Application Container Engine.
126月 03 16:22:58 ubuntu systemd[1]: docker.service: Start request repeated too quickly.
136月 03 16:22:58 ubuntu systemd[1]: docker.service: Failed with result 'exit-code'.
146月 03 16:22:58 ubuntu systemd[1]: Failed to start Docker Application Container Engine.

需要手动启动 Docker 服务:

1sudo systemctl start docker

确认安装成功

1、查看版本:

1docker --version

输出结果如下所示:

1zk@ubuntu:~$ docker --version
2Docker version 20.10.7, build f0df350

2、查看服务

安装成功后,会自动启动 Docker 服务,可用下面的命令查看:

1systemctl status docker

效果如下所示:

 1zk@ubuntu:~$ systemctl status docker
 2● docker.service - Docker Application Container Engine
 3     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
 4     Active: active (running) since Thu 2021-06-03 15:32:13 CST; 7s ago
 5TriggeredBy: ● docker.socket
 6       Docs: https://docs.docker.com
 7   Main PID: 56262 (dockerd)
 8      Tasks: 10
 9     Memory: 40.7M
10     CGroup: /system.slice/docker.service
11             └─56262 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

3、运行 hello-world 镜像:

1sudo docker run hello-world

运行效果如下所示:

 1zk@ubuntu:~$ sudo docker run hello-world
 2Unable to find image 'hello-world:latest' locally
 3latest: Pulling from library/hello-world
 4b8dfde127a29: Pull complete
 5Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
 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/
28
29zk@ubuntu:~$

卸载 Docker

1、卸载相关软件包

1sudo apt-get purge docker-ce docker-ce-cli containerd.io

2、删除相关数据

1sudo rm -rf /var/lib/docker
2sudo rm -rf /var/lib/containerd

相关资料

Install Docker Engine on Ubuntu | Docker Documentation