SlideShare a Scribd company logo
Building production
server on Docker
Hiroshi Miura
2015.4.11
第
1
5
3
回
小
江
戸
ら
ぐ
4
月
の
オ
フ
な
集
ま
り
Who am I
Introduction
Methods
Results
Discussion
What is Docker
Introduction
Methods
Results
Discussion
● コンテナを柔軟に使う技術
Dockerfile コンテナイメージ
docker HUB実行環境
docker build
docker push
docker pulldocker run
What is Container
Introduction
Methods
Results
Discussion
● 通常プロセスを仮想化技術のように使う
● リソース隔離+名前空間隔離
● jail, chroot の進化したもの
物理ハードウエア( CPU, メモリ、 NIC)
Linux カーネル
ユーザ空間 ユーザ空間
プロセス
プロセス
プロセス
プロセス
feature of Docker
Introduction
Methods
Results
Discussion
● 階層化技術
● スペースと時間を削減可能
Dockerfile
docker HUB実行環境
ベースイメー
ジ
差分ダウンロード
Ubuntu
Dockerfile
Ubuntu
ruby
Ubuntu
rubyUbuntu
APP 実行環境
ruby
Docker install
Introduction
Methods
Results
Discussion
● Docker
– http://docs.docker.com/installation/
● Docker-compose
– オーケストレーションツール
– http://docs.docker.com/compose/install/
$ wget -qO- https://get.docker.com/ | sh
$ curl -L
https://github.com/docker/compose/releases/download/1.1.0/docker-
compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
Docker つかうだけ
Introduction
Methods
Results
Discussion
● redmine を立ち上げる
– 準備するファイル : docker-compose.yml
$ docker-compose up -d
postgresql:
image: sameersbn/postgresql:9.4
environment:
- DB_USER=redmine
- DB_PASS=phatiphohsukeuwo
- DB_NAME=redmine_production
redmine:
image: sameersbn/redmine:3.0.1
links:
- postgresql:postgresql
environment:
- DB_USER=redmine
- DB_PASS=phatiphohsukeuwo
- DB_NAME=redmine_production
ports:
- "80:80"
Dockerfile の書き方
Introduction
Methods
Results
Discussion
● FROM < ベースイメージ >
● MAINTAINER < 作成者 >
● ADD < 設定ファイルのテンプレート、イン
ストールスクリプト > < 行き先 >
● RUN < コマンド>
● RUN <上記で��加したスクリプト>
● EXPOSE <ポート番号>
● VOLUMES <データ用ディレクトリ>
● ENTRYPOINT <実行用コマンド>
● CMD <実行引数>
おすすめの書き方
Introduction
Methods
Results
Discussion
● インストールスクリプトは
– 共通部、個別部の2段構成
● 複数のコンテナに共通するステージ
– 途中段階のベースイメージを作る
● ADD,RUN は乱発しない
– それぞれ階層になる
● docker キャッシュで開発効率化可能。
● 段数上限あり
– &&でつなぐ →可読性低下
● サイズ削減する処理を最後に追加
実例 :rbenv
Introduction
Methods
Results
Discussion
● rbenv: ruby の実行環境
● 共通のテンプレートとして整備する
● ruby で作られるアプリの実行基盤とな
る
● 複数の Ruby バージョンを選択可能に
● 必要なバージョンのみで最小サイズ
Ubuntu
rbenv
ruby 2.0
Ubuntu
rbenv
ruby 2.1
Ubuntu
rbenv
ruby 2.2
実例 :rbenv
Introduction
Methods
Results
Discussion
● Github で Dockerfile
● Docker HUB のリポジトリ
– Docker pull するイメージ
– TAG
● 自動 BUILD 設定
– レシピとイメージが1:1を保証
– マルウエア等が含まれていないことを
ソースで確認可能
実例 :rbenv:ruby インストール
Introduction
Methods
Results
Discussion
#!/bin/bash
( いろいろ : 環境変数設定、ユーザ作成、 gem や Bundle のパス設定)
apt-get update
apt-get -y install build-essential curl ca-certificates 
gcc g++ make bison libgdbm-dev ( いろいろ略)
rbenv install ${RUBY_VER}
rbenv global ${RUBY_VER}
gem update --system --no-document
gem install bundler --no-rdoc --no-ri
rbenv rehash
# cleanup
apt-get -y remove libgdbm-dev libncursesw5-dev libncurses5-dev 
libreadline6-dev build-essential curl ( などなど、開発ファイルのみ)
apt-get clean
apt-get -y autoremove
実例 :kandan
Introduction
Methods
Results
Discussion
● kandan: OSS の Web チャットソフト
● lingr,slack,hipchat の代わり
● github.com/kandanapp/kandan
Building production server on docker
実例 :kandan
Introduction
Methods
Results
Discussion
● kandan の Dockerfile
● インストールスクリプト
● 実行スクリプト
kandan: install
Introduction
Methods
Results
Discussion
#!/bin/bash
( いろいろ : 環境変数設定、ユーザ作成、 gem や Bundle のパス設定)
apt-get update
apt-get -y install supervisor build-essential curl unzip git-core gcc
curl -sL https://deb.nodesource.com/setup | bash -
apt-get -y install nodejs
apt-get -y install sqlite3 libmysqlclient18 libmysqlclient-dev libsqlite3-dev
( いろいろ)
cat > ${KANDAN_CONF} <<__EOL__
[program:kandan]
command=${BUNDLE} exec thin start -e production
autostart=true
autorestart=false
username=${RUN_USER}
directory=${INSTALL_DIR}/kandan
stdout_logfile=${LOG_DIR}/%(program_name)s.log
stderr_logfile=${LOG_DIR}/%(program_name)s.log
environment=RBENV_ROOT=${RBENV_ROOT}
__EOL__
sudo -u ${RUN_USER} -E -H 
git clone --depth 1 -b i18n --single-branch https://github.com/miurahr/kandan.git
kandan: install (cont.)
Introduction
Methods
Results
Discussion
# cleanup dev files
apt-get -y remove 
unzip gcc g++ make curl git-core build-essential 
libmysqlclient-dev libsqlite3-dev libpq-dev 
libcurl4-openssl-dev libpcre3-dev libxml2-dev libxslt-dev 
libreadline-gplv2-dev
# clean apt caches and more
apt-get clean
apt-get -y autoremove
find /var/lib/apt/lists/ -type f -exec rm -f {} ;
exit 0
kandan: init
Introduction
Methods
Results
Discussion
cat > ${INSTALL_DIR}/kandan/config/database.yml << __EOL1__
production:
adapter: mysql2
reconnect: false
timeout: 5000
database: ${DB_NAME}
host: ${DB_HOST}
port: ${DB_PORT}
pool: ${DB_POOL}
username: ${DB_USER}
password: ${DB_PASS}
__EOL1__
# start supervisord
exec /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
kandan: init (cont.)
Introduction
Methods
Results
Discussion
prog="mysqladmin -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USER} 
${DB_PASS:+-p$DB_PASS} status"
timeout=60
echo -n "Waiting for database server to accept connections"
while ! ${prog} >/dev/null 2>&1
do
timeout=$(expr $timeout - 1)
if [ $timeout -eq 0 ]; then
echo -e "nCould not connect to database server. Aborting..."
exit 1
fi
echo -n "."
sleep 1
done
● コンテナから DBMS を同時に起動した場
合に、 DBMS の起動を待つ必要あり
kandan: init (cont.)
Introduction
Methods
Results
Discussion
# start supervisord
exec /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
● 最終的に supervisord で
アプリデーモンを起動
– コンテナ内には、
kernel や init, upstart,systemd はない
オーケストレーションツール
Introduction
Methods
Results
Discussion
●
コマンドを複数を順序良く実行するのは大変
  ↓例↓
docker run -d -e "VIRTUAL_HOST=example.com" -e
"MONGO_URL=mongodb://dbserver:27017/libreboard"
-e "ROOT_URL=http://example.com" -p 5555:5555
miurahr/libreboard
●
オーケストレーションツールを使う
docker-compose.yml
Libreboard:
image: miurahr/libreboard
environments:
- VIRTUAL_HOST=example.com
ports:
- 5555:5555
Acknowledgements
● Kandan: Kadan app project
● Hubot: Github.com
● Docker, dockerHub: Docker.com
Acknowledgements
● Great docker recipes by Sameer Naik
https://github.com/sameersbn
Reference
● Presentation:
https://www.slideshare.net/miurahr/building-
● Source:
https://github.com/miurahr
● Docker HUB registory:
https://hub.docker.com/u/miurahr/

More Related Content

Building production server on docker