使用阿里云镜像安装 Docker 服务

  Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE。社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比如经过官方测试认证过的基础设施、容器、插件等。社区版按照stable和edge两种方式发布,每个季度更新stable版本,如17.06,17.09;每个月份更新edge版本,如17.09,17.10。

  1)、Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。

      通过 uname -r 命令查看你当前的内核版本

    #uname -r

     需要注意的是以下命令均是在root账户下执行的,如果是普通账户有些命令则可能需要加上sudo

  2)、执行 yum update

     #yum update #非必选项,但是建议执行操作

 

  3)、卸载旧版本docker

    #yum -y  remove docker  docker-common docker-selinux docker-engine

  4)、设置yum源

    #yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    #yum-config-manager  --add-repo  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #阿里云yum源

    需要注意的是,目前(2019年3月)测试的时候,docker不设置阿里云镜像源也是能正常安装的,并且阿里云镜像源地址也可能会更改,所以以上地址可能会返回404,如果以上镜像源地址不存在也可以忽略这一步直接进行下一步。

  5)、查看所有仓库中所有docker版本,并选择特定版本安装

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

  6)、安装docker

    #yum install -y docker-ce  #由于repo中默认只开启stable仓库,故这里安装的是最新稳定版17.12.0
    #yum install  -y <FQPN>  # 例如:sudo yum install docker-ce-17.12.0.ce

  7)、启动docker并加入开机自动启动

    systemctl start docker
    systemctl enable docker

  8)、配置阿里云镜像加速

    #mkdir  -p  /etc/docker
    #vim  /etc/docker/daemon.json

    添加以下内容

    {"registry-mirrors": ["https://5f2jam6c.mirror.aliyuncs.com", "http://hub-mirror.c.163.com"]}

  9)、重新加载配置文件

    #systemctl reload  docker

  10)、重启docker

    #systemctl restart docker

  安装过程中可能会出现的问题:

  1、正在处理依赖关系 docker-ce-selinux >= 17.03.0.ce-1.el7.centos,它被软件包 docker-ce-17.03.0.ce-1.el7.centos.x86_64 需要软件包 docker-ce-selinux 已经被 docker-ce-cli 取代,但是取代的软件包并未满足需求”等一大串的问题。

    这时我们需要通过 yum install 安装一个rpm包

    通过这个地址我们查看和我们安装docker版本一直的rpm包
    https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

    通过

    #yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm

    img

    问题解决

  2、非root用户使用docker命令报错

    以上我们为了方便在安装过程中使用的是root账户,然而实际操作中我们可能更多的是用普通用户操作docker命令.从root用户切到普通用户执行docker命令时会报如下错误

      
      Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied

    官方解释如下:

      
      The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the root user. 
      To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.

    (1)、创建docker用户组

      #sudo groupadd docker

    此步骤并非必须,默认情况下docker安装过程中会自动创建一个docker用户组

    (2)、将当前用户加入docker用户组

        sudo gpasswd -a ${USER} docker

    注意以上命令只能用当前用户来操作,因为变量USER获取的是当前用户,如果要以root用户来操作,则需要指定的是具体的用户名,而非此变量

    (3)、当前用户退出系统重新登陆

    这里并不是批退出系统,只要切换一下用户即可,比如先切到root用户然后再切回来就可以了

  3、安装docker时报 container-selinux >= 2.9 错误

    由于测试环境的机器是不同批准到来的,第一批安装非常顺序,第二批是直接放置在办公室的PC机,也非常不顺序,安装过程遇到各种各样麻烦。

    上面说过,加入集群时发现docker没有安装成功,安装 docker 时出现了以下错误:

      Error: Package: docker-ce-18.03.1.ce-1.el7.centos.x86_64 (docker-ce-edge)
      Requires: container-selinux >= 2.9
      You could try using --skip-broken to work around the problem
      You could try running: rpm -Va --nofiles --nodigest

    这个报错是 container-selinux 版本低或者是没安装的原因,yum 安装container-selinux 一般的yum源又找不到这个包,需要安装epel源,才能yum安装container-selinux

    然后在安装 docker-ce 就可以了。

      #wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

      #yum install epel-release #阿里云上的epel源

      #yum makecache

    然后,执行命令。
      #yum install container-selinux

原文地址:https://www.cnblogs.com/PatrickLiu/p/14271819.html