Devops(二):CentOS7(在線)安裝Docker

安裝環境:
Centos7.5node

從 2017 年 3 月開始 docker 在原來的基礎上分爲兩個分支版本: Docker CE 和 Docker EE。
1)Docker CE 即社區免費版
2)Docker EE 即企業版,強調安全,但需付費使用。python

本文介紹 Docker CE 的安裝使用。mysql

1)移除舊的版本

查詢當前的Docker版本linux

$ docker -v

若是存在已安裝的Docker,卸載git

$ 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)依然被保留。sql

執行示例:docker

[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,所以會提示找不到。json

2)配置庫

3.1)安裝所需的軟件包 yum-utils、device-mapper-persistent-data和 lvm2ubuntu

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

執行示例:centos

[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]# 

錯誤解決方案:

一、下載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]

二、安裝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]# 

三、從新安裝

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)《二、CentOS7 Docker 安裝以及基本使用

相關文章
相關標籤/搜索