Dockerを使ってコンテナ技術に入門してみたお話

昨今の IT インフラ界隈では、クラウドに加えてコンテナというキーワードをよく耳にする方も多いのではないでしょうか。

私も良く「コンテナ」というワードを耳にするのですが、実際のところ現時点で弊社にはコンテナ技術を使った案件は無く、社内であまりコンテナ技術の話をすることは無い状況です。

とりあえず流行りの技術を追いかけるためにコンテナを起動するところまでを試してみました。

Dockerインストール

まずは、コンテナを利用するツールとして Docker をインストールします。

今回は CentOS 7.6 にインストールするため、

こちらのInstall Docker Engine on CentOSを参考にしながらインストール作業を実施しました。

参考 Install Docker Engine on CentOSdocker.com

最初に Docker に必要なパッケージをインストール (アップデート) します。

yum
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp-srv2.kddilabs.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
Package yum-utils-1.1.31-50.el7.noarch already installed and latest version
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.180-8.el7 will be updated
---> Package lvm2.x86_64 7:2.02.180-10.el7_6.3 will be an update
--> Processing Dependency: lvm2-libs = 7:2.02.180-10.el7_6.3 for package: 7:lvm2-2.02.180-10.el7_6.3.x86_64
--> Running transaction check
---> Package lvm2-libs.x86_64 7:2.02.180-8.el7 will be updated
---> Package lvm2-libs.x86_64 7:2.02.180-10.el7_6.3 will be an update
--> Processing Dependency: device-mapper-event = 7:1.02.149-10.el7_6.3 for package: 7:lvm2-libs-2.02.180-10.el7_6.3.x86_64
--> Running transaction check
---> Package device-mapper-event.x86_64 7:1.02.149-8.el7 will be updated
---> Package device-mapper-event.x86_64 7:1.02.149-10.el7_6.3 will be an update
--> Processing Dependency: device-mapper-event-libs = 7:1.02.149-10.el7_6.3 for package: 7:device-mapper-event-1.02.149-10.el7_6.3.x86_64
--> Processing Dependency: device-mapper = 7:1.02.149-10.el7_6.3 for package: 7:device-mapper-event-1.02.149-10.el7_6.3.x86_64
--> Running transaction check
---> Package device-mapper.x86_64 7:1.02.149-8.el7 will be updated
--> Processing Dependency: device-mapper = 7:1.02.149-8.el7 for package: 7:device-mapper-libs-1.02.149-8.el7.x86_64
---> Package device-mapper.x86_64 7:1.02.149-10.el7_6.3 will be an update
---> Package device-mapper-event-libs.x86_64 7:1.02.149-8.el7 will be updated
---> Package device-mapper-event-libs.x86_64 7:1.02.149-10.el7_6.3 will be an update
--> Running transaction check
---> Package device-mapper-libs.x86_64 7:1.02.149-8.el7 will be updated
---> Package device-mapper-libs.x86_64 7:1.02.149-10.el7_6.3 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================
 Package                          Arch           Version                          Repository       Size
========================================================================================================
Updating:
 lvm2                             x86_64         7:2.02.180-10.el7_6.3            updates         1.3 M
Updating for dependencies:
 device-mapper                    x86_64         7:1.02.149-10.el7_6.3            updates         292 k
 device-mapper-event              x86_64         7:1.02.149-10.el7_6.3            updates         188 k
 device-mapper-event-libs         x86_64         7:1.02.149-10.el7_6.3            updates         188 k
 device-mapper-libs               x86_64         7:1.02.149-10.el7_6.3            updates         320 k
 lvm2-libs                        x86_64         7:2.02.180-10.el7_6.3            updates         1.1 M

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

Total download size: 3.3 M
Downloading packages:
updates/7/x86_64/prestodelta                                                     | 481 kB  00:00:00     
Delta RPMs reduced 2.7 M of updates to 1.5 M (43% saved)
(1/6): device-mapper-libs-1.02.149-8.el7_1.02.149-10.el7_6.3.x86_64.drpm         | 180 kB  00:00:00     
warning: /var/cache/yum/x86_64/7/updates/packages/device-mapper-1.02.149-10.el7_6.3.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for device-mapper-1.02.149-10.el7_6.3.x86_64.rpm is not installed
(2/6): device-mapper-1.02.149-10.el7_6.3.x86_64.rpm                              | 292 kB  00:00:00     
(3/6): lvm2-libs-2.02.180-8.el7_2.02.180-10.el7_6.3.x86_64.drpm                  | 387 kB  00:00:00     
(4/6): device-mapper-event-1.02.149-10.el7_6.3.x86_64.rpm                        | 188 kB  00:00:00     
(5/6): device-mapper-event-libs-1.02.149-10.el7_6.3.x86_64.rpm                   | 188 kB  00:00:00     
(6/6): lvm2-2.02.180-8.el7_2.02.180-10.el7_6.3.x86_64.drpm                       | 988 kB  00:00:00     
Finishing delta rebuilds of 3 package(s) (2.7 M)
--------------------------------------------------------------------------------------------------------
Total                                                                   963 kB/s | 2.2 MB  00:00:02     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) "
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 7:device-mapper-1.02.149-10.el7_6.3.x86_64                                          1/12 
  Updating   : 7:device-mapper-libs-1.02.149-10.el7_6.3.x86_64                                     2/12 
  Updating   : 7:device-mapper-event-libs-1.02.149-10.el7_6.3.x86_64                               3/12 
  Updating   : 7:device-mapper-event-1.02.149-10.el7_6.3.x86_64                                    4/12 
  Updating   : 7:lvm2-libs-2.02.180-10.el7_6.3.x86_64                                              5/12 
  Updating   : 7:lvm2-2.02.180-10.el7_6.3.x86_64                                                   6/12 
  Cleanup    : 7:lvm2-2.02.180-8.el7.x86_64                                                        7/12 
  Cleanup    : 7:lvm2-libs-2.02.180-8.el7.x86_64                                                   8/12 
  Cleanup    : 7:device-mapper-event-1.02.149-8.el7.x86_64                                         9/12 
  Cleanup    : 7:device-mapper-event-libs-1.02.149-8.el7.x86_64                                   10/12 
  Cleanup    : 7:device-mapper-1.02.149-8.el7.x86_64                                              11/12 
  Cleanup    : 7:device-mapper-libs-1.02.149-8.el7.x86_64                                         12/12 
  Verifying  : 7:device-mapper-libs-1.02.149-10.el7_6.3.x86_64                                     1/12 
  Verifying  : 7:device-mapper-event-libs-1.02.149-10.el7_6.3.x86_64                               2/12 
  Verifying  : 7:device-mapper-1.02.149-10.el7_6.3.x86_64                                          3/12 
  Verifying  : 7:device-mapper-event-1.02.149-10.el7_6.3.x86_64                                    4/12 
  Verifying  : 7:lvm2-libs-2.02.180-10.el7_6.3.x86_64                                              5/12 
  Verifying  : 7:lvm2-2.02.180-10.el7_6.3.x86_64                                                   6/12 
  Verifying  : 7:lvm2-2.02.180-8.el7.x86_64                                                        7/12 
  Verifying  : 7:device-mapper-event-1.02.149-8.el7.x86_64                                         8/12 
  Verifying  : 7:lvm2-libs-2.02.180-8.el7.x86_64                                                   9/12 
  Verifying  : 7:device-mapper-libs-1.02.149-8.el7.x86_64                                         10/12 
  Verifying  : 7:device-mapper-1.02.149-8.el7.x86_64                                              11/12 
  Verifying  : 7:device-mapper-event-libs-1.02.149-8.el7.x86_64                                   12/12 

Updated:
  lvm2.x86_64 7:2.02.180-10.el7_6.3                                                                     

Dependency Updated:
  device-mapper.x86_64 7:1.02.149-10.el7_6.3                                                            
  device-mapper-event.x86_64 7:1.02.149-10.el7_6.3                                                      
  device-mapper-event-libs.x86_64 7:1.02.149-10.el7_6.3                                                 
  device-mapper-libs.x86_64 7:1.02.149-10.el7_6.3                                                       
  lvm2-libs.x86_64 7:2.02.180-10.el7_6.3                                                                

Complete!

次に、Docker 社が提供しているリポジトリを追加します。

repo
[root@localhost ~]# 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

最後に、追加したリポジトリから Docker 関連のパッケージをインストールします。

yum
[root@localhost ~]# yum install -y docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp-srv2.kddilabs.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.2.5-3.1.el7 will be installed
---> Package docker-ce.x86_64 3:18.09.4-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2.9 for package: 3:docker-ce-18.09.4-3.el7.x86_64
---> Package docker-ce-cli.x86_64 1:18.09.4-3.el7 will be installed
--> Running transaction check
---> Package container-selinux.noarch 2:2.74-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================
 Package                    Arch            Version                     Repository                 Size
========================================================================================================
Installing:
 containerd.io              x86_64          1.2.5-3.1.el7               docker-ce-stable           22 M
 docker-ce                  x86_64          3:18.09.4-3.el7             docker-ce-stable           19 M
 docker-ce-cli              x86_64          1:18.09.4-3.el7             docker-ce-stable           14 M
Installing for dependencies:
 container-selinux          noarch          2:2.74-1.el7                extras                     38 k

Transaction Summary
========================================================================================================
Install  3 Packages (+1 Dependent package)

Total download size: 55 M
Installed size: 236 M
Downloading packages:
(1/4): container-selinux-2.74-1.el7.noarch.rpm                                   |  38 kB  00:00:00     
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/containerd.io-1.2.5-3.1.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for containerd.io-1.2.5-3.1.el7.x86_64.rpm is not installed
(2/4): containerd.io-1.2.5-3.1.el7.x86_64.rpm                                    |  22 MB  00:00:03     
(3/4): docker-ce-18.09.4-3.el7.x86_64.rpm                                        |  19 MB  00:00:04     
(4/4): docker-ce-cli-18.09.4-3.el7.x86_64.rpm                                    |  14 MB  00:00:03     
--------------------------------------------------------------------------------------------------------
Total                                                                   8.1 MB/s |  55 MB  00:00:06     
Retrieving key from https://download.docker.com/linux/centos/gpg
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) "
 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 : 1:docker-ce-cli-18.09.4-3.el7.x86_64                                                 1/4 
  Installing : containerd.io-1.2.5-3.1.el7.x86_64                                                   2/4 
  Installing : 2:container-selinux-2.74-1.el7.noarch                                                3/4 
  Installing : 3:docker-ce-18.09.4-3.el7.x86_64                                                     4/4 
  Verifying  : 2:container-selinux-2.74-1.el7.noarch                                                1/4 
  Verifying  : containerd.io-1.2.5-3.1.el7.x86_64                                                   2/4 
  Verifying  : 1:docker-ce-cli-18.09.4-3.el7.x86_64                                                 3/4 
  Verifying  : 3:docker-ce-18.09.4-3.el7.x86_64                                                     4/4 

Installed:
  containerd.io.x86_64 0:1.2.5-3.1.el7                 docker-ce.x86_64 3:18.09.4-3.el7                
  docker-ce-cli.x86_64 1:18.09.4-3.el7                

Dependency Installed:
  container-selinux.noarch 2:2.74-1.el7                                                                 

Complete!

パッケージのインストール後、systemctl コマンドで Docker を起動してみます。

systemctl
[root@localhost ~]# systemctl start docker
[root@localhost ~]# 
[root@localhost ~]# 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 Tue 2019-04-09 18:16:38 JST; 1min 7s ago
     Docs: https://docs.docker.com
 Main PID: 7245 (dockerd)
    Tasks: 8
   Memory: 32.2M
   CGroup: /system.slice/docker.service
           `-7245 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Apr 09 18:16:37 localhost.localdomain dockerd[7245]: time="2019-04-09T18:16:37.946535163+09:00" le...rpc
Apr 09 18:16:37 localhost.localdomain dockerd[7245]: time="2019-04-09T18:16:37.946705996+09:00" le...rpc
Apr 09 18:16:37 localhost.localdomain dockerd[7245]: time="2019-04-09T18:16:37.978769261+09:00" le...ds"
Apr 09 18:16:37 localhost.localdomain dockerd[7245]: time="2019-04-09T18:16:37.979944044+09:00" le...t."
Apr 09 18:16:38 localhost.localdomain dockerd[7245]: time="2019-04-09T18:16:38.412627342+09:00" le...ss"
Apr 09 18:16:38 localhost.localdomain dockerd[7245]: time="2019-04-09T18:16:38.823816380+09:00" le...e."
Apr 09 18:16:38 localhost.localdomain dockerd[7245]: time="2019-04-09T18:16:38.875418551+09:00" le...9.4
Apr 09 18:16:38 localhost.localdomain dockerd[7245]: time="2019-04-09T18:16:38.875705581+09:00" le...on"
Apr 09 18:16:38 localhost.localdomain dockerd[7245]: time="2019-04-09T18:16:38.934208123+09:00" le...ck"
Apr 09 18:16:38 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

正常に起動しているようです。検証のために hello-world というコンテナを起動してみます。

docker run
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
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/

メッセージが出力されており、コンテナを正常に実行 (起動) できたようです。

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

これで Docker のインストール作業は完了です。

コンテナ起動

インストールが完了したので、試しに何かコンテナを起動してみます。

今回は NGINX コンテナを使って Web サーバを起動し、HTTP リクエストを投げてみました。

以下の様なコマンドで NGINX コンテナを起動します。

docker run -d -p 8888:80 nginx:1.15.10 

このコマンドでは Docker ホスト側 (サーバ側) の 8888 番 port 宛てのリクエストを、NGINX コンテナの 80 番 port に転送するオプションを指定しています。

docker run
[root@localhost ~]# docker run -d -p 8888:80 nginx:1.15.10           
Unable to find image 'nginx:1.15.10' locally
1.15.10: Pulling from library/nginx
27833a3ba0a5: Pull complete 
e83729dd399a: Pull complete 
ebc6a67df66d: Pull complete 
Digest: sha256:c8a861b8a1eeef6d48955a6c6d5dff8e2580f13ff4d0f549e082e7c82a8617a2
Status: Downloaded newer image for nginx:1.15.10
2c0372f25b19e4fa7cf943cdd1cd43ec1e75a88ac441eeb3d61dd0cbbc227b1d

docker ps コマンドでコンテナが起動していることを確認します。

docker ps
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
2c0372f25b19        nginx:1.15.10       "nginx -g 'daemon of…"   35 seconds ago      Up 34 seconds       0.0.0.0:8888->80/tcp   admiring_cori

コンテナ起動後、ブラウザから http://<Docker ホストの IP>:8888/ にアクセスすると、以下の様に NGINX のデフォルトの index.html がレスポンスとして返ってきました。

まとめ

今回はインストールから簡単なコンテナの起動までしか実施できていませんが、Docker を使って昨今のトレンドであるコンテナ技術に触れてみました。

コンテナには様々なメリットがあり (もちろん、デメリットもありますが)、 最近注目されている技術であるため、今後も機会があれば検証してみようと思います。

また、コンテナオーケストレーションツールのデファクトスタンダードになっている Kubernetes についても、機会があれば検証してみたいと思います。