官方文档:https://docs.docker.com/engine/install/ubuntu/
01
卸载旧版本Docker
docker的旧版本称为docker、docker.io或docker engine。如果已安装这些,请卸载它们:
$ sudo apt-get remove docker docker-engine docker.io containerd runc
如果apt-get报告没有安装这些软件包,那也没有关系。
02
安装Docker
1、更新apt包索引,安装包以允许apt通过 HTTPS 使用存储库
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、设置稳定存储库
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、运行hello-world 镜像验证Docker Engine是否已正确安装
$ sudo docker run hello-world
6、检测docker版本信息
$ docker --version
输出以下内容:
Docker version 20.10.11, build dea9396
03
安装过程中出现的错误并解决
1、错误1:
执行以下指令:
$ sudo apt-get update
报错:
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.
忽略,继续执行下一条指令:
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
报错:
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'
还是报错,没有办法继续。
解决办法:
编辑ubuntu源 /etc/apt/source.list
$ sudo cp /etc/apt/source.list /etc/apt/source.list.back
$ sudo vi /etc/apt/source.list
在页尾增加以下内容:
deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable
重新执行安装指令:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
2、错误2:
当添加新的源后,进行apt-get update后,可能会出现以下问题:
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.
解决办法:
将公钥添加至服务器,即终端中输入:
$ 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
重新执行安装指令:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
问题解决,继续下一步安装。