卸载旧版本

Docker 的旧版本被称为 docker,docker.io 或 docker-engine 。如果已安装,请卸载它们:

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

当前称为 Docker Engine-Community 软件包 docker-ce

设置仓库

在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,您可以从仓库安装和更新 Docker ,更新 apt 包索引

sudo apt update

安装依赖包

sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

添加GPG密钥

添加 Docker 的官方 GPG 密钥,以下两个根据情况选择即可(国外主机可选择第一个,国内可选择第二个)

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

如果你在阿里云上部署,可能使用阿里云的镜像源(在下面),添加阿里云的证书会更好一些。添加阿里云证书

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

添加软件源

软件源根据主机地区选择最合适的

对于 amd64 架构的计算机,添加清华大学TUNA软件仓库

sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

你也可以使用阿里云的软件仓库

sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"

如果你对你的网络比较有信心,也可以使用官方的软件仓库

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable"

如果你是ARM架构,请运行

echo "deb [arch=armhf] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
     $(lsb_release -cs) stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list

安装 DOCKER ENGINE-COMMUNITY

更新 apt 包索引

sudo apt update

安装最新版本的 Docker Engine-Community

sudo apt install docker-ce

测试

注意:如果你的网络不好使,请先行进行镜像加速再测试

输入

sudo docker run hello-world

如果docker和你问好——或者说出现类似以下输出

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Already exists 
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
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/

就说明安装完事了。

题外话

免SUDO运行&镜像加速

免sudo 参考如下方法将用户添加到docke组

sudo usermod -aG docker [你的用户名]

重启docker

sudo systemctl restart docker

如果重启docker服务后依然不能以普通用户启动docker,那么则需要重启机器后才能免sudo运行。

docker镜像加速

https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

后续设置按照阿里云网页内提示即可,不再赘述。