概要
Figというyamlファイルを用いて複数のDockerコンテナを一元管理するツールを使ってみます。
環境
- Ubuntu 14.04
- Docker 1.4.1
- Fig 1.0.1
インストール
事前準備
Fig
はDocker 1.3
以上で動作します。CoreOSや通常のaptなどでインストールできるバージョンは低いので最新版にします。
を参考に最新版をインストールしてください。
Figのインストール
rootになって以下を実行します。
$ sudo su # curl -L https://github.com/docker/fig/releases/download/1.0.1/fig-`uname -s`-`uname -m` > /usr/local/bin/fig; chmod +x /usr/local/bin/fig
確認します。
$ fig --version fig 1.0.1
使ってみる
yamlファイルの作成
今回はNginxのコンテナを作成してみます。fig.yml
に以下を書いてください。
web: image: nginx ports: - '80:80'
コンテナの起動
fig.yml
のあるフォルダでコマンド実行します。fig up
だとフォアグラウンドで起動してしまうので、-d
をつけてバックグラウンドで起動します。
$ fig up -d
イメージのダウンロード&起動が完了したらプロセスを確認します。
$ fig ps Name Command State Ports -------------------------------------------------------------------------- vagrant_web_1 nginx -g daemon off; Up 443/tcp, 0.0.0.0:80->80/tcp
動作確認します。
$ curl localhost:80 <!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>
コンテナの停止
$ fig stop Stopping vagrant_web_1...
確認します。
$ fig ps Name Command State Ports ----------------------------------------------------- vagrant_web_1 nginx -g daemon off; Exit 0
スケールアウト
そもままだとポートがかぶるのでfig.yml
を少し修正します。
web: image: nginx ports: - '80'
まず1つだけ起動します。
$ fig up -d Creating vagrant_web_1...
次にスケールアウトします。
$ fig scale web=3 Starting vagrant_web_2... Starting vagrant_web_3...
一瞬でスケールアウトできますね。
当然ポートは自動で割り当てられるので、fig ps
やdocker inspect
で調べたりする必要があります。
$ fig ps Name Command State Ports ----------------------------------------------------------------------------- vagrant_web_1 nginx -g daemon off; Up 443/tcp, 0.0.0.0:49153->80/tcp vagrant_web_2 nginx -g daemon off; Up 443/tcp, 0.0.0.0:49154->80/tcp vagrant_web_3 nginx -g daemon off; Up 443/tcp, 0.0.0.0:49155->80/tcp
繋がるか確認してみましょう。
$ curl localhost:49154 <!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>
コンテナの削除
Figだとコンテナの削除も簡単です。
$ fig rm Going to remove vagrant_web_3, vagrant_web_2, vagrant_web_1 Are you sure? [yN] y Removing vagrant_web_3... Removing vagrant_web_2... Removing vagrant_web_1...
以上です。お疲れ様でした。