1. # Uninstall old versions
  2. $ apt-get remove docker docker-engine docker.io containerd runc
  3. # Install using the repository
  4. # 1.Set up the repository
  5. $ apt-get update
  6. $ apt-get install \
  7. apt-transport-https \
  8. ca-certificates \
  9. curl \
  10. gnupg \
  11. lsb-release
  12. # 2.Add Docker’s official GPG key:
  13. $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  14. # 3.Use the following command to set up the stable repository
  15. $ echo \
  16. "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  17. $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  18. # Install Docker Engine
  19. # 1.Update the apt package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:
  20. $ apt-get update
  21. $ apt-get install docker-ce docker-ce-cli containerd.io
  22. # 2.To install a specific version of Docker Engine, list the available versions in the repo, then select and install:
  23. # a. List the versions available in your repo:
  24. $ apt-cache madison docker-ce
  25. # b. Install a specific version using the version string from the second column, for example,
  26. $ apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
  27. # 3.Verify that Docker Engine is installed correctly by running the hello-world image.
  28. $ docker run hello-world
  29. # Uninstall Docker Engine
  30. # 1.Uninstall the Docker Engine, CLI, and Containerd packages:
  31. $ apt-get purge docker-ce docker-ce-cli containerd.io
  32. # 2.Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
  33. $ rm -rf /var/lib/docker
  34. $ rm -rf /var/lib/containerd
# 一旦安装完成,Docker 服务将会自动启动。你可以输入下面的命令,验证它:
$ systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-05-11 14:10:30 CST; 9min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 25797 (dockerd)
      Tasks: 26
     Memory: 66.6M
     CGroup: /system.slice/docker.service
             └─25797 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

5月 11 14:10:29 chia-ubuntu dockerd[25797]: time="2021-05-11T14:10:29.684165258+08:00" level=warning msg="Your kernel does not support cgroup blkio weight"
5月 11 14:10:29 chia-ubuntu dockerd[25797]: time="2021-05-11T14:10:29.684173928+08:00" level=warning msg="Your kernel does not support cgroup blkio weight_device"
5月 11 14:10:29 chia-ubuntu dockerd[25797]: time="2021-05-11T14:10:29.684348901+08:00" level=info msg="Loading containers: start."
5月 11 14:10:30 chia-ubuntu dockerd[25797]: time="2021-05-11T14:10:30.324585115+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip>"
5月 11 14:10:30 chia-ubuntu dockerd[25797]: time="2021-05-11T14:10:30.843583889+08:00" level=info msg="Loading containers: done."
5月 11 14:10:30 chia-ubuntu dockerd[25797]: time="2021-05-11T14:10:30.890713738+08:00" level=info msg="Docker daemon" commit=8728dd2 graphdriver(s)=overlay2 version=20.10.6
5月 11 14:10:30 chia-ubuntu dockerd[25797]: time="2021-05-11T14:10:30.890828850+08:00" level=info msg="Daemon has completed initialization"
5月 11 14:10:30 chia-ubuntu systemd[1]: Started Docker Application Container Engine.
5月 11 14:10:30 chia-ubuntu dockerd[25797]: time="2021-05-11T14:10:30.927636828+08:00" level=info msg="API listen on /run/docker.sock"
5月 11 14:14:25 chia-ubuntu dockerd[25797]: time="2021-05-11T14:14:25.611048478+08:00" level=info msg="ignoring event" container=2218960bbc12c5e3783ecb5170ebb9ea4405a27cedab8fc2e67a48fa56109fcb>

# 如果你想阻止 Docker 自动更新,锁住它的版本:
$ apt-mark hold docker-ce

$ docker info
$ docker version

以非 Root 用户身份执行 Docker

默认情况下,只有 root 或者 有 sudo 权限的用户可以执行 Docker 命令。
想要以非 root 用户执行 Docker 命令,你需要将你的用户添加到 Docker 用户组,该用户组在 Docker CE 软件包安装过程中被创建。想要这么做,输入:

sudo usermod -aG docker $USER

$USER是一个环境变量,代表当前用户名。
登出,并且重新登录,以便用户组会员信息刷新。

Install Docker Engine on Ubuntu

Ubuntu Docker 安装

如何在 Ubuntu 20.04 上安装和使用 Docker

How to Install Docker and Run Docker Containers in Ubuntu