安装

安装

优质教程:Docker — 从入门到实践

官方教程:Install Docker Engine on CentOS | Docker Documentation

主要参考以下教程

参考教程:CentOS - Docker — 从入门到实践

准备

卸载旧版

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

在线安装

一键安装

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

这个时间会比较长,不建议使用这个,建议手动安装

手动安装

首先安装必要的系统工具

yum install -y yum-utils

添加阿里云作为源

yum-config-manager --add-repo  https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo

更新 yum 缓存

yum makecache

安装 docker

yum install docker-ce docker-ce-cli containerd.io

官方文档中的安装命令还多安装了两个插件 docker-buildx-plugindocker-compose-plugin 都是常用的,我也建议留着

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

离线安装

yum获取rpm软件包的三种方法_yum rpm包_feike_3的博客-CSDN博客

Linux 离线安装 - Docker — 从入门到实践

离线安装之前,我们需要把安装包也就是 rpm 文件给下载下来,然后再将这些 rpm 文件上传到指定的离线的服务器上进行离线安装,

手动准备 rpm 包 - 推荐

首先准备一个在线的环境中,参考 手动安装 小节执行 yum install 之前的步骤。

查询可安装的 docker 版本:

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

第二列就是版本号

你可以安装指定的版本,也可以不指定,不指定的话默认是使用最新版本,注意,指定版本的时候,版本号要去掉前缀 3:,不然找不到相应版本的包

官方文档 这里 是有错误的,真的坑爹:

我们不是本地安装,而是下载安装包,也就是 rpm 文件

不指定版本:

yum install --downloadonly --downloaddir=/opt docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

指定版本,注意,版本号要去掉前缀 3:,不然找不到相应版本的包

yum install --downloadonly --downloaddir=/opt docker-ce-24.0.4-1.el7 docker-ce-cli-24.0.4-1.el7 containerd.io docker-buildx-plugin docker-compose-plugin

下载到了 /opt 目录下,不同的操作系统,下载下来的包会不一样,一般情况下只会下载 7 个文件:5 个软件 +2 个依赖

然后,我们就可以上传到目标服务器,然后在保存 rpm 的目录下执行安装命令

rpm -ivh *.rpm

如果存在重复安装,则需要带上强制参数

rpm -ivh --force *.rpm

官网下载 RPM 包

这里以 Centos 为例

官方教程:Install Docker Engine on CentOS | Docker Documentation

官网包下载地址:Index of linux/

通过 cat /etc/centos-release 或者 rpm --query centos-release 可查看 Centos 的版本

官方的 rpm 包也是零散的包,这些包都是 docker 相关的包,但是这些包也有各自依赖的包,有的环境有 docker 依赖的这些包,有的环境没有,因此,要实现一个 rpm 包在各种环境下都能成功安装是很困难的。所以我建议选择 手动准备rpm包 小节中的方法,因地制宜。

启动

启动

# 启动docker
sudo systemctl start docker
# 设置开机自启
sudo systemctl enable docker

检测是否安装成功

docker run --rm hello-world

输出一下内容表示成功

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:a13ec89cdf897b3e551bd9f89d499db6ff3a7f44c5b9eb8bca40da20eb4ea1fa
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/

彻底删除

yum remove -y docker* containerd runc
yum remove -y docker-buildx-plugin docker-ce-cli docker-compose-plugin
rm -rf /etc/systemd/system/docker.service.d
rm -rf /var/lib/docker