Docker安装教程(三):适用于CentOS的详细指南

1. 安装前准备

注意:我使用的是 root 用户,如果非 root 用户,请赋权后,添加 sudo 执行。

参考:Manage Docker as a non-root user

服务器:阿里云 ECS,CentOS 7.9 64 位。

1.1. 检查

必须启用 centos-extras 存储库。默认情况下启用此存储库。

yum repolist | grep extras

[root@iZwz9aowiyser7ia8fkqb0Z ~]# yum repolist | grep extras
extras/7/x86_64       CentOS-7

yum repolist 显示所有已启用的仓库列表。

1.2. 卸载旧版本的 Docker(如果已安装)

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

卸载 Docker 时,存储在 /var/lib/docker/ 中的映像、容器、卷和网络不会自动删除。

2. Install Docker

参考官方文档:Install Docker Engine on CentOS

安装方式有 3 种,这里选择第一种,通过 Repository 安装(也是官方推荐):

  • 设置 Docker 的 Repository 并从中进行安装,以方便安装和升级任务。这是推荐的方法。👍👍👍
  • 下载 RPM 包、手动安装并完全手动管理升级。
  • 在测试和开发环境中,使用自动化便捷脚本来安装 Docker。

2.1. 安装依赖包及镜像源

# 安装必要工具
yum install -y yum-utils device-mapper-persistent-data lvm2
# 配置阿里云的 Docker Yum 源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2.2. 安装指定版本 Docker Engine

查看 Docker 的可用版本。

yum list docker-ce --showduplicates | sort -r

以下列出了可用的 Docker 版本,以供选择,截取部分输出。以 docker-ce 的版本为例。

[root@iZwz9aowiyser7ia8fkqb0Z ~]# yum list docker-ce --showduplicates | sort -r

Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
docker-ce.x86_64            3:24.0.7-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.6-1.el7                      docker-ce-stable
...
docker-ce.x86_64            3:23.0.6-1.el7                      docker-ce-stable
docker-ce.x86_64            3:20.10.24-3.el7                    docker-ce-stable
...

安装 Docker Engine , containerd , 和 Docker Compose
我选择了当前源中最新版本安装,按以下版本固定,避免日后版本不同导致兼容问题:

软件版本备注
docker-ce24.0.7
docker-ce-cli24.0.7
containerd.io1.6.27
docker-buildx-plugin0.11.2可选,扩展了 Docker 的构建能力
docker-compose-plugin2.21.0可选,使用 compose

以上 containerd.io , docker-buildx-plugin , docker-compose-plugin 不指定版本也是可以的,会根据 docker-ce 的版本自动安装合适的版本。指定版本时,使用完整的版本标识更好,如 docker-ce-3:24.0.7-1.el7 .

yum -y install docker-ce-24.0.7 docker-ce-cli-24.0.7 containerd.io-1.6.27 docker-buildx-plugin-0.11.2 docker-compose-plugin-2.21.0

2.3. 启动 Docker

# 启动Docker
systemctl start docker
# 配置开机自启动
systemctl enable docker

如果安装最新版本 Docker,就不需要指定版本了。

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

2.4. 验证 Docker 是否成功安装

1)查看版本, docker --version .

# 查看版本
[root@iZwz9aowiyser7ia8fkqb0Z ~]# docker --version
Docker version 24.0.7, build afdd53b

2)测试容器, docker run hello-world 。此命令下载测试映像并在容器中运行它。当容器运行时,它会打印一条确认消息并退出。

[root@iZwz9aowiyser7ia8fkqb0Z ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:4bd78111b6914a99dbc560e6a20eab57ff6655aea4a80c50b0c5491968cbc2e6
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/

3. 结语

文章提供了在 CentOS 7.9 操作系统上安装 Docker Engine 的详细步骤,包括准备、检查和卸载 Docker、安装 Docker Engine 及其依赖包、指定版本安装、启动 Docker 以及验证 Docker 安装成功的方法。安装过程生产可用。安装脚本整理如下:

# 卸载已有的docker(如有)
yum -y remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

# 安装必要工具
yum install -y yum-utils device-mapper-persistent-data lvm2

# 配置阿里云的 Docker Yum 源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 安装 Docker Engine 及 Compose
yum -y install docker-ce-24.0.7 docker-ce-cli-24.0.7 containerd.io docker-buildx-plugin docker-compose-plugin

# 启动Docker
systemctl start docker

# 配置开机自启动
systemctl enable docker

# 验证Docker是否安装成功
docker run hello-world
  • 23
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值