ubuntu

正告:切勿在没有配置 Docker APT 源的状况下间接应用 apt 命令装置 Docker.

筹备工作

卸载旧版本

旧版本的 Docker 称为 docker 或者 docker-engine,应用以下命令卸载旧版本:

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

应用 APT 装置

因为 apt 源应用 HTTPS 以确保软件下载过程中不被篡改。因而,咱们首先须要增加应用 HTTPS 传输的软件包以及 CA 证书。

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release

鉴于国内网络问题,强烈建议应用国内源,官网源请在正文中查看。

为了确认所下载软件包的合法性,须要增加软件源的 GPG 密钥。

tsinghua

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

而后,咱们须要向 sources.list 中增加 Docker 软件源:

tsinghua

echo \  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
以上命令会增加稳固版本的 Docker APT 源,如果须要测试版本的 Docker 请将 stable 改为 test。

装置 Docker

更新 apt 软件包缓存,并装置 docker-ce。

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

应用脚本主动装置

在测试或开发环境中 Docker 官网为了简化装置流程,提供了一套便捷的装置脚本,Debian 零碎上能够应用这套脚本装置,另外能够通过 --mirror 选项应用国内源进行装置:
若你想装置测试版的 Docker, 请从 test.docker.com 获取脚本

# curl -fsSL test.docker.com -o get-docker.shcurl -fsSL get.docker.com -o get-docker.shsudo sh get-docker.sh --mirror Aliyun# sudo sh get-docker.sh --mirror AzureChinaCloud

执行这个命令后,脚本就会主动的将所有筹备工作做好,并且把 Docker 的稳固(stable)版本装置在零碎中。

启动 Docker

$ sudo systemctl enable docker$ sudo systemctl start docker

建设 docker 用户组

默认状况下,docker 命令会应用
与 Docker 引擎通信。而只有 root 用户和 docker 组的用户才能够拜访 Docker 引擎的 Unix socket。出于平安思考,个别 Linux 零碎上不会间接应用 root 用户。因而,更好地做法是将须要应用 docker 的用户退出 docker 用户组。
建设 docker 组:

sudo groupadd docker

将以后用户退出 docker 组:

sudo usermod -aG docker $USER

退出以后终端并从新登录,进行如下测试。

测试 Docker 是否装置正确

$ docker run --rm hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-worldb8dfde127a29: Pull completeDigest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24Status: Downloaded newer image for hello-world:latestHello 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 bashShare 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/

替换 docker 源

编辑文件

vim /etc/docker/daemon.json

增加上面的内容

{  "registry-mirrors": [    "https://hub-mirror.c.163.com",    "https://mirror.baidubce.com"  ]}

重启 docker

systemctl daemon-reloadsystemctl restart docker

查看加速器是否失效

执行 docker info,如果从后果中看到了如下内容,阐明配置胜利。

参考链接:
docker 清华源