卸载旧版本

sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

结果显示一堆 No Match for argument: xxx 表示我们没装过 xxx 是干净的。

安装前的准备工作

1. 安装 yum-utils

yum-utils 是一组用于管理 Yum 软件仓库和软件包的实用工具集合。
比如下面要用的 yum-config-manager 就是 yum-utils 中的一个工具。主要用于管理 Yum 软件源配置,可以添加、删除、启用或禁用软件源等操作。

sudo yum install -y yum-utils
                                                                   
Loaded plugins: fastestmirror                                                                                           
Loading mirror speeds from cached hostfile                                                                              
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version                                           
Nothing to do # 没啥要做的,表示我之前已经装过了

2. 添加阿里云的 yum 镜像仓库

注意:整个安装过程,只有这一步与官方不同。因为我们要用阿里的镜像仓库,其他步骤都一样。.

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

添加前、后执行 yum repolist 对比查看,多了一个仓库
在这里插入图片描述

在这里插入图片描述

3. 快速生成 Yum 缓存

当需要频繁安装、更新软件包时,或在新安装的系统中,运行 yum makecache fast 可生成缓存以提高后续操作效率。

yum makecache fast
  • makecache:表示创建或更新缓存
  • fast:表示快速模式。(可能会跳过一些可能比较耗时的检查,比如检查 GPG 签名等。)

CentOS8 没有 fast 直接 yum makecache

安装Docker

sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

以下是对安装的这些软件包的说明:

软件包名称说明
docker-ceDocker 引擎社区版,是 Docker 容器运行的核心组件,负责创建、运行和管理容器。
docker-ce-cliDocker 命令行工具,用于与 Docker 引擎进行交互,通过命令行执行各种 Docker 操作,如创建容器、启动容器、管理镜像等。
containerd.io一个行业标准的容器运行时,它为 Docker 和其他容器化技术提供底层的容器运行支持,负责管理容器的生命周期、存储、网络等。
docker-buildx-pluginDocker Buildx 插件,用于构建多架构的 Docker 镜像,允许在不同的平台和架构上构建和推送容器镜像。
docker-compose-pluginDocker Compose 插件,用于定义和运行多容器的 Docker 应用程序,通过一个 YAML 文件来配置多个容器的服务、网络和存储等,简化了多容器应用的部署和管理。

启动docker

sudo systemctl start docker

注意:如果 systemctl 用不了需要改 /etc/wsl.conf,详情看:使用 systemd 通过 WSL 管理 Linux 服务 1

运行 hello-world

通过运行 hello-world 镜像来验证 Docker Engine 安装是否成功。

sudo docker run hello-world

如果不加 sudo 无法执行 docker 相关命令,说明当前用户可能不在 docker 组里。我们手动加一下:
docker 组是安装 docker 时自动添加的,用来管理操作 docker 的权限)

# 查看,没有 docker 组
groups
# 将用户名 jerry 添加进 docker 组 
sudo usermod -aG docker jerry
# 刷新 docker 组
newgrp docker
# 查看,有 docker 说明就ok了
groups

显示这一堆表示成功

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:53cc4d415d839c98be39331c948609b659ed725170ad2ca8eb36951288f81b75
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 mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
  	"https://3rn5ycey.mirror.aliyuncs.com",
    "https://docker.m.daocloud.io",
    "https://docker.nju.edu.cn"
  ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

卸载 Docker 引擎

  1. 卸载 Docker 引擎、命令行界面(CLI)、containerd 和 Docker Compose 软件包。
sudo yum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
  1. 残留需要自己手动清理
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

参考资料

笑虾:Wni11 下 WSL 安装 CentOS

Install Docker Engine on CentOS


  1. 使用 systemd 通过 WSL 管理 Linux 服务 ↩︎

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部