Devops(二):CentOS7(在线)安装Docker

安装环境:
Centos7.5

从 2017 年 3 月开始 docker 在原来的基础上分为两个分支版本: Docker CE 和 Docker EE。
1)Docker CE 即社区免费版
2)Docker EE 即企业版,强调安全,但需付费使用。

本文介绍 Docker CE 的安装使用。

1)移除旧的版本

查询当前的Docker版本

$ docker -v

如果存在已安装的Docker,卸载

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

注意:卸载后,/var/lib/docker/ 下内容(images, containers, volumes,networks)依然被保留。

执行示例:

[root@master devops]# sudo yum remove docker \
>                   docker-client \
>                   docker-client-latest \
>                   docker-common \
>                   docker-latest \
>                   docker-latest-logrotate \
>                   docker-logrotate \
>                   docker-selinux \
>                   docker-engine-selinux \
>                   docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-selinux
No Match for argument: docker-engine-selinux
No Match for argument: docker-engine
No Packages marked for removal

备注:因为之前这台服务器未安装过docker,因此会提示找不到。

2)配置库

3.1)安装所需的软件包 yum-utils、device-mapper-persistent-data和 lvm2

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

执行示例:

[root@master devops]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Loaded plugins: fastestmirror
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock error was
12: Timeout on http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
 * base: mirrors.huaweicloud.com
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
base                                                                                                                                                                                | 3.6 kB  00:00:00     
bintray--sbt-rpm                                                                                                                                                                    | 1.3 kB  00:00:00     
http://mirrors.cn99.com/centos/7.5.1804/extras/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
Trying other mirror.
mysql-connectors-community                                                                                                                                                          | 2.5 kB  00:00:00     
mysql-tools-community                                                                                                                                                               | 2.5 kB  00:00:00     
mysql57-community                                                                                                                                                                   | 2.5 kB  00:00:00     
updates                                                                                                                                                                             | 3.4 kB  00:00:00     
(1/7): base/7/x86_64/group_gz                                                                                                                                                       | 166 kB  00:00:00     
(2/7): mysql-tools-community/x86_64/primary_db                                                                                                                                      |  61 kB  00:00:01     
(3/7): bintray--sbt-rpm/primary                                                                                                                                                     | 4.2 kB  00:00:01     
(4/7): mysql-connectors-community/x86_64/primary_db                                                                                                                                 |  44 kB  00:00:02     
(5/7): mysql57-community/x86_64/primary_db                                                                                                                                          | 184 kB  00:00:01     
(6/7): base/7/x86_64/primary_db                                                                                                                                                     | 6.0 MB  00:00:05     
(7/7): updates/7/x86_64/primary_db                                                                                                                                                  | 7.4 MB  00:00:05     
bintray--sbt-rpm                                                                                                                                                                                     37/37
Package device-mapper-persistent-data-0.7.3-3.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package lvm2.x86_64 7:2.02.177-4.el7 will be updated
---> Package lvm2.x86_64 7:2.02.180-10.el7_6.8 will be an update
--> Processing Dependency: lvm2-libs = 7:2.02.180-10.el7_6.8 for package: 7:lvm2-2.02.180-10.el7_6.8.x86_64
---> Package yum-utils.noarch 0:1.1.31-50.el7 will be installed
--> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-50.el7.noarch
--> Processing Dependency: libxml2-python for package: yum-utils-1.1.31-50.el7.noarch
--> Running transaction check
---> Package libxml2-python.x86_64 0:2.9.1-6.el7_2.3 will be installed
---> Package lvm2-libs.x86_64 7:2.02.177-4.el7 will be updated
---> Package lvm2-libs.x86_64 7:2.02.180-10.el7_6.8 will be an update
--> Processing Dependency: device-mapper-event = 7:1.02.149-10.el7_6.8 for package: 7:lvm2-libs-2.02.180-10.el7_6.8.x86_64
---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed
--> Processing Dependency: python-chardet for package: python-kitchen-1.1.1-5.el7.noarch
--> Running transaction check
---> Package device-mapper-event.x86_64 7:1.02.146-4.el7 will be updated
---> Package device-mapper-event.x86_64 7:1.02.149-10.el7_6.8 will be an update
--> Processing Dependency: device-mapper-event-libs = 7:1.02.149-10.el7_6.8 for package: 7:device-mapper-event-1.02.149-10.el7_6.8.x86_64
--> Processing Dependency: device-mapper = 7:1.02.149-10.el7_6.8 for package: 7:device-mapper-event-1.02.149-10.el7_6.8.x86_64
---> Package python-chardet.noarch 0:2.2.1-1.el7_1 will be installed
--> Running transaction check
---> Package device-mapper.x86_64 7:1.02.146-4.el7 will be updated
--> Processing Dependency: device-mapper = 7:1.02.146-4.el7 for package: 7:device-mapper-libs-1.02.146-4.el7.x86_64
---> Package device-mapper.x86_64 7:1.02.149-10.el7_6.8 will be an update
---> Package device-mapper-event-libs.x86_64 7:1.02.146-4.el7 will be updated
---> Package device-mapper-event-libs.x86_64 7:1.02.149-10.el7_6.8 will be an update
--> Running transaction check
---> Package device-mapper-libs.x86_64 7:1.02.146-4.el7 will be updated
---> Package device-mapper-libs.x86_64 7:1.02.149-10.el7_6.8 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================
 Package      Arch                            Version               Repository   Size
========================================================================================
Installing:
 yum-utils        noarch                       1.1.31-50.el7           base      121 k
Updating:
 lvm2             x86_64                       7:2.02.180-10.el7_6.8   updates   1.3 M
Installing for dependencies:
 libxml2-python   x86_64                       2.9.1-6.el7_2.3         base      247 k
 python-chardet   noarch                       2.2.1-1.el7_1           base      227 k
 python-kitchen   noarch                       1.1.1-5.el7             base      267 k
Updating for dependencies:
 device-mapper            x86_64               7:1.02.149-10.el7_6.8   updates   293 k
 device-mapper-event      x86_64               7:1.02.149-10.el7_6.8   updates   189 k
 device-mapper-event-libs x86_64               7:1.02.149-10.el7_6.8   updates   188 k
 device-mapper-libs       x86_64               7:1.02.149-10.el7_6.8   updates   321 k
 lvm2-libs                x86_64               7:2.02.180-10.el7_6.8   updates   1.1 M

Transaction Summary
========================================================================================
Install  1 Package (+3 Dependent packages)
Upgrade  1 Package (+5 Dependent packages)

Total download size: 4.2 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/10): device-mapper-1.02.149-10.el7_6.8.x86_64.rpm                | 293 kB  00:00:00     
(2/10): libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm                   | 247 kB  00:00:01     
(3/10): device-mapper-event-libs-1.02.149-10.el7_6.8.x86_64.rpm     | 188 kB  00:00:01     
(4/10): device-mapper-event-1.02.149-10.el7_6.8.x86_64.rpm          | 189 kB  00:00:01     
(5/10): python-chardet-2.2.1-1.el7_1.noarch.rpm                     | 227 kB  00:00:00     
(6/10): device-mapper-libs-1.02.149-10.el7_6.8.x86_64.rpm           | 321 kB  00:00:01     
(7/10): yum-utils-1.1.31-50.el7.noarch.rpm                          | 121 kB  00:00:01     
(8/10): lvm2-libs-2.02.180-10.el7_6.8.x86_64.rpm                    | 1.1 MB  00:00:01     
(9/10): python-kitchen-1.1.1-5.el7.noarch.rpm                       | 267 kB  00:00:01     
(10/10): lvm2-2.02.180-10.el7_6.8.x86_64.rpm                        | 1.3 MB  00:00:03     
---------------------------------------------------------------------------------------
Total                                                  979 kB/s | 4.2 MB  00:00:04     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 7:device-mapper-1.02.149-10.el7_6.8.x86_64                 1/16 
  Updating   : 7:device-mapper-libs-1.02.149-10.el7_6.8.x86_64            2/16 
  Updating   : 7:device-mapper-event-libs-1.02.149-10.el7_6.8.x86_64      3/16 
  Updating   : 7:device-mapper-event-1.02.149-10.el7_6.8.x86_64           4/16 
  Updating   : 7:lvm2-libs-2.02.180-10.el7_6.8.x86_64                     5/16 
  Installing : python-chardet-2.2.1-1.el7_1.noarch                        6/16 
  Installing : python-kitchen-1.1.1-5.el7.noarch                          7/16 
  Installing : libxml2-python-2.9.1-6.el7_2.3.x86_64                      8/16 
  Installing : yum-utils-1.1.31-50.el7.noarch                             9/16 
  Updating   : 7:lvm2-2.02.180-10.el7_6.8.x86_64                         10/16 
  Cleanup    : 7:lvm2-2.02.177-4.el7.x86_64                              11/16 
  Cleanup    : 7:lvm2-libs-2.02.177-4.el7.x86_64                         12/16 
  Cleanup    : 7:device-mapper-event-1.02.146-4.el7.x86_64               13/16 
  Cleanup    : 7:device-mapper-event-libs-1.02.146-4.el7.x86_64          14/16 
  Cleanup    : 7:device-mapper-1.02.146-4.el7.x86_64                     15/16 
  Cleanup    : 7:device-mapper-libs-1.02.146-4.el7.x86_64                16/16 
  Verifying  : 7:device-mapper-event-1.02.149-10.el7_6.8.x86_64           1/16 
  Verifying  : 7:device-mapper-libs-1.02.149-10.el7_6.8.x86_64            2/16 
  Verifying  : yum-utils-1.1.31-50.el7.noarch                             3/16 
  Verifying  : libxml2-python-2.9.1-6.el7_2.3.x86_64                      4/16 
  Verifying  : python-chardet-2.2.1-1.el7_1.noarch                        5/16 
  Verifying  : 7:device-mapper-event-libs-1.02.149-10.el7_6.8.x86_64      6/16 
  Verifying  : python-kitchen-1.1.1-5.el7.noarch                          7/16 
  Verifying  : 7:lvm2-2.02.180-10.el7_6.8.x86_64                          8/16 
  Verifying  : 7:lvm2-libs-2.02.180-10.el7_6.8.x86_64                     9/16 
  Verifying  : 7:device-mapper-1.02.149-10.el7_6.8.x86_64                10/16 
  Verifying  : 7:device-mapper-event-libs-1.02.146-4.el7.x86_64          11/16 
  Verifying  : 7:device-mapper-1.02.146-4.el7.x86_64                     12/16 
  Verifying  : 7:device-mapper-event-1.02.146-4.el7.x86_64               13/16 
  Verifying  : 7:lvm2-libs-2.02.177-4.el7.x86_64                         14/16 
  Verifying  : 7:lvm2-2.02.177-4.el7.x86_64                              15/16 
  Verifying  : 7:device-mapper-libs-1.02.146-4.el7.x86_64                16/16 

Installed:
  yum-utils.noarch 0:1.1.31-50.el7                                                                                                                                                                
Dependency Installed:
  libxml2-python.x86_64 0:2.9.1-6.el7_2.3      python-chardet.noarch 0:2.2.1-1.el7_1      python-kitchen.noarch 0:1.1.1-5.el7
Updated:
  lvm2.x86_64 7:2.02.180-10.el7_6.8 
Dependency Updated:
  device-mapper.x86_64 7:1.02.149-10.el7_6.8   device-mapper-event.x86_64 7:1.02.149-10.el7_6.8   device-mapper-event-libs.x86_64 7:1.02.149-10.el7_6.8   device-mapper-libs.x86_64 7:1.02.149-10.el7_6.8  
  lvm2-libs.x86_64 7:2.02.180-10.el7_6.8      
Complete!
View Code

3.2)设置稳定的库

$ sudo yum-config-manager \
--add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  #国内阿里库

或者

$ sudo yum-config-manager \
--add-repo https://download.docker.com/linux/centos/docker-ce.repo  #官网库

备注:
    实际上如果安装了该源,不把其他源移走依然会检测到其他源,此时可以将其他源暂时存放到/etc/yum.repos.d/back下:

    $ cd /etc/yum.repos.d/
    $ mkdir -p /etc/yum.repos.d/back
    $ mv *.repo ./back
    $ sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

执行示例:

[root@master devops]# sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://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
[root@master devops]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# ls
bintray-sbt-rpm.repo  CentOS-CR.repo         CentOS-fasttrack.repo  CentOS-Sources.repo  docker-ce.repo        mysql-community-source.repo
CentOS-Base.repo      CentOS-Debuginfo.repo  CentOS-Media.repo      CentOS-Vault.repo    mysql-community.repo
[root@master yum.repos.d]# more docker-ce.repo 
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge]
name=Docker CE Edge - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge-debuginfo]
name=Docker CE Edge - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge-source]
name=Docker CE Edge - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
View Code

3.3)启用 docker-ce-edge和docker-ce-test(可选)

如果需要最新版本的Docker CE 请使用以下命令:

$ sudo yum-config-manager --enable docker-ce-edge

如果需要测试版本的Docker CE 请使用以下命令:

$ sudo yum-config-manager --enable docker-ce-test

3.4)更新 yum 缓存

sudo yum makecache fast

执行示例:

root@master yum.repos.d]# sudo yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
base                                                 | 3.6 kB  00:00:00     
bintray--sbt-rpm                                     | 1.3 kB  00:00:00     
docker-ce-stable                                     | 3.5 kB  00:00:00     
# 出现这个问题的原因就是因为/etc/yum.repos.d/下其他源没有被移走,避免该方式可以把除了docker-ce.repo外的其他源暂时转移到/etc/yum.repos.d/back下,等安装完后在将back恢复。
http://mirrors.cn99.com/centos/7.5.1804/extras/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
Trying other mirror. 
mysql-connectors-community                           | 2.5 kB  00:00:00     
mysql-tools-community                                | 2.5 kB  00:00:00     
mysql57-community                                    | 2.5 kB  00:00:00     
updates                                              | 3.4 kB  00:00:00     
(1/2): docker-ce-stable/x86_64/primary_db            |  32 kB  00:00:00     
(2/2): docker-ce-stable/x86_64/updateinfo            |   55 B  00:00:01     
Metadata Cache Created
[root@master yum.repos.d]# 

3)查看repo中列出可用版本

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

运行示例:

[root@master yum.repos.d]# yum list docker-ce --showduplicates | sort -r
http://mirrors.cn99.com/centos/7.5.1804/extras/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
Trying other mirror.
 * updates: mirror.bit.edu.cn
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: mirror.bit.edu.cn
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.8-3.el7             docker-ce-stable
docker-ce.x86_64    3:18.09.7-3.el7             docker-ce-stable
docker-ce.x86_64    3:18.09.6-3.el7             docker-ce-stable
docker-ce.x86_64    3:18.09.5-3.el7             docker-ce-stable
docker-ce.x86_64    3:18.09.4-3.el7             docker-ce-stable
docker-ce.x86_64    3:18.09.3-3.el7             docker-ce-stable
docker-ce.x86_64    3:18.09.2-3.el7             docker-ce-stable
docker-ce.x86_64    3:18.09.1-3.el7             docker-ce-stable
docker-ce.x86_64    3:18.09.0-3.el7             docker-ce-stable
docker-ce.x86_64    18.06.3.ce-3.el7            docker-ce-stable
docker-ce.x86_64    18.06.2.ce-3.el7            docker-ce-stable
docker-ce.x86_64    18.06.1.ce-3.el7            docker-ce-stable
docker-ce.x86_64    18.06.0.ce-3.el7            docker-ce-stable
docker-ce.x86_64    18.03.1.ce-1.el7.centos     docker-ce-stable
docker-ce.x86_64    18.03.0.ce-1.el7.centos     docker-ce-stable
docker-ce.x86_64    17.12.1.ce-1.el7.centos     docker-ce-stable
docker-ce.x86_64    17.12.0.ce-1.el7.centos     docker-ce-stable
docker-ce.x86_64    17.09.1.ce-1.el7.centos     docker-ce-stable
docker-ce.x86_64    17.09.0.ce-1.el7.centos     docker-ce-stable
docker-ce.x86_64    17.06.2.ce-1.el7.centos     docker-ce-stable
docker-ce.x86_64    17.06.1.ce-1.el7.centos     docker-ce-stable
docker-ce.x86_64    17.06.0.ce-1.el7.centos     docker-ce-stable
docker-ce.x86_64    17.03.3.ce-1.el7            docker-ce-stable
docker-ce.x86_64    17.03.2.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    17.03.0.ce-1.el7.centos     docker-ce-stable
 * base: mirrors.huaweicloud.com
Available Packages
[root@master yum.repos.d]# 

4)选择版本安装:例如,docker-ce-18.06.0.ce-3.el7

$ sudo yum install docker-ce-<VERSION_STRING>
$ sudo yum -y install docker-ce #默认安装方式是 获取最新的。
[root@master yum.repos.d]#  sudo yum install docker-ce-18.06.0.ce-3.el7 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
http://mirrors.cn99.com/centos/7.5.1804/extras/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
Trying other mirror.
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:18.09.1-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2.9 for package: 3:docker-ce-18.09.1-3.el7.x86_64
--> Processing Dependency: libseccomp >= 2.3 for package: 3:docker-ce-18.09.1-3.el7.x86_64
--> Processing Dependency: containerd.io for package: 3:docker-ce-18.09.1-3.el7.x86_64
--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-18.09.1-3.el7.x86_64
--> Processing Dependency: libcgroup for package: 3:docker-ce-18.09.1-3.el7.x86_64
--> Processing Dependency: libseccomp.so.2()(64bit) for package: 3:docker-ce-18.09.1-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.68-1.el7 will be installed
--> Processing Dependency: policycoreutils-python for package: 2:container-selinux-2.68-1.el7.noarch
---> Package containerd.io.x86_64 0:1.2.6-3.3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.2.6-3.3.el7.x86_64
---> Package docker-ce-cli.x86_64 1:19.03.1-3.el7 will be installed
---> Package libcgroup.x86_64 0:0.41-20.el7 will be installed
---> Package libseccomp.x86_64 0:2.3.1-3.el7 will be installed
--> Running transaction check
---> Package containerd.io.x86_64 0:1.2.6-3.3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.2.6-3.3.el7.x86_64
---> Package policycoreutils-python.x86_64 0:2.5-29.el7_6.1 will be installed
--> Processing Dependency: policycoreutils = 2.5-29.el7_6.1 for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-14 for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-29.el7_6.1.x86_64
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.8.4-4.el7 will be installed
--> Processing Dependency: audit-libs(x86-64) = 2.8.4-4.el7 for package: audit-libs-python-2.8.4-4.el7.x86_64
---> Package checkpolicy.x86_64 0:2.5-8.el7 will be installed
---> Package containerd.io.x86_64 0:1.2.6-3.3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.2.6-3.3.el7.x86_64
---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be installed
--> Processing Dependency: libsemanage = 2.5-14.el7 for package: libsemanage-python-2.5-14.el7.x86_64
---> Package policycoreutils.x86_64 0:2.5-22.el7 will be updated
---> Package policycoreutils.x86_64 0:2.5-29.el7_6.1 will be an update
--> Processing Dependency: libsepol >= 2.5-10 for package: policycoreutils-2.5-29.el7_6.1.x86_64
--> Processing Dependency: libselinux-utils >= 2.5-14 for package: policycoreutils-2.5-29.el7_6.1.x86_64
---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be installed
--> Processing Dependency: libselinux >= 2.5-14.1 for package: setools-libs-3.3.8-4.el7.x86_64
--> Running transaction check
---> Package audit-libs.x86_64 0:2.8.1-3.el7 will be updated
--> Processing Dependency: audit-libs(x86-64) = 2.8.1-3.el7 for package: audit-2.8.1-3.el7.x86_64
---> Package audit-libs.x86_64 0:2.8.4-4.el7 will be an update
---> Package containerd.io.x86_64 0:1.2.6-3.3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.2.6-3.3.el7.x86_64
---> Package libselinux.x86_64 0:2.5-12.el7 will be updated
--> Processing Dependency: libselinux(x86-64) = 2.5-12.el7 for package: libselinux-python-2.5-12.el7.x86_64
---> Package libselinux.x86_64 0:2.5-14.1.el7 will be an update
---> Package libselinux-utils.x86_64 0:2.5-12.el7 will be updated
---> Package libselinux-utils.x86_64 0:2.5-14.1.el7 will be an update
---> Package libsemanage.x86_64 0:2.5-11.el7 will be updated
---> Package libsemanage.x86_64 0:2.5-14.el7 will be an update
---> Package libsepol.x86_64 0:2.5-8.1.el7 will be updated
---> Package libsepol.x86_64 0:2.5-10.el7 will be an update
--> Running transaction check
---> Package audit.x86_64 0:2.8.1-3.el7 will be updated
---> Package audit.x86_64 0:2.8.4-4.el7 will be an update
---> Package containerd.io.x86_64 0:1.2.6-3.3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.2.6-3.3.el7.x86_64
---> Package libselinux-python.x86_64 0:2.5-12.el7 will be updated
---> Package libselinux-python.x86_64 0:2.5-14.1.el7 will be an update
--> Finished Dependency Resolution
Error: Package: containerd.io-1.2.6-3.3.el7.x86_64 (docker-ce-stable)
           Requires: container-selinux >= 2:2.74
           Available: 2:container-selinux-2.42-1.gitad8f0f7.el7.noarch (extras)
               container-selinux = 2:2.42-1.gitad8f0f7.el7
           Available: 2:container-selinux-2.55-1.el7.noarch (extras)
               container-selinux = 2:2.55-1.el7
           Available: 2:container-selinux-2.66-1.el7.noarch (extras)
               container-selinux = 2:2.66-1.el7
           Installing: 2:container-selinux-2.68-1.el7.noarch (extras)
               container-selinux = 2:2.68-1.el7
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
[root@master yum.repos.d]# 

错误解决方案:

1、下载container-selinux:

$ sudo wget http://mirrors.atosworldline.com/public/centos/7/extras/x86_64/Packages/container-selinux-2.68-1.el7.noarch.rpm

执行示例:

[root@master devops]# wget http://mirrors.atosworldline.com/public/centos/7/extras/x86_64/Packages/container-selinux-2.68-1.el7.noarch.rpm
--2019-08-22 22:25:31--  http://mirrors.atosworldline.com/public/centos/7/extras/x86_64/Packages/container-selinux-2.68-1.el7.noarch.rpm
Resolving mirrors.atosworldline.com (mirrors.atosworldline.com)... 160.92.141.107
Connecting to mirrors.atosworldline.com (mirrors.atosworldline.com)|160.92.141.107|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 36688 (36K) [application/x-redhat-package-manager]
Saving to: ‘container-selinux-2.68-1.el7.noarch.rpm’

100%[======================================>] 36,688      59.4KB/s   in 0.6s   

2019-08-22 22:25:33 (59.4 KB/s) - ‘container-selinux-2.68-1.el7.noarch.rpm’ saved [36688/36688]

2、安装container-selinux(使用–nodeps --force跳过依赖检查):

rpm -ivh container-selinux-2.68-1.el7.noarch.rpm --nodeps --force

简单来说缺少container-selinux包,直接yum install container-selinux -y 完美解决;

执行示例:

[root@master devops]# rpm -ivh container-selinux-2.68-1.el7.noarch.rpm --nodeps --force
Preparing...                          ################################# [100%]
Updating / installing...
   1:container-selinux-2:2.68-1.el7   ################################# [100%]
[root@master devops]# 

3、重新安装

sudo yum install docker-ce-18.06.0.ce-3.el7 

执行示例:

[root@master devops]# sudo yum install docker-ce-18.06.0.ce-3.el7 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
http://mirrors.cn99.com/centos/7.5.1804/extras/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
Trying other mirror.
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:18.06.0.ce-3.el7 will be installed
--> Processing Dependency: libseccomp >= 2.3 for package: docker-ce-18.06.0.ce-3.el7.x86_64
--> Processing Dependency: libcgroup for package: docker-ce-18.06.0.ce-3.el7.x86_64
--> Processing Dependency: libseccomp.so.2()(64bit) for package: docker-ce-18.06.0.ce-3.el7.x86_64
--> Running transaction check
---> Package libcgroup.x86_64 0:0.41-20.el7 will be installed
---> Package libseccomp.x86_64 0:2.3.1-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================================================================
 Package                                        Arch                                       Version                                              Repository                                            Size
===========================================================================================================================================================================================================
Installing:
 docker-ce                                      x86_64                                     18.06.0.ce-3.el7                                     docker-ce-stable                                      41 M
Installing for dependencies:
 libcgroup                                      x86_64                                     0.41-20.el7                                          base                                                  66 k
 libseccomp                                     x86_64                                     2.3.1-3.el7                                          base                                                  56 k

Transaction Summary
===========================================================================================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 41 M
Installed size: 41 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): libseccomp-2.3.1-3.el7.x86_64.rpm                                                                                                                                            |  56 kB  00:00:05     
(2/3): libcgroup-0.41-20.el7.x86_64.rpm                                                                                                                                             |  66 kB  00:00:06     
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-18.06.0.ce-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY================-] 923 kB/s |  41 MB  00:00:00 ETA 
Public key for docker-ce-18.06.0.ce-3.el7.x86_64.rpm is not installed
(3/3): docker-ce-18.06.0.ce-3.el7.x86_64.rpm                                                                                                                                        |  41 MB  00:00:38     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                      1.1 MB/s |  41 MB  00:00:38     
Retrieving key from https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : libcgroup-0.41-20.el7.x86_64                                                                                                                                                            1/3 
  Installing : libseccomp-2.3.1-3.el7.x86_64                                                                                                                                                           2/3 
  Installing : docker-ce-18.06.0.ce-3.el7.x86_64                                                                                                                                                       3/3 
  Verifying  : libseccomp-2.3.1-3.el7.x86_64                                                                                                                                                           1/3 
  Verifying  : libcgroup-0.41-20.el7.x86_64                                                                                                                                                            2/3 
  Verifying  : docker-ce-18.06.0.ce-3.el7.x86_64                                                                                                                                                       3/3 

Installed:
  docker-ce.x86_64 0:18.06.0.ce-3.el7                                                                                                                                                                      

Dependency Installed:
  libcgroup.x86_64 0:0.41-20.el7                                                                      libseccomp.x86_64 0:2.3.1-3.el7                                                                     

Complete!
[root@master devops]# 
View Code

5)启动 Docker 并加入开机启动

$ sudo systemctl start docker
$ sudo systemctl enable docker
[root@master devops]# sudo systemctl start docker
[root@master devops]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-08-22 22:46:21 CST; 8s ago
     Docs: https://docs.docker.com
 Main PID: 1852 (dockerd)
    Tasks: 21
   Memory: 48.5M
   CGroup: /system.slice/docker.service
           ├─1852 /usr/bin/dockerd
           └─1858 docker-containerd --config /var/run/docker/containerd/containerd.toml

Aug 22 22:46:21 master dockerd[1852]: time="2019-08-22T22:46:21.480103757+08:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
Aug 22 22:46:21 master dockerd[1852]: time="2019-08-22T22:46:21.480130688+08:00" level=info msg="pickfirstBalancer: HandleSubConnStateChange: 0xc42033df20, CONNECTING" module=grpc
Aug 22 22:46:21 master dockerd[1852]: time="2019-08-22T22:46:21.480250962+08:00" level=info msg="pickfirstBalancer: HandleSubConnStateChange: 0xc42033df20, READY" module=grpc
Aug 22 22:46:21 master dockerd[1852]: time="2019-08-22T22:46:21.480265755+08:00" level=info msg="Loading containers: start."
Aug 22 22:46:21 master dockerd[1852]: time="2019-08-22T22:46:21.720656638+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
Aug 22 22:46:21 master dockerd[1852]: time="2019-08-22T22:46:21.798473819+08:00" level=info msg="Loading containers: done."
Aug 22 22:46:21 master dockerd[1852]: time="2019-08-22T22:46:21.812271357+08:00" level=info msg="Docker daemon" commit=0ffa825 graphdriver(s)=overlay2 version=18.06.0-ce
Aug 22 22:46:21 master dockerd[1852]: time="2019-08-22T22:46:21.812437469+08:00" level=info msg="Daemon has completed initialization"
Aug 22 22:46:21 master dockerd[1852]: time="2019-08-22T22:46:21.846494024+08:00" level=info msg="API listen on /var/run/docker.sock"
Aug 22 22:46:21 master systemd[1]: Started Docker Application Container Engine.
[root@master devops]# 
[root@master devops]# sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@master devops]# 
View Code

6)建立docker用户组

默认情况下,docker命令会使用  Unix socket 与Docker引擎通讯。而只有root用户和docker组的用户才可以访问docker引擎的Unix socket。出于安全考虑,一般Unix系统上不会直接使用root用户。因此,更好的做法是将需要使用docker的用户加入docke用户组。

建立docker用户组:

$ sudo groupadd docker

将当前用户加入docker组:

$ sudo usermode -aG docker $USER

退出当前终端并重新登录。

7)验证安装是否成功(有client和service两部分表示docker安装启动都成功了)

$ yum list installed | grep docker #查看已经安装的docker组件
$ sudo docker version              #查看Docker版本信息
$ sudo docker info                 #查看Docker信息

查看已经安装的docker组件

[root@master docker]#  yum list installed | grep docker
docker-ce.x86_64                     18.06.0.ce-3.el7               @docker-ce-stable

查看Docker版本信息

[root@master devops]# docker version
Client:
 Version:           18.06.0-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        0ffa825
 Built:             Wed Jul 18 19:08:18 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.0-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       0ffa825
  Built:            Wed Jul 18 19:10:42 2018
  OS/Arch:          linux/amd64
  Experimental:     false
[root@master devops]# 

查看Docker信息

[root@master docker]# sudo docker info
Containers: 2
 Running: 0
 Paused: 0
 Stopped: 2
Images: 1
Server Version: 18.06.0-ce
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: d64c661f1d51c48782c9cec8fda7604785f93587
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-862.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 4.671GiB
Name: master
ID: QIKO:U734:PE2L:PTJE:7NXC:ATTJ:HB3Z:3KLL:KXA6:QQKJ:OIW5:FJA2
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
View Code

8)测试运行 hello-world

$ docker run hello-world

执行示例:

[root@master devops]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:451ce787d12369c5df2a32c85e5a03d52cbcef6eb3586dd03075f3034f10adcd
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@master devops]# 

由于本地没有hello-world这个镜像,所以会下载一个hello-world的镜像,并在容器内运行。

9)镜像加速

鉴于国内网络问题,拉取 Docker 镜像比较缓慢,我们可以通过配置加速器来解决,我使用的是网易的镜像地址:http://hub-mirror.c.163.com。
新版的 Docker 使用/etc/docker/daemon.json(Linux,没有该文件的话,请先建一个),%programdata%\docker\config\daemon.json(Windows)来配置 Daemon。
请在/etc/docker/daemon.json(Linux,没有该文件的话,请先建一个)配置文件中加入:

{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}

注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。

之后重新启动服务。

[root@localhost ~]# sudo systemctl daemon-reload
[root@localhost ~]# sudo systemctl restart docker

备注:

docker-cn镜像:

{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}

腾讯云

{
  "registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
}

阿里云
https://yq.aliyun.com/articles/29941
也可参考:《centos7:docker使用阿里云镜像加速器的方法,链接:https://my.oschina.net/u/3707314/blog/1635184》

 

参考

1)《【深度知识】DOCKER入门,框架原理,镜像制作和资源列表

2)《2、CentOS7 Docker 安装以及基本使用

 

posted @ 2019-08-21 23:36  cctext  阅读(1714)  评论(0编辑  收藏  举报