dockerとdocker hubの操作と概念
TRANSCRIPT
黒い画面が大好きなヒト向け
コマンドで学ぶ
DockerとDockerHubの
概念と操作
Engineer / Technology Evangelist@zembutsu 前佛 雅人 ZEMBUTSU Masahito
JAWS-UG コンテナ支部 入門編 #4 2017年3月30日(木)
http://docs.docker.jp/
2
このドキュメントの翻訳をしているヒト
誰?
• 運用系(サーバ) … データセンタの運用・サポート対応
• HashiCorp / Munin / Zabbix / Docker などに興味
• エンジニアのためのプレゼン研究会
• ドキュメント翻訳
• 稲作農家(富山県滑川市出身)
• インターネットの力で普通の人が価値を高められる社会
3
Software Degisn 2017年2月号→
Authorized Docker Trainer (2016.6~)
今日お伝えすること
コンテナは技術、Dockerは仕様
Docker Hubが中継点
コマンドでコンテナとイメージを操作
技術と仕様
6
技術と仕様Technology Specificationコンテナ Docker
7
サービスを提供したい
開発環境
テスト環境
準備環境
本番環境
都度、環境構築課題 異なるOS環境課題
都度、プロビジョニング課題 動かない課題
時間がかかる課題 遅い課題 面倒課題アプリを動かしたい
コンテナ開発 テスト 準備 本番
“Docker allows you to package an application
with all of its dependencies into a standardized
unit for software development.”
www.docker.com
8
全ての依存関係をパッケージ化して、コンテナとして動かす
9
コンテナ
20世紀最大の発明
10
11
技術と仕様Technology Specificationコンテナ Docker
12
Linux kernelのコンテナ化技術
• 名前空間とコントロール・グループの制御により、•
プロセス環境を分離し、リソースも制限できる
• この技術をDocker Engine (dockerd) で制御
namespace control group (cgroup)
isolate
13
Docker Engine とデーモンの変遷
Docker Engine
Linux Kernel・namespaces・cgroups
LXC libcontainer runC
containerd
v0.9~
v1.11~
Version 7 Unixchroot
jail
dockerd
v1.12~
デーモン
ライブラリランタイム
docker daemon
Docker: the container engine
v1.11~
14
15
Docker はサーバ・クライアント型モデル
OS ( Linux )
物理/仮想サーバ
Docker エンジン( dockerd デーモン )
Linux kernel
コンテナ コンテナ コンテナ
リモートAPI
dockerクライアント TCP あるいは
Unix ソケットドメイン
containerdRuntime: runC (OCI規格準拠)
・docker コマンドLinux, Mac OS X, Windows
・Kitematic (GUI)Mac OS X, Windows
・Docker Compose
・Docker Swarm
16
httpdPID 1
コンテナA コンテナB
rubyPID 1
chris.rbPID 2
/sbin/initPID 1
containerdPID 5
httpdPID 6
rubyPID 7
chris.rbPID 8
alicePID 2
bobPID 3
PPID 1 PPID 1
PPID 4
PPID 5 PPID 5
PPID 7
PPID 1
dockerdPID 4
コンテナのプロセス
17
コンテナAのファイルシステム
… …
コンテナBのファイルシステム
/etc /bin /etc /bin
/ /
/
/etc /data
/data/ubuntu /data/centos
/bin
コンテナのファイルシステム
18コンテナAの
ファイルシステム
… …
コンテナBのファイルシステム
/etc(/data/ubuntu/etc)
/bin(/data/ubuntu/bin)
/etc(/data/centos/etc)
/bin(/data/centos/bin)
/ /
httpdPID 1
プロセスA プロセスB
rubyPID 1
chris.rbPID 2
コンテナA コンテナB
名前空間の isolate・プロセス・ファイルシステム・ネットワーク・ホスト名・UID・GID
リソース制限・CPU・メモリ・I/O・ディスク・クォータ
コンテナの実行
19
技術と仕様Technology Specificationコンテナ Docker
20
Dockerイメージ• イメージ・レイヤの積み重ね
• 読み込み専用
Dockerコンテナ• イメージ・レイヤを1つのファイルシステムとみなす
• 読み書き可能なイメージ・レイヤを持つ
21
$ docker image pull ubuntuUsing default tag: latestlatest: Pulling from library/ubuntu203137e8afd5: Pull complet2ff1bbbe9310: Pull complete933ae2486129: Pull completea3ed95caeb02: Pull completeDigest: sha256:1bea66e185d3464fec1abda32ffaf2a11de69833cfcf81bd2b9a5be147776814Status: Downloaded newer image for ubuntu:latest/
22
$ docker pull wordpressUsing default tag: latestlatest: Pulling from library/wordpressfdd5d7827f33: Already existsa3ed95caeb02: Download complete8c80f2e38113: Download complete2da85bfb1ac0: Download complete1da50ec818af: Download completeb2799c7ad5c9: Downloading 1.113 MB/2.844 MB4893554c0107: Download completeb1d739e1b940: Waitingbd103e3f6195: Waitingaa560ff33ce6: Waiting1deabfa10759: Waiting91e6991f7a34: Waiting7234c82b998e: Waiting6bf8bdf2e550: Waitinga5c7e6ead07c: Waitingfe011342f195: Waitingc6dd706ba27e: Waiting35d564cafd69: Waiting730edfa5d07f: Waiting
Digest: sha256:bfd7e102741d73cce4ec58b2d937586c670f31df1c80aeaf4d5c525eb3c6ac06Status: Downloaded newer image for wordpress:latest
ベース・イメージ(公式のubuntu等)
イメージ層読み込み専用(Read Only)
Docker コンテナの実行とは
ベース・イメージ(公式のubuntu等)
イメージ層読み込み専用(Read Only)
・新しいイメージ・レイヤの自動的な割り当て
・イメージ内のプログラムを独立したプロセスで実行(コンテナ化された状態)
Docker コンテナの実行とは
25
26
ここまでのまとめ
は
技術で
の を
して実行
27
ここまでのまとめ
は
Docker Engine
Linux Kernel
技術で
の
ファイル
を
Docker イメージ
コンテナ化
して実行
28
技術と仕様Technology Specificationコンテナ Docker
29
でも、コンテナって前からあったよね?
Docker Hub
31
32
33
34
35
36
構築・移動・実行Build Ship Run
37
Dockerと愉快な仲間達
Build Run開発 ・構築 移動 実行
Ship
“Build, Ship, Run, Any App Anywhere”
Docker Engine for Linux / Commercial SupportDocker for Mac, Windows, Windows Server 2016
Docker Trusted Registry
Docker Hub
Universal Control Plane
Toolbox
Kitematic
Dev(開発)
Ops(運用)
38
Docker Hub
Dockerイメージの保管と共有をするためのリポジトリ(倉庫)
docker クライアント docker エンジン
docker container run hello-world
OSに対してdocker プログラムの実行を伝える
Docker に対してコンテナに読み込むイメージの情報を伝える
Docker コンテナを作成・実行するためのサブコマンド
$ docker container run hello-world
run
docker クライアント docker エンジン
$ docker container run hello-world
run
Docker Hub
pull
レジストリ
latest
イメージ
タグ
hello-world レポジトリ
latest
イメージ
タグ
latest
コンテナ化した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/
$ docker container run hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-world03f4658f8b78: Pull completea3ed95caeb02: Pull completeDigest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7Status: 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/
ローカルにhello-worldイメージがない
公式イメージlibraryにある”hello-world”を取得
最新の”hello-world:latest”イメージを取得完了
hello-worldイメージを使ったコンテナの実行(run)を命令
42
https://hub.docker.com
43
イメージの自動構築機能
webhook
コマンドでコンテナ操作
45
Dockerイメージ操作コマンド
docker images
イメージ・レイヤ(読み込み専用)
コンテナ・レイヤ(読み書き可能)
イメージ1のリポジトリ
イメージ2のリポジトリ
イメージnのリポジトリ
…docker save
docker load
tartar
docker import
docker commit
…
Dockerfile
docker build
…
docker tag
docker rmi
削除したレイヤ
docker inspect
docker history
レジストリ(Docker Hub)
リポジトリ docker pull
docker push
旧体系
46
Dockerイメージ操作コマンド
docker images
イメージ・レイヤ(読み込み専用)
コンテナ・レイヤ(読み書き可能)
イメージ1のリポジトリ
イメージ2のリポジトリ
イメージnのリポジトリ
…docker save
docker load
tartar
docker import
docker commit
…
Dockerfile
docker build
…
docker tag
docker rmi
削除したレイヤ
docker inspect
docker history
レジストリ(Docker Hub)
リポジトリ docker pull
docker push
旧体系
v1.13~
docker image ls
docker image push
docker image pull
docker image build
docker image import docker image save
docker image load
docker image commitdocker image tag
docker image inspect
docker image history
docker image rm / prune
47
Dockerコンテナ操作コマンド
start
イメージ・レイヤ(読み込み専用)
コンテナ・レイヤ(読み書き可能)
削除したレイヤ
イメージのリポジトリ
コンテナ用レイヤ作成 親子
関係
create
diff
プロセス(PID1)
コンテナ内のプロセスやファイルシステム
追加プロセス
pause
stop
kill
exec
ps
stats
top
attach
logs
inspect
port
restartrun
ホスト上のコンテナ
全体を管理
ホストから各コンテナを管理・操作
docker events
コンテナのPID1を操作
rm
pull
レジストリ
旧体系
48
Dockerコンテナ操作コマンド
start
イメージ・レイヤ(読み込み専用)
コンテナ・レイヤ(読み書き可能)
削除したレイヤ
イメージのリポジトリ
コンテナ用レイヤ作成 親子
関係
create
diff
プロセス(PID1)
コンテナ内のプロセスやファイルシステム
追加プロセス
pause
stop
kill
exec
ps
stats
top
attach
logs
inspect
port
restartrun
ホスト上のコンテナ
全体を管理
ホストから各コンテナを管理・操作
docker events
コンテナのPID1を操作
rm
pull
レジストリ
旧体系
v1.13~
docker container top
docker container inspect
docker container attach
docker container logsdocker container run
docker container start
docker container create
docker image pulldocker container diff
docker container pause
docker container stop
docker container kill
docker container restart
docker container rm / prune
docker container exec
docker container ls
docker container port
docker container stats
docker events
イメージRead Only
コンテナ用レイヤ読み書き可
docker ps … コンテナ一覧や状態の表示
docker rmi … コンテナイメージ削除
docker rm … コンテナ削除
docker images … 一覧
コンテナとイメージの典型的なライフサイクル
docker pull … イメージの取得
docker pull … イメージの取得
docker image pull( docker pull )
イメージをリポジトリから取得
docker pull … イメージの取得
docker image ls( docker images )
ローカルのイメージを一覧表示
$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEhello-world latest 690ed74de00f 6 months ago 960 B
イメージRead Only
コンテナ用レイヤ読み書き可
docker pull … イメージの取得
docker run … コンテナの実行
docker container run( docker run )
イメージをコンテナとして実行
イメージRead Only
コンテナ用レイヤ読み書き可
docker pull … イメージの取得
docker run … コンテナの実行
イメージRead Only
コンテナ用レイヤ読み書き可
docker pull … イメージの取得
docker run … コンテナの実行
docker container ls( docker ps )
コンテナの状態を一覧表示
イメージRead Only
コンテナ用レイヤ読み書き可
docker commit … イメージの作成
docker build… イメージの自動構築(自動commit)
イメージRead Only
コンテナ用レイヤ読み書き可
docker commit … イメージの作成
docker build… イメージの自動構築(自動commit)
docker image build( docker build )
イメージの自動構築(ビルド)
イメージRead Only
コンテナ用レイヤ読み書き可
docker commit … イメージの作成
docker build… イメージの自動構築(自動commit)
docker container commit( docker commit )
コンテナ用レイヤをイメージ化
イメージRead Only
コンテナ用レイヤ読み書き可
docker history … イメージの履歴表示
docker diff… 元イメージとコンテナの差分表示
docker inspect… コンテナやイメージの調査
イメージRead Only
コンテナ用レイヤ読み書き可
docker history … イメージの履歴表示
docker diff… 元イメージとコンテナの差分表示
docker inspect… コンテナやイメージの調査
docker container inspect( docker inspect )
コンテナの詳細情報を表示
docker image inspect( docker inspect )
イメージの詳細情報を表示
イメージRead Only
コンテナ用レイヤ読み書き可
docker history … イメージの履歴表示
docker diff… 元イメージとコンテナの差分表示
docker inspect… コンテナやイメージの調査
docker container diff( docker diff )
イメージとの差分を表示
イメージRead Only
コンテナ用レイヤ読み書き可
docker history … イメージの履歴表示
docker diff… 元イメージとコンテナの差分表示
docker inspect… コンテナやイメージの調査
docker image history( docker history )
イメージ・レイヤと履歴の表示
イメージRead Only
コンテナ用レイヤ読み書き可
docker push … Dockerイメージの送信
docker login … ログイン
docker tag … イメージをタグ付け
docker search … 検索
イメージRead Only
コンテナ用レイヤ読み書き可
docker push … Dockerイメージの送信
docker login … ログイン
docker tag … イメージをタグ付け
docker search … 検索
docker login
Docker Hubにログイン
イメージRead Only
コンテナ用レイヤ読み書き可
docker push … Dockerイメージの送信
docker login … ログイン
docker tag … イメージをタグ付け
docker search … 検索
イメージを送信
docker image push( docker push )
イメージRead Only
コンテナ用レイヤ読み書き可
docker ps … コンテナ一覧や状態の表示
docker rmi … コンテナイメージ削除
docker rm … コンテナ削除
docker images … 一覧
イメージRead Only
コンテナ用レイヤ読み書き可
docker ps … コンテナ一覧や状態の表示
docker rmi … コンテナイメージ削除
docker rm … コンテナ削除
docker images … 一覧
コンテナを削除
docker container rm( docker rm )
イメージRead Only
コンテナ用レイヤ読み書き可
docker ps … コンテナ一覧や状態の表示
docker rmi … コンテナイメージ削除
docker rm … コンテナ削除
docker images … 一覧
イメージを削除
docker image rm( docker rmi )
イメージRead Only
コンテナ用レイヤ読み書き可
docker ps … コンテナ一覧や状態の表示
docker rmi … コンテナイメージ削除
docker rm … コンテナ削除
docker images … 一覧
不要なコンテナを削除
docker container prune
不要なイメージを削除
docker image prune
イメージRead Only
コンテナ用レイヤ読み書き可
docker ps … コンテナ一覧や状態の表示
docker rmi … コンテナイメージ削除
docker rm … コンテナ削除
docker images … 一覧
不要なコンテナ、イメージ、ネットワーク、ボリューム削除
docker system prune
# docker system pruneWARNING! 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 images
Are you sure you want to continue? [y/N] yDeleted Containers:6612edf483662725e4b1147c16f5842c780d702f1ee2d3605cb6d2c49603f45608c57b9fdb25db0705f20a6a4aaafd5ab2e367b5adbfe62b3a88e3d83327615f(snip)
Total reclaimed space: 0 B
71
バージョン情報等の表示docker system info
バージョン情報等の表示docker system df
# docker system dfTYPE TOTAL ACTIVE SIZE RECLAIMABLEImages 4 4 1.345 GB 123 MB (9%)Containers 5 1 621.1 kB 0 B (0%)Local Volumes 3 3 432.6 MB 0 B (0%)
72
詳しくはウェブで…! https://qiita.com/zembutsu
その他、運用面
Copy on Write(コピー・オン・ライト)
読み込み専用(Read Only)
• Read Only のイメージ層上のファイルに対する変更は、必ずコピー作業を行う
• 内部実装はストレージ・ドライバに依存する(パフォーマンスに影響が出る)
• CoWを回避するにはボリュームを使う
書き込む前にコピーしてから
コンテナのレイヤ上に変更を反映
捕 捉
イメージ、コンテナ、ストレージ・ドライバの理解http://docs.docker.jp/engine/userguide/storagedriver/imagesandcontainers.html
AUFS
AUFS ストレージ・ドライバを使う— Docker-docs-ja 1.10.0b ドキュメントhttp://docs.docker.jp/engine/userguide/storagedriver/aufs-driver.html
devicemapper
Device Mapper ストレージ・ドライバを使う— Docker-docs-ja 1.10.0b ドキュメントhttp://docs.docker.jp/engine/userguide/storagedriver/device-mapper-driver.html
https://docs.docker.com/engine/userguide/storagedriver/selectadriver/
ボリュームはコンテナ用のレイヤとは分離
イメージRead Only
コンテナ用レイヤ読み書き可
• イメージ・レイヤの仕様(Copy on Write)を回避できる
• docker commit してもボリューム内容は反映されない。
• 複数のコンテナでボリュームを共有できる
• ホスト側のファイルやディレクトリをマウント可能
捕 捉
コンテナでデータを管理するhttp://docs.docker.jp/engine/userguide/containers/dockervolumes.html
79
用途に応じた技術選択が鍵
1. 利用者は誰で、用途は何か?
2. インフラをどうするのか?
3. OSを何にするのか?
4. ディストリビューションを何にするのか?
5. ストレージ・ドライバを何にするのか?
6. ベースイメージは何にするのか?
7. セキュリティをどのように考慮すべきか?
8. 性能をどのように担保するのか?
振り返り
今日お伝えしたかったこと
コンテナは技術、Dockerは仕様
Docker Hubが中継点
コマンドでコンテナとイメージを操作
何か気になる所がありますか?
ご参考:Docker 日本語ドキュメント
http://docs.docker.jp/
http://slideshare.net/zembutsu
twitter: @zembutsu