ビルドサーバで使うdocker
TRANSCRIPT
2014/12/19 shin1x1
DevLove 関西
ビルドサーバで使う Docker
Agenda
(c) 2014 Masashi Shinbara @shin1x1
• 開発構成 • 旧ビルドサーバ(Docker導入前) • 現ビルドサーバ(Docker導入後) • Docker in CI • 運用してみて • まとめ
開発構成
(c) 2014 Masashi Shinbara @shin1x1
PHPプロジェクト
(c) 2014 Masashi Shinbara @shin1x1
• Web アプリケーション開発 • PHP / Webサーバ / DB / KVS 等々 • PHPUnit で自動テスト
ビルドサーバ
(c) 2014 Masashi Shinbara @shin1x1
• Jenkins サーバ • PHPUnit によるテスト実行 • デプロイ [ステージング / 本番] • (カバレッジ計測、静的解析等)
旧ビルドサーバ
(c) 2014 Masashi Shinbara @shin1x1
(c) 2014 Masashi Shinbara @shin1x1
構成
Mac
(1) git push
(2) hook
(3) git pull
社内サーバ(4) notification
社内Jenkinsサーバ
(c) 2014 Masashi Shinbara @shin1x1
• 2009年頃構築 • 複数プロジェクトを管理 • 1サーバに別バージョンのPHPやDBを同居 • ビルドタスクを欲張る (カバレッジ、APIドキュメント、静的解析等)
困っていたこと
(c) 2014 Masashi Shinbara @shin1x1
• プロジェクト毎の環境構築が面倒 (パスやポートを変えて同居)
• トラブルシューティングが面倒 • git push の度にサーバが唸る
困っていたこと
(c) 2014 Masashi Shinbara @shin1x1
• プロジェクト毎の環境構築が面倒 (パスやポートを変えて同居)
• トラブルシューティングが面倒 • git push の度にサーバが唸る
困っていたこと
(c) 2014 Masashi Shinbara @shin1x1
• プロジェクト毎の環境構築が面倒 (パスやポートを変えて同居)
• トラブルシューティングが面倒 • git push の度にサーバが唸る
現ビルドサーバ
(c) 2014 Masashi Shinbara @shin1x1
(c) 2014 Masashi Shinbara @shin1x1
構成
Mac
private repo
(1) git push
ビルドサーバ(さくらVPS)
(2) hook
(3) git pull
(4) docker build & docker run
(5) notification
現Jenkinサーバ
(c) 2014 Masashi Shinbara @shin1x1
• さくらのVPS(2G)/ 月々1,580円 • Memory: 2GB • CPU: 仮想3Core • HDD: 200GB • CentOS 6.6
構成のポイント
(c) 2014 Masashi Shinbara @shin1x1
• 外部サーバ、SaaS を利用 • Dockerでビルド環境を構築 • タスクを自動テスト実行に絞る
Docker in CI
(c) 2014 Masashi Shinbara @shin1x1
Dockerの利用方針
(c) 2014 Masashi Shinbara @shin1x1
• アプリケーションが動作する環境を 1 コンテナに集約
• Dockerfile を push して、 Jenkins サーバでイメージを生成
• アプリケーションは、ホストからマウント コンテナは、実行環境に徹する
(c) 2014 Masashi Shinbara @shin1x1
構成
Mac
private repo
(1) git push
CI サーバ(さくらVPS)
(2) hook
(3) git pull
(4) docker build & docker run
(c) 2014 Masashi Shinbara @shin1x1
Dockerfile
Mac
Dockerfile
private repo
CI サーバ(さくらVPS)
Dockerfile
(c) 2014 Masashi Shinbara @shin1x1
• Dockerfile とプロビジョンファイルを用意 • プロビジョンには Ansible を利用 • 開発プロジェクトと同じリポジトリで管理
FROM centos:centos6
# Ansible インストール、プロビジョンRUN …
# Supervisor インストールRUN …
# アプリケーションセットアップ、テスト実行CMD …
Dockerfile
FROM centos:centos6
# Ansible インストールRUN …
# Supervisor インストールRUN …
# アプリケーションセットアップ、テスト実行CMD …
Dockerfile
CentOS Official Image
FROM centos:centos6
# Ansible インストール、プロビジョンRUN …
# Supervisor インストールRUN …
# アプリケーションセットアップ、テスト実行CMD …
Dockerfile
プロビジョン
FROM centos:centos6
# Ansible インストール、プロビジョンRUN …
# Supervisor インストールRUN …
# アプリケーションセットアップ、テスト実行CMD …
Dockerfile
サービス起動用
FROM centos:centos6
# Ansible インストール、プロビジョンRUN …
# Supervisor インストールRUN …
# アプリケーションセットアップ、テスト実行CMD …
Dockerfile
コンテナ実行コマンド
Ansible
(c) 2014 Masashi Shinbara @shin1x1
• コンテナにインストール • ローカルコネクションで自身にプロビジョン • playbookやroleを別環境 (開発、ステージング等)と共有
• レイヤを節約
RUN rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpmRUN yum -y install ansible libselinux-python sudoADD provision /opt/provisionRUN ansible-playbook /opt/provision/docker.yml --connection=local -i /opt/provision/localhost -vv
Ansible
RUN rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpmRUN yum -y install ansible libselinux-python sudoADD provision /opt/provisionRUN ansible-playbook /opt/provision/docker.yml --connection=local -i /opt/provision/localhost -vv
Ansible
Ansibleインストール
RUN rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpmRUN yum -y install ansible libselinux-python sudoADD provision /opt/provisionRUN ansible-playbook /opt/provision/docker.yml --connection=local -i /opt/provision/localhost -vv
Ansible
プロビジョンファイルをインポート
RUN rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpmRUN yum -y install ansible libselinux-python sudoADD provision /opt/provisionRUN ansible-playbook /opt/provision/docker.yml --connection=local -i /opt/provision/localhost -vv
Ansible
ansible-playbook 実行 (ローカルコネクション)
Supervisor
(c) 2014 Masashi Shinbara @shin1x1
• デーモンプロセスを起動(死活監視込) • 1プロセスで、複数のデーモンを起動 • Webサーバ、DB を起動
RUN yum -y install python-setuptoolsRUN easy_install supervisorADD docker/supervisord.conf /etc/supervisord.conf
Supervisor
Supervisor インストール
RUN yum -y install python-setuptoolsRUN easy_install supervisorADD docker/supervisord.conf /etc/supervisord.conf
Supervisor
設定ファイルをインポート
コンテナコマンド
(c) 2014 Masashi Shinbara @shin1x1
• Supervisor を起動 • composer install(依存解決) • DBマイグレーション • PHPUnit(自動テスト)実行
コンテナコマンド
CMD /usr/bin/supervisord -c /etc/supervisord.conf && \ cd /share && \ /usr/bin/composer install && \ php artisan migrate --env=testing && \ ./vendor/bin/phpunit --log-junit \ /share/reports/junit.xml \ —coverage-html /share/reports/coverage \ —coverage-clover /share/reports/coverage.xml
CMD /usr/bin/supervisord -c /etc/supervisord.conf && \ cd /share && \ /usr/bin/composer install && \ php artisan migrate --env=testing && \ ./vendor/bin/phpunit --log-junit \ /share/reports/junit.xml \ —coverage-html /share/reports/coverage \ —coverage-clover /share/reports/coverage.xml
コンテナコマンド
Supervisor 起動
CMD /usr/bin/supervisord -c /etc/supervisord.conf && \ cd /share && \ /usr/bin/composer install && \ php artisan migrate --env=testing && \ ./vendor/bin/phpunit --log-junit \ /share/reports/junit.xml \ —coverage-html /share/reports/coverage \ —coverage-clover /share/reports/coverage.xml
コンテナコマンド
依存解決
CMD /usr/bin/supervisord -c /etc/supervisord.conf && \ cd /share && \ /usr/bin/composer install && \ php artisan migrate --env=testing && \ ./vendor/bin/phpunit --log-junit \ /share/reports/junit.xml \ —coverage-html /share/reports/coverage \ —coverage-clover /share/reports/coverage.xml
コンテナコマンド
DBマイグレーション
CMD /usr/bin/supervisord -c /etc/supervisord.conf && \ cd /share && \ /usr/bin/composer install && \ php artisan migrate --env=testing && \ ./vendor/bin/phpunit --log-junit \ /share/reports/junit.xml \ —coverage-html /share/reports/coverage \ —coverage-clover /share/reports/coverage.xml
コンテナコマンド
自動テスト実行
(c) 2014 Masashi Shinbara @shin1x1
Jenkinsで実行
Mac
private repo
CI サーバ(さくらVPS)
docker build && run
Jenkinsで実行
(c) 2014 Masashi Shinbara @shin1x1
• git pull でリポジトリから Dockerfile 取得 • docker build でイメージ生成 • docker run でコンテナ実行 • 結果レポートを Jenkins で表示
Jenkinsで実行
(c) 2014 Masashi Shinbara @shin1x1
Jenkinsで実行
(c) 2014 Masashi Shinbara @shin1x1
sudo docker build -t ${JOB_NAME} .sudo docker run -v ${WORKSPACE}/src:/share ${JOB_NAME}
docker build
(c) 2014 Masashi Shinbara @shin1x1
• Dockerfile からイメージをビルド • ジョブ名(${JOB_NAME})をイメージ名に
sudo docker build -t ${JOB_NAME} .
docker run
(c) 2014 Masashi Shinbara @shin1x1
• 生成したイメージからコンテナ実行 • -v で、アプリケーションディレクトリを マウント
sudo docker run -v ${WORKSPACE}/src:/share ${JOB_NAME}
マウントディレクトリ対応
(c) 2014 Masashi Shinbara @shin1x1
${WORKSPACE}/src + application_code + build/
/share + application_code + build/
ホスト(Jenkins) コンテナ
アプリケーション
(c) 2014 Masashi Shinbara @shin1x1
${WORKSPACE}/src + application_code + build/
/share + application_code + build/
ホスト(Jenkins) コンテナ
アプリケーションを参照
結果レポート
(c) 2014 Masashi Shinbara @shin1x1
${WORKSPACE}/src + application_code + build/
/share + application_code + build/
ホスト(Jenkins) コンテナ
/share/build/ に 結果ログを出力
結果レポート
(c) 2014 Masashi Shinbara @shin1x1
${WORKSPACE}/src + application_code + build/
/share + application_code + build/
ホスト(Jenkins) コンテナ
build/ のログから 結果レポート生成
結果レポート
(c) 2014 Masashi Shinbara @shin1x1
8ヶ月運用してみて
(c) 2014 Masashi Shinbara @shin1x1
Dockerコンテナの良さ
(c) 2014 Masashi Shinbara @shin1x1
• プロジェクト毎に独立した環境 • 軽量なので、低スペック機でも利用可能 • 毎回、作って、使って、捨ててなので、管理が楽 • どこでも同じコンテナが作れるので、メンテが楽 (boot2docker で検証できる)
突然のテスト失敗
(c) 2014 Masashi Shinbara @shin1x1
• 開発環境で all green だったテストがこける • なぜか依存解決で失敗 • コンテナから外部へ通信できない>< • docker デーモン再起動で解決
突然のテスト失敗2
(c) 2014 Masashi Shinbara @shin1x1
• docker デーモン再起動でも復旧せず • yum install でエラー • EPEL の CA証明書の期限切れ • ベースイメージを更新 (yum update ca-certificates でも ok)
今後
(c) 2014 Masashi Shinbara @shin1x1
• ステージングサーバへのコンテナデプロイ • docker コンテナ実行環境は クラウドサービスに任せたい
• コンテナレポジトリも任せたい • 全部自分でやるなら、PaaS の方が楽
まとめ
(c) 2014 Masashi Shinbara @shin1x1
(c) 2014 Masashi Shinbara @shin1x1
まとめ
• マルチな環境を同居させるのに便利 • ビルド、ジョブワーカーなど 使い捨ての環境なら、実用的
• 常に起動するサービスなら、 docker デーモン運用、デプロイ、監視等々を Dockerクラウドサービスに任せたい
@shin1x1
(c) 2014 Masashi Shinbara @shin1x1