1.2.2.2 CentOS安装和删除Docker

官方文档:https://docs.docker.com/engine/install/centos/

CentOS 6因内核太旧,即使支持安装docker,但会有各种问题,不建议安装

CentOS 7的extras源虽然可以安装docker,但包比较旧,建议从官方源或镜像源站点下载安装docker

CentOS 8有新技术 podman 代替 docker

因此建议在CentOS 7上安装docker

# extras源中包名为docker
[root@centos7 ~]# yum list docker
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Determining fastest mirrors
 * base: mirrors.ustc.edu.cn
 * epel: mirror.lzu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Available Packages
docker.x86_64                  2:1.13.1-205.git7d71120.el7.centos                  extras

下载rpm包安装:

官方rpm包下载地址:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

阿里镜像下载地址:https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/

通过yum源安装:

由于官网的yum源太慢,下面使用阿里云的Yum源进行安装

# 备份官方源
[root@centos7 ~]# mkdir /etc/yum.repos.d/backup
[root@centos7 ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup

# centos7安装docker依赖三个yum源:Base,Extras,docker-ce
[root@centos7 ~]# wget -O /etc/yum.repos.d/Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@centos7 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@centos7 ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

[root@centos7 ~]# yum clean all
[root@centos7 ~]# yum makecache
[root@centos7 ~]# yum install docker-ce -y
[root@centos7 ~]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

删除 docker

[root@centos7 ~]# yum remove docker-ce -y

# 删除docker资源存放的相关文件
[root@centos7 ~]# rm -rf /var/lib/docker/
1.2.2.2.1 Centos 7 基于阿里云的安装docker方法

阿里云说明:https://developer.aliyun.com/mirror/docker-ce?spm=a2c6h.13651102.0.0.3e221b11sUMKNV

# step1:安装一些必要的系统工具
[root@centos7 ~]# yum install yum-utils device-mapper-persistent-data lvm2 -y
# step2: 添加软件源信息
[root@centos7 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
# step:更新并安装docker-ce
[root@centos7 ~]# yum makecache fast
[root@centos7 ~]# yum install docker-ce -y
# step4:开启docker服务
[root@centos7 ~]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

# 注意:
# 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。
# vim /etc/yum.repos.d/docker-ee.repo
# 将[docker-ce-test]下方的enabled=0修改为enabled=1
#
# 安装指定版本的Docker-CE:
# step1:查找docker-ce的版本:
[root@centos7 ~]# yum list docker-ce --showduplicates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
docker-ce.x86_64                3:20.10.7-3.el7                         @docker-ce-stable
Available Packages
docker-ce.x86_64                17.03.0.ce-1.el7.centos                 docker-ce-stable 
docker-ce.x86_64                17.03.1.ce-1.el7.centos                 docker-ce-stable 
...省略...
docker-ce.x86_64                3:20.10.6-3.el7                         docker-ce-stable 
docker-ce.x86_64                3:20.10.7-3.el7                         docker-ce-stable 
# step2:安装指定版本的docker-ce:(version例如上边的19.03.15-3.el7)
yum install docker-ce-[version]

# 示例
[root@centos7 ~]# yum install docker-ce-19.03.15-3.el7 -y
1.2.2.2.2 在centos 7上安装指定版本的docker
[root@centos7 ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@centos7 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo       CentOS-Media.repo          epel.repo
CentOS-CR.repo         CentOS-Sources.repo        epel-testing.repo
CentOS-Debuginfo.repo  CentOS-Vault.repo
CentOS-fasttrack.repo  CentOS-x86_64-kernel.repo
[root@centos7 ~]# mkdir /etc/yum.repos.d/backup
[root@centos7 ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
[root@centos7 ~]# wget -O /etc/yum.repos.d/Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2021-07-07 10:10:06--  http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 27.221.120.243, 27.221.120.239, 119.188.122.243, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|27.221.120.243|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/Centos-Base.repo’

100%[===============================================>] 2,523       --.-K/s   in 0.005s  

2021-07-07 10:10:06 (528 KB/s) - ‘/etc/yum.repos.d/Centos-Base.repo’ saved [2523/2523]

[root@centos7 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
--2021-07-07 10:09:10--  http://mirrors.aliyun.com/repo/epel-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 27.221.120.115, 27.221.120.243, 27.221.120.248, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|27.221.120.115|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/epel.repo’

100%[===============================================>] 664         --.-K/s   in 0s      

2021-07-07 10:09:10 (242 MB/s) - ‘/etc/yum.repos.d/epel.repo’ saved [664/664]

[root@centos7 ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
--2021-07-07 10:09:14--  https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 27.221.120.248, 27.221.120.118, 119.188.122.240, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|27.221.120.248|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2081 (2.0K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/docker-ce.repo’

100%[===============================================>] 2,081       --.-K/s   in 0.002s  

2021-07-07 10:09:14 (1.12 MB/s) - ‘/etc/yum.repos.d/docker-ce.repo’ saved [2081/2081]

[root@centos7 ~]# ls /etc/yum.repos.d/
backup  Centos-Base.repo  docker-ce.repo  epel.repo
[root@centos7 ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base docker-ce-stable epel extras updates
Cleaning up list of fastest mirrors
[root@centos7 ~]# yum repolist

[root@centos7 ~]# yum list docker-ce* --showduplicates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Available Packages
docker-ce.x86_64                         17.03.0.ce-1.el7.centos         docker-ce-stable
docker-ce.x86_64                         17.03.1.ce-1.el7.centos         docker-ce-stable
...省略...
docker-ce-selinux.noarch                 17.03.2.ce-1.el7.centos         docker-ce-stable
docker-ce-selinux.noarch                 17.03.3.ce-1.el7                docker-ce-stable
[root@centos7 ~]# yum install docker-ce-19.03.15-3.el7 docker-ce-cli-19.03.15-3.el7 -y

[root@centos7 ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.15
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        99e3ed8919
 Built:             Sat Jan 30 03:17:57 2021
 OS/Arch:           linux/amd64
 Experimental:      false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@centos7 ~]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@centos7 ~]# docker version               
Client: Docker Engine - Community
 Version:           19.03.15
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        99e3ed8919
 Built:             Sat Jan 30 03:17:57 2021
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.15
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       99e3ed8919
  Built:            Sat Jan 30 03:16:33 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
1.2.2.2.3 在CentOS 8安装docker
[root@centos8 ~]# tee /etc/yum.repos.d/docker.repo <<EOF
> [docker]
> name=docker
> gpgcheck=0
> baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
> EOF
[docker]
name=docker
gpgcheck=0
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/

[root@centos8 ~]# dnf install docker-ce -y
[root@centos8 ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        f0df350
 Built:             Wed Jun  2 11:56:24 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@centos8 ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@centos8 ~]# docker version               
Client: Docker Engine - Community
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        f0df350
 Built:             Wed Jun  2 11:56:24 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:54:48 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0