一、参考资料

官方文档 - Install Docker Engine on Ubuntu菜鸟教程 - Ubuntu安装Docker

二、卸载Docker

# 卸载可能存在的旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc

# 删除安装时自动安装的所有包
sudo apt-get autoremove docker docker-ce docker-engine docker.io containerd runc
sudo apt-get purge docker-ce

# 删除相关目录和配置文件
# 卸载Docker CE
sudo apt-get purge docker-ce
# 卸载Docker EE
sudo apt-get purge docker-ee
# 删除Docker镜像、容器、数据卷等文件
sudo rm -rf /var/lib/docker

sudo rm -rf /etc/systemd/system/docker.service.d
sudo rm -rf /etc/docker
sudo rm /etc/apparmor.d/docker
sudo groupdel docker
sudo rm -rf /var/run/docker.sock

# 使用dpkg查询已安装包,逐个卸载
dpkg -l | grep docker
sudo apt-get autoremove docker-ce-*

# 检查系统中是否还存在docker文件
sudo find / -name '*docker*'

# 验证是否卸载成功
# 如果是下面的输出,说明已经卸载成功
# sh: /usr/bin/docker: 没有那个文件或目录
docker --version

三、安装Docker

# 更新软件列表
sudo apt-get update

# 允许apt命令可以使用HTTPS访问Docker repository
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
    
# Add key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Add repo
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

# 更新软件列表
sudo apt-get update
  
# 列出可用版本的Docker-ce
sudo apt-cache madison docker-ce

# # 安装指定版本的Docker CE和containerd
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker.io

# 安装最新版本的Docker CE和containerd  
sudo apt-get install docker-ce docker-ce-cli containerd.io docker.io

# 查看docker版本
docker --version

# 重载docker配置
sudo systemctl daemon-reload

# 重启docker服务
sudo systemctl restart docker

可能失效:

# 安装稳定版仓库
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# 添加Docker官方的GPG key    
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

四、安装bash-completion

通过bash_complete脚本,docker提供自动补全功能,在执行该命令时,敲tab即可自动补全参数,大大提高命令输入效率。

# 安装bash-completion
apt install bash-completion

# 加载bash-completion
source /etc/bash_completion

# 修改docker-compose二进制文件的权限
sudo chmod +x /usr/local/bin/docker-compose

# 查看版本
docker-compose --version

五、docker换源加速

参考资料
中科大 docker腾讯云 docker七牛云 docker

# 腾讯云
https://mirror.ccs.tencentyun.com 
# 中科大
https://reg-mirror.qiniu.com/ 
# 七牛云
https://reg-mirror.qiniu.com
# 修改  /etc/docker/daemon.json 文件
{"registry-mirrors":["https://reg-mirror.qiniu.com/"]}

# 重新启动服务
sudo systemctl daemon-reload
sudo systemctl restart docker

# 查看加速配置是否生效
docker info

# 如果出现下面的提示,说明加速配置生效
Registry Mirrors:
    https://reg-mirror.qiniu.com

Docker镜像加速

参考资料
Docker实践(一):Ubuntu16.04安装Docker

六、运行Docker镜像

1. 运行hello-world

# 运行hello-world镜像
sudo docker run hello-world

运行成功的结果

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/

2. 运行apache服务

# 运行apache容器
docker run -d -p 80:80 httpd

# 访问apache
0.0.0.0:8080

七、可能出现的问题

权限问题

解决Ubuntu18.04启动Docker“Got permission denied while trying to connect to the Docker daemon socket“问题[问题解决]Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

docker run -d -p 80:80 httpd
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
错误原因:
Docker 需要用户具有 sudo 权限,为了避免每次命令都输入sudo,可以把用户加入 Docker 用户组。

方法一(推荐)
# 执行以下指令,重启系统后生效
sudo usermod -aG docker $USER 

方法二
sudo chmod 777 /var/run/docker.sock

方法三
sudo groupadd docker          #添加docker用户组
sudo gpasswd -a $XXX docker   #检测当前用户是否已经在docker用户组中,其中XXX为用户名,例如我的,yichao
sudo gpasswd -a $USER docker  #将当前用户添加至docker用户组
newgrp docker                 #更新docker用户组

检查是否更新成功
再次执行"docker version"命令,发现不再出现"Got permission denied"权限报错

docker卸载的问题

bash: /usr/bin/docker: 没有那个文件或目录
错误原因:
手动删除了 /usr/bin/docker 目录,重新安装docker失效

解决办法:
卸载docker,删除docker相关的包,重新安装

未添加

yoyo@ubuntu:~$ sudo apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu xenial InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu xenial-updates InRelease                
Hit:3 http://mirrors.aliyun.com/ubuntu xenial-security InRelease               
Get:4 https://download.docker.com/linux/ubuntu xenial InRelease [66.2 kB]
Ign:4 https://download.docker.com/linux/ubuntu xenial InRelease
Fetched 66.2 kB in 15s (4,299 B/s)
Reading package lists... Done
W: GPG error: https://download.docker.com/linux/ubuntu xenial InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8
W: The repository 'https://download.docker.com/linux/ubuntu xenial InRelease' is not signed.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
解决办法:
将公钥添加至服务器
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys '7EA0A9C3F273FCD8'