在 Ubuntu 系统安装 Docker

本贴最后更新于 1055 天前,其中的信息可能已经天翻地覆

安装环境

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

Docker 有三种安装方式:

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

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

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

通过 Docker 仓库安装

配置 Docker 仓库

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

sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

2、添加官方 GPG key

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

3、设置 stable 仓库

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

安装 Docker

1、安装最新版本

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

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

2、安装指定版本

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

apt-cache madison docker-ce

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

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

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

sudo 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 目录,然后运行下面的命令进行安装:

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

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

通过脚本安装

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

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

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

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

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

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

检查 Docker 已经安装了:

zk@ubuntu:~$ apt list --installed | grep docker

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

docker-ce-cli/focal,now 5:20.10.7~3-0~ubuntu-focal amd64 [installed,automatic]
docker-ce-rootless-extras/focal,now 5:20.10.7~3-0~ubuntu-focal amd64 [installed,automatic]
docker-ce/focal,now 5:20.10.7~3-0~ubuntu-focal amd64 [installed]
docker-scan-plugin/focal,now 0.8.0~ubuntu-focal amd64 [installed,automatic]

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

zk@ubuntu:~$ systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2021-06-03 16:22:58 CST; 7min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
    Process: 77752 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=1/FAILURE)
   Main PID: 77752 (code=exited, status=1/FAILURE)

6月 03 16:22:58 ubuntu systemd[1]: docker.service: Scheduled restart job, restart counter is at 5.
6月 03 16:22:58 ubuntu systemd[1]: Stopped Docker Application Container Engine.
6月 03 16:22:58 ubuntu systemd[1]: docker.service: Start request repeated too quickly.
6月 03 16:22:58 ubuntu systemd[1]: docker.service: Failed with result 'exit-code'.
6月 03 16:22:58 ubuntu systemd[1]: Failed to start Docker Application Container Engine.

需要手动启动 Docker 服务:

sudo systemctl start docker

确认安装成功

1、查看版本:

docker --version

输出结果如下所示:

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

2、查看服务

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

systemctl status docker

效果如下所示:

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

3、运行 hello-world 镜像:

sudo docker run hello-world

运行效果如下所示:

zk@ubuntu:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Status: Downloaded newer image for hello-world:latest

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/

zk@ubuntu:~$

卸载 Docker

1、卸载相关软件包

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

2、删除相关数据

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

相关资料

Install Docker Engine on Ubuntu | Docker Documentation

  • Docker

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的操作系统上。容器完全使用沙箱机制,几乎没有性能开销,可以很容易地在机器和数据中心中运行。

    476 引用 • 899 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...