1 Docker的搭建

本安装教程参考Docker官方文档,地址如下:

CentOS | Docker Docs

1.1 卸载旧版Docker

首先如果系统中已经存在旧的Docker,则先卸载:

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

1.2 配置Docker yum库

首先要安装一个yum工具

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

1.2.1 安装时出现服务器问题 (可选)

在安装yum工具时可能会出现与服务器链接的问题 无法连接成功 这时可能需要选择换源来解决 一下为更换阿里云源为示例:

1.2.1.1 备份原有的yum源文件

sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

1.2.1.2 然后编辑或替换/etc/yum.repos.d/CentOS-Base.repo文件,使用你喜欢的文本编辑器(如vi或nano):

sudo vi /etc/yum.repos.d/CentOS-Base.repo

1.2.1.3 将文件内容更换为阿里云的ContOS7源

[base]
name=CentOS-$releasever - Base - 163.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates - 163.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras - 163.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

1.2.1.4 清除yum缓存并重启

sudo yum clean all
sudo yum makecache

1.3 安装成功后,执行命令,配置Docker的yum源(已更新为阿里云源):

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

sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo

1.4 更新yum,建立缓存

sudo yum makecache fast

1.5 安装Docker

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

1.6 启动和校验

# 启动Docker
systemctl start docker

# 停止Docker
systemctl stop docker

# 重启
systemctl restart docker

# 设置开机自启
systemctl enable docker

# 执行docker ps命令,如果不报错,说明安装启动成功
docker ps