tkuchikiの日記

新ブログ https://blog.tkuchiki.net

chef で Docker container を provisioning する

検証した環境は Docker 1.3.2、chef 12.0.3 ですが、
バージョンには依存していないと思います。

例は、以下の様なファイル構成です。

$ tree .
.
├── client.rb
├── cookbooks
│   └── nginx
│       └── recipes
│           └── default.rb
├── Dockerfile
├── nodes
│   └── node.json
└── roles

5 directories, 4 files

Dockerfile

内容は、以下のとおりです。

  • chef-client を実行するのに必要なファイルを ADD
  • chef を install
  • chef-client 実行
  • nginx を foreground で実行

roles ディレクトリの ADD もしていますが、
例では使用していません。

client.rb

設定は適宜変更してください。
local_mode の設定は、chef-client に -z をつければ、書かなくても動きます。

default.rb

nginx の公式 repo から nginx を install する recipe です。


node.json

node の json です。


docker build

あとは、

$ docker build -t chef-nginx .

とすれば chef-client が実行され、image を作成できます。

確認

build が成功している時点で確認する必要はないのですが、
実際に dokcer run して動かしてみると、

$ docker run -d -p 80 chef-nginx

$ docker ps -l
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                   NAMES
7c22ce748a32        chef-test:latest    "/usr/sbin/nginx -g    43 minutes ago      Up 43 minutes       0.0.0.0:49157->80/tcp   berserk_goodall

$ curl -s localhost:49157
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

nginx が動いています。

まとめ

chef で docker container の provisioning をする例を示しました。
chef-client を使うようにしたので、この先 chef-solo が使えなくなっても大丈夫ですね。