前提条件

  1. Docker 运行在CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。
  2. Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。

    基础环境

    内核版本

    1. [root@vm ~]# uname -r
    2. 3.10.0-514.el7.x86_64
    3. [root@vm ~]#

    系统设置

    关闭防火墙并禁止开机自启
[root@vm ~]# systemctl stop firewalld.service
[root@vm ~]# systemctl disable firewalld

关闭selinux

[root@vm ~]# setenforce 0
[root@vm ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

版本清理

查看是否已经安装过docker

[root@vm ~]# yum list installed | grep docker
containerd.io.x86_64                   1.2.10-3.2.el7                  @docker-ce-stable
docker-ce.x86_64                       3:19.03.5-3.el7                 @docker-ce-stable
docker-ce-cli.x86_64                   1:19.03.5-3.el7                 @docker-ce-stable
[root@vm ~]#

如果出现如上情况则使用如下命令卸载

yum remove docker docker-common docker-selinux docker-engine

如果卸载不干净,则使用如下命令

yum remove –y containerd.io.x86_64 
yum remove –y docker-ce.x86_64  
yum remove –y docker-ce-cli.x86_64

如果采用最小化安装,则默认不会安装,如下

[root@cka-master ~]# yum list docker-ce --showduplicates | sort -r
Error: No matching Packages to list
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: mirrors.aliyun.com
 * epel: mirrors.ustc.edu.cn
 * base: mirrors.aliyun.com
[root@cka-master ~]#

依赖安装

lv2和device-mapper-persistent-data为dockerdevicemapper存储设备的必须依赖

[root@cka-master ~]# yum install -y device-mapper-persistent-data lvm2

添加YUM

添加docker-ce安装源

[root@cka-master ~]# curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@cka-master ~]# yum clean all &&  yum makecache

也可以使用https://download.docker.com/linux/centos/docker-ce.repo

可用版本

$ yum list docker-ce --showduplicates | sort -r
* updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
* extras: mirrors.aliyun.com
docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable
  1. 安装指定版本:yum install docker-ce-
  2. 软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。
    例如,docker-ce-19.03.5。
  3. yum install docker-ce-19.03.5

安装服务

安装命令

安装最新版本

$ yum install -y docker-ce

安装完成后,配置文件/etc/docker/daemon.json默认不存在[docker服务未启动时没有/etc/docker目录,启动后会自动创建此目录],如果需要配置则需要手动创建即可。

配置加载

重新加载某个服务的配置文件,如果新安装了一个服务,归属于 systemctl 管理,要是新服务的服务程序配置文件生效,需重新加载,不关闭UNIT的情况下重新载入配置文件,让配置生效,只重新加载.conf类的文件。

$ systemctl daemon-reload

开启启动

$ systemctl enable docker

启动服务

$ systemctl start docker

停止服务

systemctl stop docker

服务状态

systemctl status docker