Docker は、コンテナ型の仮想環境を作成、配布、実行するためのプラットフォームです。CentOS 7 をインストールして Docker を利用できるように設定します。
- サーバーの準備
- CentOS のインストール
- Docker のセットアップ
- 必要な関連パッケージをインストール
- Docker CE のリポジトリの設定
- Docker CE のインストール
- サービスの起動
- グループ docker にユーザを追加
- hello-world イメージを使って動作確認
- 参考サイト
サーバーの準備
CentOS のインストール
CentOS は、ダウンロードした CentOS-7-x86_64-DVD-1810.iso を Fedora Media Writer で USB メモリに焼いて、評価用 PC へインストールしました。今回は「最小限のインストール」でインストールしました。
なお、Docker はデフォルトで /var/lib/docker/ 以下の領域にコンテナのイメージやインスタンスを格納、生成します。一方、CentOS インストーラのおまかせ(自動)でパーティションを設定すると、インストール先のストレージ容量が十分にある場合、システム領域 / に 50GB を確保し、残りをスワップ領域とユーザー領域 /home に LVM で論理ボリュームを割り当てます。
このままでインストール後に多くのコンテナを扱おうとすると、システム領域 / を使うため容量が足りなくなります。そのため、手動でパーティションを設定して、/var と /home 用にそれぞれ 200GB 程度の論理ボリュームの領域を確保しました。
CentOS インストール後、他の PC から ssh でログインしてパッケージを最新の状態にアップデートしておきます。
[bitwalk@fedora-pc ~]$ ssh 192.168.0.22 ... (省略) ... [bitwalk@centos7 ~]$ sudo yum -y update [sudo] bitwalk のパスワード: ... (省略) ... 完了しました! [bitwalk@centos7 ~]$
ちなみに、パーティションは以下のようになっています。
[bitwalk@centos7 ~]$ df -h ファイルシス サイズ 使用 残り 使用% マウント位置 /dev/mapper/centos-root 50G 1.4G 49G 3% / devtmpfs 1.8G 0 1.8G 0% /dev tmpfs 1.8G 0 1.8G 0% /dev/shm tmpfs 1.8G 8.9M 1.8G 1% /run tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup /dev/sda1 1014M 193M 822M 19% /boot /dev/mapper/centos-home 200G 33M 200G 1% /home /dev/mapper/centos-var 211G 207M 211G 1% /var tmpfs 368M 0 368M 0% /run/user/1000 [bitwalk@centos7 ~]$
Docker のセットアップ
必要な関連パッケージをインストール
Docker CE をインストールする方法はいくつかありますが、ここでは一番簡単な方法を取ることにします。すなわち、Docker のリポジトリをセットアップしてインストールします。
※ 2017年 Docker 社は CE(コミュニティエディション、無償版)と EE(エンタープライズエディション、有償版)の二種類のリリース提供を開始しました。
まず、リポジトリやストレージドライバを扱うために必要なパッケージをインストールします。
[bitwalk@centos7 ~]$ sudo yum -y install yum-utils device-mapper-persistent-data lvm2
[sudo] bitwalk のパスワード:
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.riken.jp
* extras: ftp.riken.jp
* updates: ftp.riken.jp
パッケージ device-mapper-persistent-data-0.7.3-3.el7.x86_64 はインストール済みか最新バージョンです
パッケージ 7:lvm2-2.02.180-10.el7_6.8.x86_64 はインストール済みか最新バージョンです
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ yum-utils.noarch 0:1.1.31-50.el7 を インストール
...
(省略)
...
検証中 : python-chardet-2.2.1-1.el7_1.noarch 4/4
インストール:
yum-utils.noarch 0:1.1.31-50.el7
依存性関連をインストールしました:
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
完了しました!
[bitwalk@centos7 ~]$
Docker CE のリポジトリの設定
次に、Docker CE の stable (安定版)リポジトリをセットアップします。
[bitwalk@centos7 ~]$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[sudo] bitwalk のパスワード:
読み込んだプラグイン:fastestmirror
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
[bitwalk@centos7 ~]$
リポジトリ追加後、yum パッケージ・インデックスを更新します。
[bitwalk@centos7 ~]$ sudo yum makecache fast
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.riken.jp
* extras: ftp.riken.jp
* updates: ftp.riken.jp
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 3.4 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:00
メタデータのキャッシュを作成しました
[bitwalk@centos7 ~]$
Docker CE のインストール
Docker CEをインストールします。
[bitwalk@centos7 ~]$ sudo yum -y install docker-ce
[sudo] bitwalk のパスワード:
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.riken.jp
* extras: ftp.riken.jp
* updates: ftp.riken.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ docker-ce.x86_64 3:19.03.1-3.el7 を インストール
--> 依存性の処理をしています: container-selinux >= 2:2.74 のパッケージ: 3:docker-ce-19.03.1-3.el7.x86_64
...
(省略)
...
検証中 : 2:container-selinux-2.99-1.el7_6.noarch 11/11
インストール:
docker-ce.x86_64 3:19.03.1-3.el7
依存性関連をインストールしました:
audit-libs-python.x86_64 0:2.8.4-4.el7 checkpolicy.x86_64 0:2.5-8.el7
container-selinux.noarch 2:2.99-1.el7_6 containerd.io.x86_64 0:1.2.6-3.3.el7
docker-ce-cli.x86_64 1:19.03.1-3.el7 libcgroup.x86_64 0:0.41-20.el7
libsemanage-python.x86_64 0:2.5-14.el7 policycoreutils-python.x86_64 0:2.5-29.el7_6.1
python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-4.el7
完了しました!
[bitwalk@centos7 ~]$
サービスの起動
Docker のサービスを起動します。
[bitwalk@centos7 ~]$ sudo systemctl start docker [sudo] bitwalk のパスワード: [bitwalk@centos7 ~]$ sudo 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 土 2019-07-27 20:35:24 JST; 10s ago Docs: https://docs.docker.com Main PID: 27893 (dockerd) Tasks: 11 Memory: 39.9M CGroup: /system.slice/docker.service └─27893 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/con... 7月 27 20:35:19 centos7 dockerd[27893]: time="2019-07-27T20:35:19.54517182...c 7月 27 20:35:19 centos7 dockerd[27893]: time="2019-07-27T20:35:19.54542850...c 7月 27 20:35:19 centos7 dockerd[27893]: time="2019-07-27T20:35:19.54685374...c 7月 27 20:35:19 centos7 dockerd[27893]: time="2019-07-27T20:35:19.86344758..." 7月 27 20:35:22 centos7 dockerd[27893]: time="2019-07-27T20:35:22.20617975..." 7月 27 20:35:23 centos7 dockerd[27893]: time="2019-07-27T20:35:23.76180745..." 7月 27 20:35:23 centos7 dockerd[27893]: time="2019-07-27T20:35:23.89615003...1 7月 27 20:35:23 centos7 dockerd[27893]: time="2019-07-27T20:35:23.90030657..." 7月 27 20:35:24 centos7 dockerd[27893]: time="2019-07-27T20:35:24.21006351..." 7月 27 20:35:24 centos7 systemd[1]: Started Docker Application Container ...e. Hint: Some lines were ellipsized, use -l to show in full. [bitwalk@centos7 ~]$ sudo systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. [bitwalk@centos7 ~]$
起動した Docker サービスの情報を確認します。
[bitwalk@centos7 ~]$ sudo docker info
[sudo] bitwalk のパスワード:
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 19.03.1
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 ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
runc version: 425e105d5a03fabd737a126ad93d62a9eeede87f
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.591GiB
Name: centos7
ID: O26I:LZFL:R2II:VSIZ:43JA:DUUZ:H575:TYZ3:4XVI:YARJ:J5RJ:RX4E
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
[bitwalk@centos7 ~]$
グループ docker にユーザを追加
今のところ sudo で docker コマンドを実行していますが、今後、毎回 sudo で実行するのは面倒です。そこで、docker という名のグループを作って(実際には docker-ce のインストール時にグループ docker が作成されていたようです)ユーザをそこに所属させて sudo なしに docker コマンドを実行できるようにします。
※ ただし、参考サイト [6] で解説されているように、このやり方はセキュリティ上の問題があることを理解しておく必要があります。
[bitwalk@centos7 ~]$ sudo groupadd docker [sudo] bitwalk のパスワード: groupadd: グループ 'docker' は既に存在します [bitwalk@centos7 ~]$ sudo gpasswd -a $USER docker ユーザ bitwalk をグループ docker に追加 [bitwalk@centos7 ~]$ sudo systemctl restart docker [bitwalk@centos7 ~]$ exit ログアウト Connection to 192.168.0.22 closed. [bitwalk@fedora-pc ~]$ ssh 192.168.0.22 bitwalk@192.168.0.22's password: Last login: Sat Jul 27 20:27:56 2019 from 192.168.0.10 [bitwalk@centos7 ~]$ docker info Client: Debug Mode: false Server: Containers: 0 ... (省略) ... Live Restore Enabled: false [bitwalk@centos7 ~]$
グループ docker に自分のユーザアカウント(この例では bitwalk)を追加した後、docker のサービスを再起動します。ログアウト → 再ログインして docker コマンドを sudo 無しで実行できることを確認しています。
hello-world イメージを使って動作確認
公開されている hello-world という Docker イメージを取得して、コンテナを起動させてみます。
[bitwalk@centos7 docker]$ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f 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/ [bitwalk@centos7 docker]$
コンテナの状態を確認すると、以下のようになっています。
[bitwalk@centos7 docker]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
895fe50eb652 hello-world "/hello" 4 minutes ago Exited (0) 4 minutes ago jolly_bose
[bitwalk@centos7 docker]$
コンテナを削除するには以下のようにします。
[bitwalk@centos7 docker]$ docker rm jolly_bose jolly_bose [bitwalk@centos7 docker]$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [bitwalk@centos7 docker]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 6 months ago 1.84kB [bitwalk@centos7 docker]$
コンテナは削除されますが hello-world のイメージは削除されていません。イメージを削除するには以下のようにします。
[bitwalk@centos7 docker]$ docker rmi hello-world Untagged: hello-world:latest Untagged: hello-world@sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3 [bitwalk@centos7 docker]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE [bitwalk@centos7 docker]$
参考サイト
- CentOS 用 Docker CE の入手 — Docker-docs-ja 17.06.Beta ドキュメント
- Install Docker on CentOS 7 - Qiita [2019-02-08]
- Docker入門(第二回)~Dockerセットアップ、コンテナ起動~ | さくらのナレッジ [2018-03-12]
- Dockerのインストール&操作方法(CentOS7.3) - Qiita [2017-12-10]
- Dockerが商用版Dockerとして「Docker Enterprise Edition」発表、認証済みイメージやプラグインなど提供。無償版は「Community Edition」に - Publickey [2017-03-06]
- Dockerでユーザーをdockerグループに追加することの危険性を理解しよう - Qiita [2019-03-13]
にほんブログ村
0 件のコメント:
コメントを投稿