官方文檔:https://docs.docker.com/engine/install/ubuntu/
01
卸載舊版本Docker
docker的舊版本稱為docker、docker.io或docker engine。如果已安裝這些,請(qǐng)卸載它們:
$ sudo apt-get remove docker docker-engine docker.io containerd runc
如果apt-get報(bào)告沒有安裝這些軟件包,那也沒有關(guān)系。

02
安裝Docker
1、更新apt包索引,安裝包以允許apt通過 HTTPS 使用存儲(chǔ)庫(kù)
Update the apt package index and install packages to allow apt to use arepository over HTTPS:
$ sudo apt-get update
$ sudo apt-get install ca-certificates curl gnupg lsb-release apt-transport-https

2、添加Docker官方的GPG密鑰
Add Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

3、設(shè)置穩(wěn)定存儲(chǔ)庫(kù)
Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels.
$ echo \ "deb [arch=$(dpkg --print-architecture) 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
4、更新apt包索引,安裝最新版本的Docker Engine和containerd
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:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
5、運(yùn)行hello-world 鏡像驗(yàn)證Docker Engine是否已正確安裝
$ sudo docker run hello-world
6、檢測(cè)docker版本信息
$ docker --version
輸出以下內(nèi)容:
Docker version 20.10.11, build dea9396

03
安裝過程中出現(xiàn)的錯(cuò)誤并解決
1、錯(cuò)誤1:
執(zhí)行以下指令:
$ sudo apt-get update
報(bào)錯(cuò):
Hit:1 http://mirrors.tencentyun.com/ubuntu bionic InRelease
Get:2 http://mirrors.tencentyun.com/ubuntu bionic-security InRelease [88.7 kB]? ? ? ? ? ? ? ? ? ? ??
Get:3 http://mirrors.tencentyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Ign:4 https://download.docker.com/linux/ubuntu \ InRelease
Err:5 https://download.docker.com/linux/ubuntu \ Release
? 404? Not Found [IP: 18.65.191.34 443]
Reading package lists... Done
E: The repository 'https://download.docker.com/linux/ubuntu \ Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

忽略,繼續(xù)執(zhí)行下一條指令:
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
報(bào)錯(cuò):
Reading package lists... Done
Building dependency tree? ??
Reading state information... Done
Package docker-ce is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another sourceE: Package 'docker-ce' has no installation candidate
E: Unable to locate package docker-ce-cli
E: Unable to locate package containerd.io
E: Couldn't find any package by glob 'containerd.io'
E: Couldn't find any package by regex 'containerd.io'

還是報(bào)錯(cuò),沒有辦法繼續(xù)。
解決辦法:
編輯ubuntu源 /etc/apt/source.list
$ sudo cp /etc/apt/source.list /etc/apt/source.list.back
$ sudo vi /etc/apt/source.list
在頁尾增加以下內(nèi)容:
deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable

重新執(zhí)行安裝指令:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
2、錯(cuò)誤2:
當(dāng)添加新的源后,進(jìn)行apt-get update后,可能會(huì)出現(xiàn)以下問題:
Hit:1 http://mirrors.tencentyun.com/ubuntu bionic InRelease
Get:2 http://mirrors.tencentyun.com/ubuntu bionic-security InRelease [88.7 kB]? ? ? ? ? ? ? ? ? ??
Get:3 http://mirrors.tencentyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 https://download.docker.com/linux/ubuntu bionic InRelease [64.4 kB]? ? ? ? ? ? ? ? ?
Err:4 https://download.docker.com/linux/ubuntu bionic InRelease
? The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8
Ign:5 https://download.docker.com/linux/ubuntu \ InRelease
Err:6 https://download.docker.com/linux/ubuntu \ Release
? 404? Not Found [IP: 18.65.191.124 443]
Reading package lists... Done
W: GPG error: https://download.docker.com/linux/ubuntu bionic InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8
E: The repository 'https://download.docker.com/linux/ubuntu bionic InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: The repository 'https://download.docker.com/linux/ubuntu \ Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

解決辦法:
將公鑰添加至服務(wù)器,即終端中輸入:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys '7EA0A9C3F273FCD8'
添加成功后終端輸出為:
Executing: /tmp/apt-key-gpghome.VIv8eLEwb3/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys 7EA0A9C3F273FCD8
gpg: key 8D81803C0EBFCD88: 9 signatures not checked due to missing keys
gpg: key 8D81803C0EBFCD88: public key "Docker Release (CE deb) <docker@docker.com>" imported
gpg: Total number processed: 1
gpg:? ? ? ? ? ? ? imported: 1

重新執(zhí)行安裝指令:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
問題解決,繼續(xù)下一步安裝。