fbpx

Chef Container 0.2.4 で Docker コンテナを作成する #opschef_ja #getchef_ja #docker

この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。

注意: Chef Provisioning と Docker の組み合わせとして knife-container がありましたがプロジェクト終了しており、現在は chef-provisioning-docker の利用が推奨されています。Chef Provisioning Docker については Chef ProvisioningとDockerでSerfクラスタ環境を作成する を参照してください(2015/07/08 追記)。

Chef Container がベータ版としてリリースされてからおよそ半年が経ちました(Chef Container 0.2.0 (beta) で Docker コンテナを作成する #opschef_ja #getchef_ja)。本稿では、2014/12時点のリリース版である0.2.4を対象に、どのような変化があったかを見ていきます。

Ubuntu 14.04 LTSホストにDockerをインストール

Docker installationに従い、Ubuntu 14.04 LTSにDockerをインストールします。

ubuntu@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.1 LTS
Release: 14.04
Codename: trusty
ubuntu@ubuntu:~$

Dockerのインストールを簡単化するスクリプトを利用してインストールを行います。

ubuntu@ubuntu:~$ curl -sSL https://get.docker.com/ubuntu/ | sudo sh
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.1IooFwtSPm --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
gpg: 鍵A88D21E9をhkpからサーバーkeyserver.ubuntu.comに要求
gpg: 鍵A88D21E9: 公開鍵“Docker Release Tool (releasedocker) <docker@dotcloud.com>”を読み込みました
gpg: 処理数の合計: 1
gpg: 読込み: 1 (RSA: 1)
:
:
:
aufs-tools (1:3.2+20130722-1.1) を設定しています ...
liberror-perl (0.17-1.1) を設定しています ...
git-man (1:1.9.1-1) を設定しています ...
git (1:1.9.1-1) を設定しています ...
cgroup-lite (1.9) を設定しています ...
cgroup-lite start/running
lxc-docker-1.3.2 (1.3.2) を設定しています ...
docker start/running, process 2693
Processing triggers for ureadahead (0.100.0-16) ...
lxc-docker (1.3.2) を設定しています ...
Processing triggers for libc-bin (2.19-0ubuntu6.4) ...
ubuntu@ubuntu:~$ </docker@dotcloud.com>

操作ユーザであるubuntudockerグループに所属させます。

ubuntu@ubuntu:~$ groups
ubuntu adm cdrom sudo dip plugdev lpadmin sambashare
ubuntu@ubuntu:~$

ubuntu@ubuntu:~$ sudo gpasswd -a ubuntu docker
Adding user ubuntu to group docker
ubuntu@ubuntu:~$

グループ変更を反映させるため、一旦ログアウトし、ログインし直します。

ubuntu@ubuntu:~$ groups
ubuntu adm cdrom sudo dip plugdev lpadmin sambashare docker
ubuntu@ubuntu:~$

Dockerの動作テストを行います。次のようにubuntuコンテナ内で/bin/bashコマンドを実行し、コマンドプロンプトが表示されたら成功です。

ubuntu@ubuntu:~$ docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu' locally
ubuntu:latest: The image you are pulling has been verified
511136ea3c5a: Pull complete
01bf15a18638: Pull complete
30541f8f3062: Pull complete
e1cdf371fbde: Pull complete
9bd07e480c5b: Pull complete
Status: Downloaded newer image for ubuntu:latest
root@79c746da3f3c:/#

exitでDockerコンテナから抜けて、次の手順に移ります。

Ubuntu 14.04 LTSホストにChef DKをインストール

Chef Development Kit | Chef DownloadsからUbuntu 14.04 LTS用のChef DKパッケージをダウンロードします。今回はバージョン0.3.5をインストールします。

ubuntu@ubuntu:~$ wget https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/12.04/x86_64/chefdk_0.3.5-1_amd64.deb
:
ubuntu@ubuntu:~$ sudo dpkg -i chefdk_0.3.5-1_amd64.deb
:
chefdk (0.3.5-1) を設定しています ...
Thank you for installing Chef Development Kit!
ubuntu@ubuntu:~$

knife-containerプラグインをインストール

chef gemコマンドを用いてknife-containerプラグインをインストールします。

ubuntu@ubuntu:~$ chef gem install knife-container --no-document
Fetching: knife-container-0.2.4.gem (100%)
WARNING: You don't have /home/ubuntu/.chefdk/gem/ruby/2.1.0/bin in your PATH,
gem executables will not run.
Successfully installed knife-container-0.2.4
1 gem installed
ubuntu@ubuntu:~$

chef-repoの準備

必須ではありませんがchef generate repoコマンドでchef-repoを準備しておきます。以降、chef-repoディレクトリ内で作業を行います。

ubuntu@ubuntu:~$ chef generate repo chef-repo
Compiling Cookbooks...
Recipe: code_generator::repo
:
- update content in file /home/ubuntu/chef-repo/.gitignore from none to 3523c4
(diff output suppressed by config)
ubuntu@ubuntu:~$

Docker Hub Registryの確認

Chef Containerでは、対象とするDockerイメージはChef Clientrunitchef-initをあらかじめ含んでいないといけません。以前Chef社によって準備されていたのはchef/ubuntu-12.04のみでした。現在のChef社のDocker Hub Registryは登録イメージが拡充されています。

  • chef/ubuntu-14.04
  • chef/ubuntu-12.04
  • chef/debian-7
  • chef/debian-6
  • chef/centos-7
  • chef/centos-6

chef/ubuntu-12.04をベースイメージとする

まず以前からと同じく、chef/ubuntu-12.04をベースイメージとして、demo/apache22という名前のDockerコンテキストを作成します。設定項目は、

  • イメージ名: demo/apache22
  • ベースイメージ: -f chef/ubuntu-12.04:latest
  • run-list: -r 'recipe[apt],recipe[apache2]'
  • ローカルモードで実行: -z
  • Berkshelfを利用: -b

としてknife container docker initコマンドを実行します。

ubuntu@ubuntu:~/chef-repo$ knife container docker init demo/apache22 -f chef/ubuntu-12.04:latest -r 'recipe[apt],recipe[apache2]' -z -b
WARNING: No knife configuration file found
Compiling Cookbooks...
Recipe: knife_container::docker_init
:
:
:
Downloading base image: chef/ubuntu-12.04:latest. This process may take awhile...
Tagging base image chef/ubuntu-12.04 as demo/apache22

Context Created: /home/ubuntu/chef-repo/dockerfiles/demo/apache22
ubuntu@ubuntu:~/chef-repo$

chef/first-boot.jsonにはrunitが管理するサービスをcontainerのAttributeとして設定します。ここではapache2を起動するようにします。また、Dockerfileを編集して80/tcpをホストに結び付けるようにします。

ubuntu@ubuntu:~/chef-repo$ vi dockerfiles/demo/apache22/chef/first-boot.json
ubuntu@ubuntu:~/chef-repo$

ubuntu@ubuntu:~/chef-repo$ vi dockerfiles/demo/apache22/Dockerfile
ubuntu@ubuntu:~/chef-repo$


diff --git a/dockerfiles/demo/apache22/Dockerfile b/dockerfiles/demo/apache22/Dockerfile
index 5dd844f..b640834 100644
--- a/dockerfiles/demo/apache22/Dockerfile
+++ b/dockerfiles/demo/apache22/Dockerfile
@@ -5,3 +5,4 @@ RUN chef-init --bootstrap
RUN rm -rf /etc/chef/secure/*
ENTRYPOINT ["chef-init"]
CMD ["--onboot"]
+EXPOSE 80
diff --git a/dockerfiles/demo/apache22/chef/first-boot.json b/dockerfiles/demo/apache22/chef/first-boot.json
index ad41416..10378d6 100644
--- a/dockerfiles/demo/apache22/chef/first-boot.json
+++ b/dockerfiles/demo/apache22/chef/first-boot.json
@@ -2,5 +2,10 @@
"run_list": [
"recipe[apt]",
"recipe[apache2]"
- ]
-}
\ No newline at end of file
+ ],
+ "container_service": {
+ "apache2": {
+ "command": "/usr/sbin/apache2 -k start"
+ }
+ }
+}

knife container docker buildコマンドを実行します。先にローカルモードで実行したので、こちらもローカルモードで実行します。

ubuntu@ubuntu:~/chef-repo$ knife container docker build demo/apache22 -z
WARNING: No knife configuration file found
Resolving cookbook dependencies...
Fetching cookbook index from https://supermarket.getchef.com...
Installing apache2 (3.0.0)
Installing apt (2.6.0)
Installing iptables (0.14.0)
Installing logrotate (1.7.0)
Resolving cookbook dependencies...
Using apache2 (3.0.0)
Using apt (2.6.0)
Using logrotate (1.7.0)
Using iptables (0.14.0)
Vendoring apache2 (3.0.0) to /home/ubuntu/chef-repo/dockerfiles/demo/apache22/chef/cookbooks/apache2
Vendoring apt (2.6.0) to /home/ubuntu/chef-repo/dockerfiles/demo/apache22/chef/cookbooks/apt
Vendoring iptables (0.14.0) to /home/ubuntu/chef-repo/dockerfiles/demo/apache22/chef/cookbooks/iptables
Vendoring logrotate (1.7.0) to /home/ubuntu/chef-repo/dockerfiles/demo/apache22/chef/cookbooks/logrotate
Sending build context to Docker daemon 747 kB
Sending build context to Docker daemon
Step 0 : FROM demo/apache22
---> 50d3c5c9e133
Step 1 : ADD chef/ /etc/chef/
---> 86c3df0a4864
Removing intermediate container c5c057b323fd
Step 2 : RUN chef-init --bootstrap
---> Running in 8e67576af0c0
[2014-12-12T09:04:16+00:00] INFO: Starting Supervisor...
[2014-12-12T09:04:16+00:00] INFO: Supervisor pid: 13
[2014-12-12T09:04:21+00:00] INFO: Starting chef-client run...
[2014-12-12T09:04:22+00:00] INFO: Starting chef-zero on host localhost, port 8889 with repository at repository at /etc/chef
One version per cookbook

[2014-12-12T09:04:22+00:00] INFO: Forking chef instance to converge...
[2014-12-12T09:04:22+00:00] INFO: *** Chef 11.16.2 ***
[2014-12-12T09:04:22+00:00] INFO: Chef-client pid: 21
[2014-12-12T09:04:23+00:00] INFO: HTTP Request Returned 404 Not Found : Object not found: http://localhost:8889/nodes/demo-apache22-build
[2014-12-12T09:04:23+00:00] INFO: Setting the run_list to ["recipe[apt]", "recipe[apache2]"] from CLI options
[2014-12-12T09:04:23+00:00] INFO: Run List is [recipe[apt], recipe[apache2]]
[2014-12-12T09:04:23+00:00] INFO: Run List expands to [apt, apache2]
[2014-12-12T09:04:23+00:00] INFO: Starting Chef Run for demo-apache22-build
[2014-12-12T09:04:23+00:00] INFO: Running start handlers
[2014-12-12T09:04:23+00:00] INFO: Start handlers complete.
[2014-12-12T09:04:23+00:00] INFO: HTTP Request Returned 404 Not Found : Object not found: /reports/nodes/demo-apache22-build/runs
[2014-12-12T09:04:24+00:00] INFO: Loading cookbooks [apt@2.6.0, apache2@3.0.0, iptables@0.14.0, logrotate@1.7.0]
:
:
:
[2014-12-12T09:05:59+00:00] INFO: service[apache2] reloaded
[2014-12-12T09:05:59+00:00] INFO: Chef Run complete in 95.250918468 seconds
[2014-12-12T09:05:59+00:00] INFO: Running report handlers
[2014-12-12T09:05:59+00:00] INFO: Report handlers complete
[2014-12-12T09:06:01+00:00] INFO: Deleting client key...
---> 61c43ba0ef17
Removing intermediate container 8e67576af0c0
Step 3 : RUN rm -rf /etc/chef/secure/*
---> Running in 5fc80bb3b9f0
---> eae9e38aa524
Removing intermediate container 5fc80bb3b9f0
Step 4 : ENTRYPOINT chef-init
---> Running in 6bd4c8f65ae6
---> bc82efe84472
Removing intermediate container 6bd4c8f65ae6
Step 5 : CMD --onboot
---> Running in eb5c6ae59d5c
---> a754cd25a313
Removing intermediate container eb5c6ae59d5c
Step 6 : EXPOSE 80
---> Running in 583ea3977424
---> eebea766e0b7
Removing intermediate container 583ea3977424
Successfully built eebea766e0b7
ubuntu@ubuntu:~/chef-repo$

Dockerイメージがビルドできました。

ubuntu@ubuntu:~/chef-repo$ docker images | head -3
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
demo/apache22 latest eebea766e0b7 12 seconds ago 403.6 MB
ubuntu latest 9bd07e480c5b 7 days ago 192.7 MB
ubuntu@ubuntu:~/chef-repo$

起動してみます。

ubuntu@ubuntu:~/chef-repo$ docker run -p 80:80 -d demo/apache22
af659fb44fae00e5698cd00dfc7978d6872080d4041139f1028bd27f8bf66f00
ubuntu@ubuntu:~/chef-repo$

ubuntu@ubuntu:~/chef-repo$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af659fb44fae demo/apache22:latest "chef-init --onboot" 19 seconds ago Up 18 seconds 0.0.0.0:80->80/tcp distracted_pike
ubuntu@ubuntu:~/chef-repo$

apache2が動くDockerコンテナが起動しました。

ubuntu@ubuntu:~$ docker top af659fb44fae
UID PID PPID C STIME TTY TIME CMD
root 12695 765 0 18:12 ? 00:00:00 /opt/chef/embedded/bin/ruby /usr/bin/chef-init --onboot
root 12726 12695 0 18:12 ? 00:00:00 /opt/chef/embedded/bin/runsvdir -P /opt/chef/service 'log:...........................................................................................................................................................................................................................................................................................................................................................................................................'.
root 12958 12695 0 18:12 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 13077 12958 0 18:12 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 13078 12958 0 18:12 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 13079 12958 0 18:12 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 13080 12958 0 18:12 ? 00:00:00 /usr/sbin/apache2 -k start
ubuntu@ubuntu:~$

コンテナのIPアドレスを確認してアクセスしてみます。

ubuntu@ubuntu:~/chef-repo$ docker inspect --format="{{ .NetworkSettings.IPAddress }}" af659fb44fae
172.17.0.16
ubuntu@ubuntu:~/chef-repo$


ubuntu@ubuntu:~/chef-repo$ curl 172.17.0.16

Not Found

The requested URL / was not found on this server.


Apache Server at 172.17.0.16 Port 80



ubuntu@ubuntu:~/chef-repo$

ホストに結びつけた80/tcpにもアクセスできます。


ubuntu@ubuntu:~/chef-repo$ curl 127.0.0.1

Not Found

The requested URL / was not found on this server.


Apache Server at 127.0.0.1 Port 80



ubuntu@ubuntu:~/chef-repo$

まとめ

ベータ版と大きな使い勝手の変更はありません。何よりChef社がChef Container用のDockerイメージを増強したことによって、用途が広がったと言えるでしょう。

Author

Chef・Docker・Mirantis製品などの技術要素に加えて、会議の進め方・文章の書き方などの業務改善にも取り組んでいます。「Chef活用ガイド」共著のほか、Debian Official Developerもやっています。

Daisuke Higuchiの記事一覧

新規CTA