Dockerイメージの理解とコンテナのライフサイクル

170
-1

Published on

2016年3月22日(月) 18:30 ~
JAWS-UGコンテナ支部×JAWS-UG CLI専門支部 #1 発表資料
jawsug-container.connpass.com/event/27537/
会場:NHN テコラス株式会社
#jawsug #jawsug_cli #jawsug_ct

Published in: Software

Dockerイメージの理解とコンテナのライフサイクル

  1. 1. Docker イメージの理解と コンテナのライフサイクル JAWS-UGコンテナ支部 × JAWS-UG CLI専門支部 #1 発表資料 2016年3月22日(火) @zembutsu Technology Evangelist; Creationline, Inc. What are Docker Images and Containers, and Container's life cycle. 背景画像CREDIT:スフィア / PIXTA(ピクスタ)
  2. 2. 本資料の内容 • Docker とは何なのか?お復習い • Docker イメージと Docker コンテナの違い • Docker イメージとレイヤ • コンテナのライフサイクルと主なコマンド ※ 内容は Docker 初心者の方(なんとなく Docker を知っているけれど、 コンテナとイメージの違いが分からないケース)を想定してます。 ・
  3. 3. 3 私は誰なのか? ‣ @zembutsu a.k.a. 前佛雅人 - Technology Evangelist; Creationline, Inc. – 2 yrs - Technical Trainer; Docker Authorized Trainer – 0.8 yr - Data Center Operations Engineer – 15+ yrs 興味:運用監視自動化、趣味でOSSやクラウド系の検証・情報発信 - SlideShare http://slideshare.net/zembutsu - Blog http://pocketstudio.jp/log3 Why am I here? +MasahitoZembutsu
  4. 4. 4 Docker って何だったっけ? ‣ Docker アプリケーションを開発・移動・実行するためのプラットフォーム • 設計思想は、開発者が簡単にアプリケーションを動かす環境を作る オープンソース・コミュニティ及び支援会社としての Docker, Inc. • 2013年3月 Python Conference US のライトニング・トークで発表 – 前身は PaaS 事業者だった dotCloud 社(後に事業売却) – https://github.com/docker/ • Open Container Initiative を通して各社と協調 – https://www.opencontainers.org/ developing, shipping, running
  5. 5. 開発者が簡単にアプリケーションを開発・デプロイできる仕組み プラットフォーム
  6. 6. 6 Dockerコンテナ ‣ コンテナとは ホスト OS の kernel 機能を使い、複数のルート・ファイルシステムを実行 • 各ルート・ファイルシステムをコンテナ (container) と呼ぶ • コンテナは各々のリソースを持つ – プロセス、メモリ、デバイス、ネットワーク コンテナとは Linux カーネルの技術を使う • Namespaces (名前空間によるプロセス間の隔離・分離;isolation) • Cgroups(CPU・メモリ・Disk I/Oのリソース制限) ‣ コンテナの実行に必要な環境 • Docker Engine(エンジン) – コンテナを移動・実行するための必要なプログラム(デーモン)
  7. 7. OS ( Linux ) 物理/仮想サーバ Docker エンジン ( docker デーモン ) Linux kernel コンテナ コンテナ コンテナ リモート API docker クライアント ・docker コマンド ・Kitematic (GUI)
  8. 8. /sbin/init PID 1 alice PID 2 bob PID 3 docker PID 4 httpd PID 1 コンテナA コンテナB ruby PID 1 chris.rb PID 2 httpd PID 5 chris.rb PID 7 ruby PID 6 Dockerコンテナはプロセスのア イソレーション(isolation;分離・ 独立)を実現します。コンテナ毎 にCPUやメモリ等のリソースや、 ネットワークを割り当て可能です。 プロセスはホスト上に普通に起 動していても、コンテナ内はホ スト側や他のコンテナの状況を 見ることができません。 ホスト上のプロセス
  9. 9. / /etc /bin /data コンテナAのファイルシステム … … コンテナBのファイルシステム /etc (/data/ubuntu/etc) /data/ubuntu /data/centos /bin (/data/ubuntu/bin) /etc (/data/ubuntu/etc) /bin (/data/ubuntu/bin) / / ホスト上のファイルシステム コンテナ内ではコンテナの中で ルート・ディレクトリ(chroot風)を それぞれマウントしています。 お互いのコンテナは、通常はそ のまま相互に参照できません。
  10. 10. OS ( Linux ) 物理/仮想サーバ Docker エンジン ( docker デーモン ) Linux kernel コンテナ コンテナ コンテナ リモート API docker クライアント ・docker コマンド ・Kitematic (GUI) Docker コンテナ
  11. 11. docker クライアント docker エンジン Docker Hub $ docker run hello-world Hello from Docker. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com For more examples and ideas, visit: https://docs.docker.com/userguide/ run pull レジストリ latest イメージ タグ hello-world レポジトリ latest イメージ タグ コンテナ化した hello-worldの実行 • Docker Engine は「docker run」実行時にローカルにイ メージがなければレジストリ (DockerHubが標準)から イメージをダウンロードする。 そのため初回実行時は時間 かかる場合がある • hello-worldコンテナは画面 出力が終わると、プロセス が停止(コンテナが停止)
  12. 12. $ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 03f4658f8b78: Pull complete a3ed95caeb02: Pull complete Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7 Status: Downloaded newer image for hello-world:latest Hello from Docker. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. (省略) Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com For more examples and ideas, visit: https://docs.docker.com/userguide/ 「run」を実行すると、ホスト上に イメージがなければ、自動的に Docker Hub からダウンロードし た後、コンテナを実行しています。 hello-worldコンテナを実行 コンテナを実行するファイルを含むイ メージがないので取得(pull)する 最新のイメージを取得完了 タグを指定しなかったので「latest」
  13. 13. $ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 03f4658f8b78: Pull complete a3ed95caeb02: Pull complete Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7 Status: Downloaded newer image for hello-world:latest Hello from Docker. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. (省略) Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com For more examples and ideas, visit: https://docs.docker.com/userguide/ Docker イメージ
  14. 14. 14 Docker イメージ ‣ Dockerイメージは コンテナを実行する時に必要な ファイルシステム • イメージ・レイヤ(層)の集合体 – ファイルの実体やメタ情報を含む • レイヤには親子関係がある • 差分情報のみを記録する • Read Only で書き込みできない 共通するレイヤはイメージ間で共有できる • ディスク容量の削減や高いポータビリティを実現する
  15. 15. $ docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu 203137e8afd5: Pull complete 2ff1bbbe9310: Pull complete 933ae2486129: Pull complete a3ed95caeb02: Pull complete Digest: sha256:1bea66e185d3464fec1abda32ffaf2a11de69833cfcf81bd2b9a5be147776814 Status: Downloaded newer image for ubuntu:latest/ レイヤごとに 並列ダウンロード
  16. 16. $ docker pull wordpress Using default tag: latest latest: Pulling from library/wordpress fdd5d7827f33: Already exists a3ed95caeb02: Download complete 8c80f2e38113: Download complete 2da85bfb1ac0: Download complete 1da50ec818af: Download complete b2799c7ad5c9: Downloading 1.113 MB/2.844 MB 4893554c0107: Download complete b1d739e1b940: Waiting bd103e3f6195: Waiting aa560ff33ce6: Waiting 1deabfa10759: Waiting 91e6991f7a34: Waiting 7234c82b998e: Waiting 6bf8bdf2e550: Waiting a5c7e6ead07c: Waiting fe011342f195: Waiting c6dd706ba27e: Waiting 35d564cafd69: Waiting 730edfa5d07f: Waiting Digest: sha256:bfd7e102741d73cce4ec58b2d937586c670f31df1c80aeaf4d5c525eb3c6ac06 Status: Downloaded newer image for wordpress:latest ダウンロード済みのレイヤ なので再取得しない ダウンロード中 ダウンロード済み ダウンロード待ち
  17. 17. Nginx イメージの構造 902b87aaaec9 3 months ago /bin/sh -c #(nop) ADD file:e1dd18493a216ecd0c 125.2 MB 9a61b6b1315e 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B aface2a79f55 3 months ago /bin/sh -c #(nop) MAINTAINER NGINX Docker Mai 0 B 5dd2638d10a1 3 months ago /bin/sh -c apt-key adv --keyserver hkp://pgp. 1.997 kB 97df1ddba09e 3 months ago /bin/sh -c echo "deb http://nginx.org/package 221 B e7e7a55e9264 10 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.9.4-1~j 0 B 72b67c8ad0ca 10 weeks ago /bin/sh -c apt-get update && apt-get inst 7.695 MB 9108e25be489 10 weeks ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx/ 11 B 6dda3f3a8c05 10 weeks ago /bin/sh -c ln -sf /dev/stderr /var/log/nginx/ 11 B 42d2189f6cbe 10 weeks ago /bin/sh -c #(nop) VOLUME [/var/cache/nginx] 0 B 3cb7f49c6bc4 10 weeks ago /bin/sh -c #(nop) EXPOSE 443/tcp 80/tcp 0 B a486da044a3f 10 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon o 0 B $ docker history nginx IMAGE CREATED CREATED BY SIZE COMMENT docker history <image id/name> コンテナ作成の履歴を辿るには docker history コマンドが便利
  18. 18. ベース・イメージ (公式イメージ等) イメージ層 Docker コンテナを起動するとは 読み込み専用 (Read Only)
  19. 19. ベース・イメージ (公式) イメージ層 Docker コンテナを起動するとは 読み込み専用 (Read Only) ・新しいイメージ・レイヤの 自動的な割り当て ・イメージ内のバイナリを 隔離されたプロセスで起動 (コンテナ化された状態) docker run <opts> <image:tag> docker diff
  20. 20. Dockerコンテナとイメージのライフサイクル
  21. 21. イメージ Read Only コンテナ用 レイヤ 読み書き可 • Docker イメージとはイメージ・レイヤ (rootファイルシステム)とメタデー タ(何のコマンドを実行するのか、ど のポートを開くかなどの管理情報)の 集合体であり、読み込み専用 • それぞれのレイヤは ID を持つ • コンテナの起動とは、Dockerイメージ 上に読み書き可能なレイヤを追加し、 指定されたプロセスを隔離された状態 やシステムリソース上で実行すること docker pull … イメージの取得 docker run … コンテナの実行
  22. 22. イメージ Read Only コンテナ用 レイヤ 読み書き可 • イメージを作成するには3つの方法 1. Docker Hub から取得する 2. コンテナの内容を手動でコミットし、 新しいイメージを作成する 3. Dockerfileを使ってイメージを自動構 築する(自動コミットの繰り返し) docker commit … イメージの作成 docker build… イメージの自動構築(自動commit)
  23. 23. イメージ Read Only コンテナ用 レイヤ 読み書き可 docker history … イメージの履歴表示 docker diff… 元イメージとコンテナの差分表示 docker inspect… コンテナやイメージの調査
  24. 24. イメージ Read Only コンテナ用 レイヤ 読み書き可 docker push … Dockerイメージの送信 docker login … ログイン docker tag … イメージをタグ付け docker search … 検索
  25. 25. イメージ Read Only コンテナ用 レイヤ 読み書き可 Dockerコンテナとイメージのライフサイクル docker ps … コンテナ一覧や状態の表示 docker rmi … コンテナイメージ削除 docker rm … コンテナ削除 docker images … 一覧
  26. 26. Copy on Write(コピー・オン・ライト) 読み込み専用 (Read Only) • Read Only のイメージ層上 のファイルに対する変更は、 必ずコピー作業を行う • 内部実装はストレージ・ドラ イバに依存する(パフォーマ ンスに影響が出る) • CoWを回避するには ボリュームを使う 書き込む前に コピーしてから コンテナのレイヤ上に 変更を反映
  27. 27. ボリュームはコンテナ用のレイヤとは分離 イメージ Read Only コンテナ用 レイヤ 読み書き可 • イメージ・レイヤの仕様(Copy on Write)を 回避できる • docker commit してもボリューム内容は反 映されない。 • 複数のコンテナでボリュームを共有できる • ホスト側のファイルやディレクトリをマウン ト可能
  28. 28. まとめ • Docker とはアプリケーションを開発・移動・実行する プラットフォームの役割で、Linuxカーネル技術を使う • Docker イメージはイメージ・レイヤの集合体 • Read Only のイメージを使って Docker コンテナを実行 • イメージ・レイヤの性質上コピー・オン・ライトの処理が 発生するが、ボリュームを使えば回避可能
  29. 29. 29 何か気になる所はありますか? Dockerイメージ入門とコンテナのライフサイクル ‣ 日本語ドキュメントはこちら http://docs.docker.jp
  30. 30. 30 ‣ アーキテクチャの理解 http://docs.docker.jp/engine/understanding-docker.html ‣ イメージの構築 http://docs.docker.jp/engine/userguide/containers/dockerimages.html ‣ イメージ、コンテナ、ストレージ・ドライバの理解 http://docs.docker.jp/engine/userguide/storagedriver/imagesandcontainers.html ‣ ストレージ・ドライバの選択 http://docs.docker.jp/engine/userguide/storagedriver/selectadriver.html ‣ コンテナでデータを管理する http://docs.docker.jp/engine/userguide/containers/dockervolumes.html より詳しい情報

×