CentOS7にDockerをインストールする

# CentOSにDockerをインストールする

## 事前準備
AzureでCentOSのVMを作成しておく。  

### 作成したマシン情報
* OS: Linux (centos 7.7.1908)
* マシン名: centos-sample 

## 手順
クライアントのWindowsからteratermで接続する。

### rootユーザに変更
```
sudo su
```

実行結果

```
[aky100200@centos-sample ~]$ sudo su

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for aky100200:
[root@centos-sample aky100200]#
```

### パッケージのインストール

* yum-utils
* device-mapper-persistent-data
* lvm2

```
yum install -y yum-utils device-mapper-persistent-data lvm2
```

実行結果

```
[root@centos-sample aky100200]# yum install -y yum-utils device-mapper-persistent-data lvm2
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
base                                                     | 3.1 kB     00:00
extras                                                   | 2.5 kB     00:00
openlogic                                                | 2.9 kB     00:00
updates                                                  | 2.6 kB     00:00
(1/5): openlogic/7/x86_64/primary_db                       |  33 kB   00:00
(2/5): base/7/x86_64/group_gz                              | 165 kB   00:00
(3/5): extras/7/x86_64/primary_db                          | 159 kB   00:00
(4/5): updates/7/x86_64/primary_db                         | 6.7 MB   00:00
(5/5): base/7/x86_64/primary_db                            | 6.0 MB   00:00
Package yum-utils-1.1.31-52.el7.noarch already installed and latest version
Package device-mapper-persistent-data-0.8.5-1.el7.x86_64 already installed and latest version
Package 7:lvm2-2.02.185-2.el7_7.2.x86_64 already installed and latest version
Nothing to do
[root@centos-sample aky100200]#
```

### Dockerリポジトリのインストール
```
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
```
実行結果

```
root@centos-sample aky100200]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@centos-sample aky100200]#
```

### docker-ceのインストール
```
yum -y install docker-ce docker-ce-cli containerd.io
```

実行結果

```
[root@centos-sample aky100200]# yum -y install docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.2.10-3.2.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.2.10-3.2.el7.x86_64
---> Package docker-ce.x86_64 3:19.03.5-3.el7 will be installed
--> Processing Dependency: libcgroup for package: 3:docker-ce-19.03.5-3.el7.x86_64
---> Package docker-ce-cli.x86_64 1:19.03.5-3.el7 will be installed
--> Running transaction check
---> Package container-selinux.noarch 2:2.107-3.el7 will be installed
--> Processing Dependency: policycoreutils-python for package: 2:container-selinux-2.107-3.el7.noarch
---> Package libcgroup.x86_64 0:0.41-21.el7 will be installed
--> Running transaction check
---> Package policycoreutils-python.x86_64 0:2.5-33.el7 will be installed
--> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-14 for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-33.el7.x86_64
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.8.5-4.el7 will be installed
---> Package checkpolicy.x86_64 0:2.5-8.el7 will be installed
---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be installed
---> 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
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                  Arch     Version             Repository          Size
================================================================================
Installing:
 containerd.io            x86_64   1.2.10-3.2.el7      docker-ce-stable    23 M
 docker-ce                x86_64   3:19.03.5-3.el7     docker-ce-stable    24 M
 docker-ce-cli            x86_64   1:19.03.5-3.el7     docker-ce-stable    39 M
Installing for dependencies:
 audit-libs-python        x86_64   2.8.5-4.el7         base                76 k
 checkpolicy              x86_64   2.5-8.el7           base               295 k
 container-selinux        noarch   2:2.107-3.el7       extras              39 k
 libcgroup                x86_64   0.41-21.el7         base                66 k
 libsemanage-python       x86_64   2.5-14.el7          base               113 k
 policycoreutils-python   x86_64   2.5-33.el7          base               457 k
 python-IPy               noarch   0.75-6.el7          base                32 k
 setools-libs             x86_64   3.3.8-4.el7         base               620 k

Transaction Summary
================================================================================
Install  3 Packages (+8 Dependent packages)

Total download size: 89 M
Installed size: 368 M
Downloading packages:
(1/11): container-selinux-2.107-3.el7.noarch.rpm           |  39 kB   00:00
(2/11): audit-libs-python-2.8.5-4.el7.x86_64.rpm           |  76 kB   00:00
(3/11): checkpolicy-2.5-8.el7.x86_64.rpm                   | 295 kB   00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-19.03.5-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-19.03.5-3.el7.x86_64.rpm is not installed
(4/11): docker-ce-19.03.5-3.el7.x86_64.rpm                 |  24 MB   00:00
(5/11): libcgroup-0.41-21.el7.x86_64.rpm                   |  66 kB   00:00
(6/11): libsemanage-python-2.5-14.el7.x86_64.rpm           | 113 kB   00:00
(7/11): policycoreutils-python-2.5-33.el7.x86_64.rpm       | 457 kB   00:00
(8/11): python-IPy-0.75-6.el7.noarch.rpm                   |  32 kB   00:00
(9/11): setools-libs-3.3.8-4.el7.x86_64.rpm                | 620 kB   00:00
(10/11): docker-ce-cli-19.03.5-3.el7.x86_64.rpm            |  39 MB   00:01
(11/11): containerd.io-1.2.10-3.2.el7.x86_64.rpm           |  23 MB   00:02
--------------------------------------------------------------------------------
Total                                               38 MB/s |  89 MB  00:02
Retrieving key from https://download.docker.com/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://download.docker.com/linux/centos/gpg
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libcgroup-0.41-21.el7.x86_64                                1/11
  Installing : setools-libs-3.3.8-4.el7.x86_64                             2/11
  Installing : 1:docker-ce-cli-19.03.5-3.el7.x86_64                        3/11
  Installing : audit-libs-python-2.8.5-4.el7.x86_64                        4/11
  Installing : python-IPy-0.75-6.el7.noarch                                5/11
  Installing : libsemanage-python-2.5-14.el7.x86_64                        6/11
  Installing : checkpolicy-2.5-8.el7.x86_64                                7/11
  Installing : policycoreutils-python-2.5-33.el7.x86_64                    8/11
  Installing : 2:container-selinux-2.107-3.el7.noarch                      9/11
  Installing : containerd.io-1.2.10-3.2.el7.x86_64                        10/11
  Installing : 3:docker-ce-19.03.5-3.el7.x86_64                           11/11
  Verifying  : checkpolicy-2.5-8.el7.x86_64                                1/11
  Verifying  : policycoreutils-python-2.5-33.el7.x86_64                    2/11
  Verifying  : 3:docker-ce-19.03.5-3.el7.x86_64                            3/11
  Verifying  : libsemanage-python-2.5-14.el7.x86_64                        4/11
  Verifying  : 2:container-selinux-2.107-3.el7.noarch                      5/11
  Verifying  : python-IPy-0.75-6.el7.noarch                                6/11
  Verifying  : audit-libs-python-2.8.5-4.el7.x86_64                        7/11
  Verifying  : containerd.io-1.2.10-3.2.el7.x86_64                         8/11
  Verifying  : 1:docker-ce-cli-19.03.5-3.el7.x86_64                        9/11
  Verifying  : setools-libs-3.3.8-4.el7.x86_64                            10/11
  Verifying  : libcgroup-0.41-21.el7.x86_64                               11/11

Installed:
  containerd.io.x86_64 0:1.2.10-3.2.el7     docker-ce.x86_64 3:19.03.5-3.el7
  docker-ce-cli.x86_64 1:19.03.5-3.el7

Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7
  checkpolicy.x86_64 0:2.5-8.el7
  container-selinux.noarch 2:2.107-3.el7
  libcgroup.x86_64 0:0.41-21.el7
  libsemanage-python.x86_64 0:2.5-14.el7
  policycoreutils-python.x86_64 0:2.5-33.el7
  python-IPy.noarch 0:0.75-6.el7
  setools-libs.x86_64 0:3.3.8-4.el7

Complete!
[root@centos-sample aky100200]#
```

### インストールの確認
dockerコマンドのパスを確認する。  
パスが確認できればOK。  

```
which docker
```

実行結果

```
[root@centos-sample aky100200]# which docker
/bin/docker
[root@centos-sample aky100200]#
```

### dockerdの起動
実行してエラーが出なければOK。  

```
systemctl start docker
```

実行結果

```
[root@centos-sample aky100200]# systemctl start docker
[root@centos-sample aky100200]#
```

### 自動起動設定
```
systemctl enable docker
```

実行結果

```
root@centos-sample aky100200]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@centos-sample aky100200]#
```

これでdockerが使えるようになった。

## 参考
* [CentOS 7にDocker CEをインストールしてコンテナ型仮想化を使う - レムシステム エンジニアブログ](https://www.rem-system.com/centos-docker-install/)