Centos 7安装docker、docker-compose教程

一、卸载旧版Docker

1、查询是否安装过旧版

yum list installed | grep docker

没有出现内容就表示没有安装过docker

在这里插入图片描述
出现以下内容代表安装过docker,若需要安装最新版则进行卸载

在这里插入图片描述

2、查询Docker状态,目前是运行状态

systemctl status docker  #查询docker状态

当前docker正在运行需要停止后再进行卸载
在这里插入图片描述

systemctl stop docker  #停止docker

停止docker服务后,出现以下警告信息,docker在关闭状态下被访问会触发自动唤醒机制,下次再执行任意的docker命令会直接启动docker服务

在这里插入图片描述

再次查询docker状态为停止状态

在这里插入图片描述

3、删除安装过Docker的相关包

#删除命令如下
yum -y remove containerd.io.x86_64 \ docker-buildx-plugin.x86_64 \ docker-ce.x86_64 \ docker-ce-cli.x86_64 \ docker-ce-rootless-extras.x86_64 \ docker-compose-plugin.x86_64

在这里插入图片描述

#删除docker相关软件包
yum remove -y docker*

在这里插入图片描述

4、删除Docker相关的镜像和容器

docker相关的镜像和容器,在 /var/lib 下

在这里插入图片描述

rm -rf /var/lib/docker  #删除/var/lib下的docker文件夹

运行后再次查询已经没有docker文件夹了

在这里插入图片描述

二、安装Docker

1、安装yum-utils工具包并设置阿里云镜像

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

在这里插入图片描述

如果安装yum-utils工具时显示以下内容时,说明安装失败,国外镜像源封锁加强,需要更换国内镜像源后才能安装 点击更改镜像源

Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"


 One of the configured repositories failed (未知),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

设置阿里云镜像

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

在这里插入图片描述

2、查看所有Docker版本

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

在这里插入图片描述

3、安装指定版本Docker

例如安装26.0.2-1.el7版本

在这里插入图片描述

注意版本号只要"3:"后面的那部分

yum install -y docker-ce-26.0.2-1.el7 docker-ce-cli-26.0.2-1.el7 containerd.io

4、安装最新版Docker

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

在命令行中 --allowerasing 参数允许替换冲突的软件包,可以不加

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

在这里插入图片描述

三、Docker使用前准备

1、启动Docker服务

systemctl start docker

2、停止Docker服务

systemctl stop docker

在这里插入图片描述

停止docker服务后,出现以上警告信息,docker在关闭状态下被访问会触发自动唤醒机制,下次再执行任意的docker命令会直接启动docker服务,如果希望docker不会触发访问自动唤醒机制,停止docker服务时执行以下命令

systemctl stop docker.socket

3、查看Docker服务状态

systemctl status docker

以下是docker服务运行状态

在这里插入图片描述

以下是docker服务停止状态

在这里插入图片描述

4、设置Docker服务开机自启动

systemctl enable docker

四、docker-compose安装

1、下载Docker Compose二进制文件

# 下载Docker Compose二进制文件
sudo curl -L "https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 赋予执行权限
sudo chmod +x /usr/local/bin/docker-compose

或者使用以下离线安装

2、先到GitHub下载docker-compose

docker-compose下载地址

在这里插入图片描述

3、将下载好的文件上传到Linux

将docker-compose文件上传到/usr/local/bin下,并改名为docker-compose
在这里插入图片描述
给docker-compose添加可执行权限

chmod +x /usr/local/bin/docker-compose

4、查看版本号

docker-compose version

在这里插入图片描述