KEMBAR78
Docker ComposeでMastodonが必要なものを梱包する話 | PDF
1
Docker Networking & Volume & Compose files
Docker Composeで
Mastodonが必要なものを
梱包する話
Engineer / Technology Evangelist, SAKURA Internet, Inc.
@zembutsu 前佛 雅人 ZEMBUTSU Masahito
2017年6月28日(水)
Compose解説編
2
http://docs.docker.jp/
https://github.com/zembutsu/docs.docker.jp/
3
4
5
https://www.patreon.com/mastodon/
https://github.com/tootsuite/documentation/blob/master/Contributing-to-Mastodon/Sponsors.md
今日の内容
Docker 「ネットワーク」と「ボリューム」
「コンテナ」と「サービス」の概念
Mastodon 用 Compose file 詳解
7
8
Mastodon
9
Mastodon
10
docker system prune -af
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all networks not used by at least one container
- all dangling imagesAre you sure you want to continue? [y/N] y
※ Docker 1.13(17.03以降)
11
コンテナの「技術」
Dockerの「仕様」
1分でDockerの概要
お さ ら い
12
Docker コンテナは「軽量な仮想マシン」ではない
“仮装化技術”ではなく、”アプリケーション配信”技術
Virtualization Technology Application Delivery Technology
一軒家 マンションなど集合住宅
間取り(玄関・居間・寝室)を建築時に決める必要
建築時に初期コストが必要であり、引っ越しも大変
同居人や予算に応じ、広さや間取りを変えられる
設備(電気や設備)は共有だが、セキュリティあり
仮想化技術 コンテナ
用途が異なるので
一概な比較は乱暴
また
併用可能である
 ハードウェアの仮装化技術、OSも何でも入る  ホスト上のリソースは共有だが、アプリは別々
13
Docker は「仮想化」 (クラウド)でも「物理」でも
“仮装化技術”ではなく、”アプリケーション配信”技術
Virtualization Technology Application Delivery Technology
アプリケーションの”コンテナ化“ 移動しやすいコンテナ・イメージの形式
Docker Engine
Linux Host OS
アプリA専用
ファイル群
アプリB専用
ファイル群
アプリA アプリB
• 互いのプロセスや
リソースが見えない
• リソースを制限できる
技術 仕様
Docker Hub
Docker
イメージ
• アプリケーションが必要な最小限のファイル
• イメージ・レイヤの仕組みを活用した、高速な移動
 自動処理(テスト・デプロイ)・Webhook連携
イメージ層の集積
14
“Docker allows you to package an application
with all of its dependencies into a standardized
unit for software development.”
www.docker.com
14
全ての依存関係をパッケージ化して、コンテナとして動かす
15
サービスを
提供したい
開発
環境
テスト
環境
準備
環境
本番
環境
都度、環境構築課題 異なるOS環境課題
都度、プロビジョニング課題 動かない課題
時間がかかる課題 遅い課題 面倒課題
アプリを
動かしたい
コンテナ
開発 テスト 準備 本番
16
技術と仕様
Technology Specification
コンテナ Docker
17
構築・移動・実行
Build Ship Run
あらゆるアプリケーションを
するためのプラットフォーム
18
詳細はウェブで…
https://www.slideshare.net/zembutsu/docker-container-image-command-introduction-2017-03
19
ネットワーク機能
サービス・ディスカバリ
Docker Networking
20
3つの Docker 標準ネットワークモデル
bridge
(bridge)
host
(host)
none
(null)
ブリッジ(bridge0 …)
veth
eth0
ethX
21
3つの Docker 標準ネットワークモデル
bridge
(bridge)
ブリッジ(bridge0 …)
veth
eth0
ethX
none
(null)
host
(host)
複数のブリッジ(ネットワーク)を定義できる
デフォルトのbridge0ブリッジは、旧仕様の
ネットワークで、挙動が異なる
22
3つの Docker 標準ネットワークモデル
bridge
(bridge)
host
(host)
none
(null)
ブリッジ(bridge0 …)
veth
eth0
ethX
ホスト側のネットワークに直接接続
ブリッジのオーバーヘッドがない一方で、
セキュリティに対する考慮が必要
23
3つの Docker 標準ネットワークモデル
bridge
(bridge)
host
(host)
ブリッジ(bridge0 …)
veth
eth0
ethX
ネットワークを追加しない限り疎通できない
none
(null)
24
3つの Docker 標準ネットワークモデル
bridge
(bridge)
host
(host)
none
(null)
ブリッジ(bridge0 …)
veth
eth0
ethX
NAT
(iptables)
+
docker-proxy
ホストと
ネットワーク
共通
疎通しない
コンテナはパブリックなIPアドレスを持ない
ホスト側のポート番号を重複して、コンテナ
のポート利用(マッピング)はできない
動的なネットワークの追加・変更・削除
25
コンテナは複数のネットワーク(ブリッジ)に接続できる
ブリッジ1(bridge)
veth
eth0
ethX
各ネットワーク内部では、動的なコンテナ名
(サービス)の名前解決機能(サービス・ディス
カバリ)を標準提供
eth0 eth1 eth0
ブリッジ2(bridge)
veth192.168.0.1
172.18.0.2 172.18.0.3 172.19.0.2 172.19.0.3
172.19.0.1
172.19.0.0/16172.19.0.0/16
サービス・ディスカバリ連携の負荷分散
26
ボリューム機能と特長
Docker Volume
27
ボリュームには3分類
ホストをマウント 名前付き
ホスト上のディレクトリ
/docker/data
/data
名前無し
volume
ボリュームの実体は、ホスト側のディレクトリ
/var/lib/docker/volumes
ボリュームはコンテナ間でデータを共有できる
volume
/data /data /etc
28
Compose とは?
基本概念と役割
Docker Compose
29
Introducing Moby Project: a new open-source project to advance the software containerization movement - Docker Blog
https://blog.docker.com/2017/04/introducing-the-moby-project/
A new upstream project to break up Docker into independent components by shykes · Pull Request #32691 · moby/moby
https://github.com/moby/moby/pull/32691
Docker と周辺ツールやプロジェクトとの関係性まとめ
references
2013
Docker
LXC
+
2014
libcontainer
ver0.9
デーモン
Linux向けコンテナ用
ライブラリ(当時開発中)
独自にライブラリを実装
2015
Docker Engine
デーモンの呼称を変更
補助ツール群の登場
Docker Swarm
Docker Compose
Docker Machine
2016 2017
runC
v1.11
OCI基準のランタイムの1つ
“docker”バイナリから
dockerd デーモンに代わり
Linux capabilityの管理
Engine (dockerd)
containerD
OCI準拠のランタイムを
動かすスーパーバイザ
v1.11
Docker独自規格
業界規格・共通技術
CNCF (Cloud Native
Computing Foundation)
の規格の1つに採用
v1.12
infrakit
v1.12
SwarmKit
platform agnostic
インフラの差違を埋めるためのツール
カーネルとユーザスペースを提供
イミュータブルなインフラのため
派生
swarm
モード
派生
hyperkit
vpnkit
datakit
コンテナシステムの組立に
特化したフレームワークを
開発するプロジェクト
https://github.com/mobyhttps://github.com/docker
30
“Compose is a tool for defining and running
multi-container Docker applications.”
複数コンテナのDockerアプリケーションを
定義し、実行するツールがCompose
31
“Compose is a tool for defining and running
multi-container Docker applications.”
複数コンテナのDockerアプリケーションを
定義し、実行するツールがCompose
docker-compose.yml
YAMLファイル
docker-compose
CLI
32
Why?
33
Docker Engine
エ ン ジ ン
$ docker run …
$ docker run …
$ docker run …
$ docker run …
34
Docker Swarm
ス ウ ォ ー ム
$ docker run …
$ docker run …
$ docker run …
$ docker run …
35
• 複数のコンテナを一斉に
操作できない
• 複数台のサーバ上で一斉に
操作できない
管理が
面倒
“Docker allows you to package an application
with all of its dependencies into a standardized
unit for software development.”
www.docker.com
36
全ての依存関係をパッケージ化して、コンテナとして動かす
37
“Fast, isolated development environments
using Docker”
www.fig.sh
38
“Fast, isolated development environments
using Docker”
www.fig.sh
39
Docker Compose
コ ン ポ ー ズ
“Compose is a tool for
defining and running
multi-container Docker
applications.”
40
41
Mastodon
42
Mastodon
43
コンテナ単位
Engine
(プロジェクト内の)
サービス単位
Compose
44
開発、テスト、ステージング
CI ワークフローに適している
• 単一ホスト上でも環境を分けたい場合に
• コンテナが作成したボリューム(データ領域)を
残したり、共有できる
• Compose ファイルを再利用できる
45
Docker Engine とデーモンの変遷
Docker Engine
Linux Kernel
・namespaces
・cgroups
LXC libcontainer runC
containerd
v0.9~
v1.11~
Version 7 Unix
chroot
jail
dockerd
v1.12~
デーモン
ライブラリ
ランタイム
docker daemon
Docker: the container engine
v1.11~
46
https://www.opencontainers.org https://www.cncf.io
コンテナ化 動的なオーケストレーション
マイクロサービス指向のアプリケーション
オープンソース・ソフトウェアの集積(stack)
オープンな業界標準のコンテナ規格と
ランタイムを策定するのが目的
“An open governance structure for the express
purpose of creating open industry standards
around container formats and runtime”
ソフトウェアのビジネスを行う企業ではなく、
ソフトウェア企業が本当に必要なもの
“Companies are realizing that they need to be a
software company, even if they are not in the
software business”
どちらも2015年6月に設立
47
https://www.opencontainers.org https://www.cncf.io
• コンテナの実行と監視(管理)
• イメージ配布
• ネットワークインタフェースと管理
• ローカル・ストレージ
• ネイティブに組み込まれたAPI
• OCI に準拠したコンテナを作成・実行
• “runc” コマンドライン・ツール
48
dockerd
Docker Engine
Dockerfile
コンテナの元となるイメージを定義
docker-compose.yml
クラスタ(swarm)上でサービス群の状態を定義
49
“Compose is a tool for defining and running
multi-container Docker applications.”
複数コンテナのDockerアプリケーションを
定義し、実行するツールがCompose
docker-compose.yml
YAMLファイル
docker-compose
CLI
50
複数のホスト・システム上を横
断するアプリケーションをス
ケール(拡大・縮小)できる機能
※コンテナに依存しない
• Marathon, chronos
• Docker swarm
• Deis
• fleet
• Apache Mesos
• DCOS (Mesosphere)
• Kubernetes
• Docker Engine
(swarm mode)
+ Docker Compose
• Rancher
• Nomad
設定ファイルをベースに
サービスを定義・維持
計算資源の抽象化
5151
Compose file 解説
ヒャッハー
Mastodonを動かそう
52
https://bit.ly/sakura170628
53
54
55
version: ‘3'
services:
db:
restart: always
image: postgres:alpine
volumes:
- ./postgres:/var/lib/postgresql/data
redis:
restart: always
image: redis:alpine
volumes:
- ./redis:/data
web:
restart: always
build: .
env_file: .env.production
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- "3000:3000"
depends_on:
- db
- redis
volumes:
- ./public/assets:/mastodon/public/assets
- ./public/system:/mastodon/public/system
streaming:
restart: always
build: .
env_file: .env.production
command: npm run start
ports:
- "4000:4000"
depends_on:
- db
- redis
sidekiq:
restart: always
build: .
env_file: .env.production
command: bundle exec sidekiq -q default -q mailers -q pull -q push
depends_on:
- db
- redis
volumes:
- ./public/system:/mastodon/public/system
mastodon docker-compose.yml
FROM ruby:2.4.1-alpine
LABEL maintainer="https://github.com/tootsuite/mastodon" ¥
description="A GNU Social-compatible microblogging server"
ENV RAILS_ENV=production ¥
NODE_ENV=production
EXPOSE 3000 4000
WORKDIR /mastodon
COPY Gemfile Gemfile.lock package.json yarn.lock /mastodon/
RUN BUILD_DEPS=" ¥
postgresql-dev ¥
libxml2-dev ¥
libxslt-dev ¥
build-base" ¥
&& apk -U upgrade && apk add ¥
$BUILD_DEPS ¥
nodejs ¥
libpq ¥
libxml2 ¥
libxslt ¥
ffmpeg ¥
file ¥
imagemagick ¥
&& npm install -g npm@3 && npm install -g yarn ¥
&& bundle install --deployment --without test development ¥
&& yarn ¥
&& yarn cache clean ¥
&& npm -g cache clean ¥
&& apk del $BUILD_DEPS ¥
&& rm -rf /tmp/* /var/cache/apk/*
COPY . /mastodon
VOLUME /mastodon/public/system /mastodon/public/assets
Dockerfile
依存関係
.env.production
# Service dependencies
REDIS_HOST=redis
REDIS_PORT=6379
DB_HOST=db
(省略)
依存関係
依存関係
57
redis
:alpine
postgres
:alpine
サービス サービス
イメージ イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
https://github.com/tootsuite/mastodon
mastodon_postgresmastodon_redis assets packs system
volumevolume volume volume volume
3000
3000
ホスト側ネットワーク
eth0等
4000
4000
58
version: "3"
services:
db:
restart: always
image: postgres:alpine
networks:
mastodon:
volumes:
- mastodon_postgres:/var/lib/postgresql/data
redis:
restart: always
image: redis:alpine
networks:
mastodon:
volumes:
- mastodon_redis:/data
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.nginx
image: mastodon_frontend:alpine
ports:
- "80:80"
- "443:443"
depends_on:
- web
- streaming
networks:
mastodon:
volumes:
- /etc/localtime:/etc/localtime:ro
- certbot:/etc/letsencrypt
sidekiq:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: bundle exec sidekiq -q default -q mailers -q pull -q push
depends_on:
- db
- redis
networks:
mastodon:
volumes:
- system:/mastodon/public/system
mta:
restart: always
image: namshi/smtp
env_file: .env.smtp
networks:
mastodon:
volumes:
- /etc/localtime:/etc/localtime:ro
networks:
mastodon:
external: true
volumes:
certbot:
external: true
mastodon_postgres:
mastodon_redis:
assets:
packs:
system:
web:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: bundle exec rails s -p 3000 -b '0.0.0.0'
#ports:
# - "3000:3000"
depends_on:
- db
- redis
networks:
mastodon:
aliases:
- mastodon_web
volumes:
- assets:/mastodon/public/assets
- packs:/mastodon/public/packs
- system:/mastodon/public/system
streaming:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: npm run start
ports:
- "4000:4000"
networks:
mastodon:
aliases:
- mastodon_streaming
depends_on:
- db
- redis
docker-compose.yml (https://github.com/zembutsu/mastodon-docker ver.)
59
version: "3"
services:
db:
restart: always
image: postgres:alpine
networks:
mastodon:
volumes:
- mastodon_postgres:/var/lib/postgresql/data
redis:
restart: always
image: redis:alpine
networks:
mastodon:
volumes:
- mastodon_redis:/data
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.nginx
image: mastodon_frontend:alpine
ports:
- "80:80"
- "443:443"
depends_on:
- web
- streaming
networks:
mastodon:
volumes:
- /etc/localtime:/etc/localtime:ro
- certbot:/etc/letsencrypt
sidekiq:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: bundle exec sidekiq -q default -q mailers -q pull -q push
depends_on:
- db
- redis
networks:
mastodon:
volumes:
- system:/mastodon/public/system
mta:
restart: always
image: namshi/smtp
env_file: .env.smtp
networks:
mastodon:
volumes:
- /etc/localtime:/etc/localtime:ro
networks:
mastodon:
external: true
volumes:
certbot:
external: true
mastodon_postgres:
mastodon_redis:
assets:
packs:
system:
web:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: bundle exec rails s -p 3000 -b '0.0.0.0'
#ports:
# - "3000:3000"
depends_on:
- db
- redis
networks:
mastodon:
aliases:
- mastodon_web
volumes:
- assets:/mastodon/public/assets
- packs:/mastodon/public/packs
- system:/mastodon/public/system
streaming:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: npm run start
ports:
- "4000:4000"
networks:
mastodon:
aliases:
- mastodon_streaming
depends_on:
- db
- redis
docker-compose.yml (https://github.com/zembutsu/mastodon-docker ver.)
60
version: "3"
services:
db:
restart: always
image: postgres:alpine
networks:
mastodon:
volumes:
- mastodon_postgres:/var/lib/postgresql/data
redis:
restart: always
image: redis:alpine
networks:
mastodon:
volumes:
- mastodon_redis:/data
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.nginx
image: mastodon_frontend:alpine
ports:
- "80:80"
- "443:443"
depends_on:
- web
- streaming
networks:
mastodon:
volumes:
- /etc/localtime:/etc/localtime:ro
- certbot:/etc/letsencrypt
sidekiq:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: bundle exec sidekiq -q default -q mailers -q pull -q push
depends_on:
- db
- redis
networks:
mastodon:
volumes:
- system:/mastodon/public/system
mta:
restart: always
image: namshi/smtp
env_file: .env.smtp
networks:
mastodon:
volumes:
- /etc/localtime:/etc/localtime:ro
networks:
mastodon:
external: true
volumes:
certbot:
external: true
mastodon_postgres:
mastodon_redis:
assets:
packs:
system:
web:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: bundle exec rails s -p 3000 -b '0.0.0.0'
#ports:
# - "3000:3000"
depends_on:
- db
- redis
networks:
mastodon:
aliases:
- mastodon_web
volumes:
- assets:/mastodon/public/assets
- packs:/mastodon/public/packs
- system:/mastodon/public/system
streaming:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: npm run start
ports:
- "4000:4000"
networks:
mastodon:
aliases:
- mastodon_streaming
depends_on:
- db
- redis
docker-compose.yml (https://github.com/zembutsu/mastodon-docker ver.)
61
networks:
mastodon:
external: true
volumes:
certbot:
external: true
mastodon_postgres:
mastodon_redis:
assets:
packs:
system:
62
networks:
mastodon:
external: true
volumes:
certbot:
external: true
mastodon_postgres:
mastodon_redis:
assets:
packs:
system:
docker network create mastodon
docker run -itd -P nginx --net=mastodon
docker volume create certbot
docker run -itd -P nginx –v certbot:/etc/
“external: true” 指定があれば、
• ホスト上の他のサービスやコンテナと
共有できる
• docker-compose down の影響を
受けない(外部リソース扱い)
63
redis
:alpine
postgres
:alpine
サービス サービス
イメージ イメージ
nginx
:alpine
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
namshi/smtp
:latest
サービス
イメージ
https://github.com/tootsuite/mastodon
ネットワーク
192.168.0.0/16 mastdon (external)
ホスト側ネットワーク
eth0等
mastodon_postgresmastodon_redis certbot
(external)
assets packs system
volumevolume volume volume volume volume
80 443
80 443
64
redis
:alpine
postgres
:alpine
サービス サービス
イメージ イメージ
nginx
:alpine
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
namshi/smtp
:latest
サービス
イメージ
https://github.com/tootsuite/mastodon
ネットワーク
192.168.0.0/16 mastdon (external)
ホスト側ネットワーク
eth0等
mastodon_postgresmastodon_redis certbot
(external)
assets packs system
volumevolume volume volume volume volume
80 443
80 443
services:
volumes:
networks:
65
redis
:alpine
postgres
:alpine
サービス サービス
イメージ イメージ
nginx
:alpine
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
gargron/mastodon
:v1.4.6
サービス
イメージ
namshi/smtp
:latest
サービス
イメージ
https://github.com/tootsuite/mastodon
ネットワーク
192.168.0.0/16 mastdon (external)
ホスト側ネットワーク
eth0等
mastodon_postgresmastodon_redis certbot
(external)
assets packs system
volumevolume volume volume volume volume
80 443
80 443
services:
volumes:
networks:
66
docker-compose build
docker-compose pull
docker-compose up -d
docker-compose logs -f
docker-compose stop
docker-compose rm -v /down
67
docker stack
68
docker stack deploy -c docker-compose.yml [NAME]
docker service は「サービス」単位
docker stack は「アプリケーション全体」かつ
swarm mode で Docker Compose 互換機能を提供
ポイント
69
docker stack deploy -c docker-compose.yml [NAME]
70
$ docker stack deploy -c docker-compose.yml web
Creating service web_web
$ docker stack ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE
ERROR PORTS
uhm1xxq1u70w web_web.1 zembutsu/docker-sample-nginx:latest frontend-01 Running Running 45
seconds ago
ccmokdadpjqx web_web.2 zembutsu/docker-sample-nginx:latest frontend-02 Running Running 28
seconds ago
cflckriidpt0 web_web.3 zembutsu/docker-sample-nginx:latest frontend-03 Running Running 33
seconds ago
$ docker stack services web
ID NAME MODE REPLICAS IMAGE PORTS
yunncblhngu6 web_web replicated 3/3 zembutsu/docker-sample-nginx:latest *:80->80/tcp
= docker service ps web
= docker service ls
71
version: '3'
services:
web:
image: zembutsu/docker-sample-nginx
deploy:
replicas: 3
resources:
limits:
cpus: "0.1"
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
internal:
aliases:
- web
volumes:
- /etc/localtime:/etc/localtime:ro
networks:
internal:
スケールできる
(replicas指定)
ホストを分散できる
(constraints)
※ただし、データをどのように同期す
るかという問題はあるが、Docker
Engine 本体側では関わらない領域
72
https://alpinelinux.org/
73
https://alpinelinux.org/
超小容量Linuxディストリビューション
Alpine Linux
$ docker images ls
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest a41a7446062d 11 days ago 3.96MB
74
7575
サービスを梱包する話
Docker 「ネットワーク」と「ボリューム」
「コンテナ」と「サービス」の概念
Mastodon 用 Compose file 詳解
Compose
77
3つの Docker 標準ネットワークモデル
bridge
(bridge)
host
(host)
none
(null)
ブリッジ(bridge0 …)
veth
eth0
ethX
NAT
(iptables)
+
docker-proxy
ホストと
ネットワーク
共通
疎通しない
コンテナはパブリックなIPアドレスを持ない
ホスト側のポート番号を重複して、コンテナ
のポート利用(マッピング)はできない
動的なネットワークの追加・変更・削除
78
アプリケーションを簡単に試すのに
いいぞ
私からは以上です
ありがとうございました
何か気になる所がありますか?
ご参考:Docker 日本語ドキュメント
http://docs.docker.jp/
http://slideshare.net/zembutsu
twitter: @zembutsu

Docker ComposeでMastodonが必要なものを梱包する話