Download - 使ってわかる 今どきのdocker超入門
![Page 1: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/1.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
使ってわかる
今どきの 超入門
2016年2月18日
先端IT活用推進コンソーシアムクラウド・テクノロジー活用部会
勉強会資料
岡村 和英(株式会社テクリエ)
![Page 2: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/2.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
?
2
![Page 3: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/3.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
Linuxコンテナによる
リソース分割を利用した
仮想化環境
3
![Page 4: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/4.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
ユーザー空間
4
通常の非仮想化環境
物理マシン (ハードウェア)
OS (カーネル空間)
ユーザープロセス
ユーザープロセス
ユーザープロセス
ユーザープロセス
![Page 5: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/5.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 5
ハイパーバイザー型仮想化環境
物理マシン (ハードウェア)
ホストOS
ユーザープロセス
ユーザープロセス
仮想マシン
ハイパーバイザー
ゲストOS
ユーザープロセス
ユーザープロセス
仮想マシン
ゲストOS
![Page 6: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/6.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
ユーザー空間(コンテナ)
6
コンテナ型仮想化環境
物理マシン (ハードウェア)
OS (カーネル空間)
ユーザープロセス
ユーザープロセス
ユーザー空間(コンテナ)
ユーザープロセス
ユーザープロセス
![Page 7: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/7.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
コンテナ型仮想化
の
メリット
7
![Page 8: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/8.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
起動が早い
8
コンテナのメリット
仮想マシンの起動を要するハイパーバイザー型仮想化とは異なり、ホストOS側の処理としては通常の非仮想化環境下におけるプロセスの起動とほとんど変わらない。
![Page 9: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/9.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
リソース消費が少ない
9
コンテナのメリット
仮想マシンを介さないため、メモリ・CPU・HDDなどのリソースを有効に活用できる。また、これにより一つの物理マシン上でより多くのプロセスを実行することが可能となる。
![Page 10: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/10.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
ポータビリティが高い
10
コンテナのメリット
HTTPサーバのみのコンテナなど機能に応じた小さなコンテナを組み合わせて運用することで、物理マシン間におけるコンテナの再配置や、スケールアウトなどへの対応が容易となる。
![Page 11: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/11.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
面倒な説明はともかく体験してみよう
11
![Page 12: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/12.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
Hands On
12
![Page 13: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/13.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 13
Today’s menu
コンテナを起動してみるコンテナの中をのぞいてみるコンテナをカスタマイズしてみるコンテナ間で連携してみる
![Page 14: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/14.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 14
始める前に
今日のハンズオン勉強会ではAWS上に事前に用意した実習環境を用います。後日おさらいをする場合には各自でLinux
環境を用意して下さい。DockerはVM上のLinux環境でも動作します。
https://docs.docker.com/engine/installation/
![Page 15: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/15.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 15
コンテナを起動する
【書式】docker run [オプション...] イメージ名:タグ [コマンド] [引数...]
![Page 16: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/16.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 16
コンテナ一覧を表示する
【書式】docker ps [オプション...]
![Page 17: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/17.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 17
Let’s Try!
$ sudo docker run --name=fpm-hogege php:fpm
[17-Feb-2016 12:23:20] NOTICE: fpm is running, pid 1[17-Feb-2016 12:23:20] NOTICE: ready to handle connections
console:1
コンテナがフォアグラウンドプロセスとして実行される
コンテナの起動
--name:コンテナに名前をつける(今回の実習では他の人が実行したコンテナと区別するために明示的に名前をつけます)
![Page 18: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/18.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 18
Let’s Try!
$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES94fc6fe8f0be php:fpm "php-fpm" 3 seconds ago Up 2 seconds 9000/tcp fpm-hogege
実行中のコンテナ情報が表示される
コンテナの表示
console:2
docker runを行ったのとは別の端末画面から実行すること!
![Page 19: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/19.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 19
コンテナ内でコマンドを実行する
【書式】docker exec [オプション...] コンテナID
(又はコンテナ名) コマンド [引数...]
![Page 20: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/20.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 20
Let’s Try!$ sudo docker exec -ti fpm-hogege /bin/bash
root@94fc6fe8f0be:/var/www/html# ps ax
PID TTY STAT TIME COMMAND1 ? Ss 0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
8 ? S 0:00 php-fpm: pool www9 ? S 0:00 php-fpm: pool www10 ? Ss 0:00 /bin/bash15 ? R+ 0:00 ps ax
root@94fc6fe8f0be:/var/www/html# ls -a. ..
root@94fc6fe8f0be:/var/www/html# exitexit
$
-t: tty(端末デバイス)を割り当てる-i:コンテナの標準入力を開く
コンテナ内で実行中のプロセスを表示する
コマンドの実行
console:2
コマンドを終了する
コンテンツは存在しない
![Page 21: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/21.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 21
コンテナを終了する
【書式】docker stop [オプション...] コンテナID
(又はコンテナ名)...
![Page 22: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/22.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 22
Let’s Try!コンテナの終了
$ sudo docker stop fpm-hogege$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS ....
$ sudo docker ps –a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES94fc6fe8f0be php:fpm "php-fpm" 42 minutes ago Exited (0) About a minute ago fpm-hogege
終了したコンテナは表示されない
console:2
-a:全てのコンテナを表示する
終了したコンテナも表示される
![Page 23: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/23.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 23
イメージを作成する
【書式】docker build [オプション...] パス名(又はURL)...
Dockerfileの記述内容に応じて、コンテナを起動する基となるイメージを作成する
![Page 24: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/24.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 24
イメージ一覧を表示する
【書式】docker images [オプション...] [イメージ名[:タグ名]]
Dockerfileの記述内容に応じて、コンテナを起動する基となるイメージを作成する
![Page 25: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/25.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 25
Let’s Try!
$ mkdir ~/hogege$ cd ~/hogege$ git clone https://github.com/kzokm/aitc-cloud-20160217 .$ ls -R ..:Dockerfile www
./www:index.php static.html
console:1
各自の作業用ディレクトリを作成する
イメージ作成の準備
事前に用意された実習用ファイルを取得する
![Page 26: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/26.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 26
Let’s Try!$ cat ./DockerfileFROM php:fpmCOPY www/* /var/www/html/
$ sudo docker build -t hogege/php:fpm .Sending build context to Docker daemon 10.75 kBStep 1 : FROM php:fpm---> a630b021ad2aStep 2 : COPY www/* /var/www/html/---> e73d59c9b3f7Removing intermediate container 809dd9b91a13Successfully built e73d59c9b3f7
$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEhogege/php fpm e73d59c9b3f7 19 seconds ago 495.8 MBphp fpm a630b021ad2a 11 days ago 495.8 MB
console:1
wwwディレクトリ以下のコンテンツをコンテナ内にコピーする
イメージの作成
-t:イメージ名をつける
Dockerfileの内容が実行される
作成したイメージが表示される
![Page 27: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/27.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 27
Let’s Try!
$ sudo docker run --name=fpm-hogege hogege/php:fpm
Error response from daemon: Conflict. The name "fpm-hogege" is already in use by container 94fc6fe8f0be. You have to remove (or rename) that container to be able to reuse that name.
console:1
作成したイメージからコンテナを起動する
イメージの確認
先に終了済みのコンテナと名前が同一のためコンテナの起動に失敗した!
![Page 28: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/28.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 28
コンテナを削除する
【書式】docker rm [オプション...] コンテナID
(又はコンテナ名)...
![Page 29: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/29.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 29
Let’s Try!
$ sudo docker rm fpm-hogegefpm-hogege
$ sudo docker ps –aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
console:1
終了済みのコンテナを削除する
コンテナの削除
削除されたことを確認する
![Page 30: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/30.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 30
Let’s Retry!
$ sudo docker run --name=fpm-hogege hogege/php:fpm[17-Feb-2016 14:43:45] NOTICE: fpm is running, pid 1[17-Feb-2016 14:43:45] NOTICE: ready to handle connections
console:1
今度は正しく起動した
イメージの確認(再)
$ sudo docker exec -ti fpm-hogege /bin/bashroot@94fc6fe8f0be:/var/www/html# ls -a. .. index.php static.html
root@94fc6fe8f0be:/var/www/html# exitexit
$
コンテンツがコピーされていることを確認する
console:2
![Page 31: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/31.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 31
コンテナを連携する
![Page 32: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/32.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 32
Let’s Try!コンテナの連携
$ sudo docker run -d -p 8081:80-v `pwd`/www:/var/www/html:ro-v `pwd`/nginx:/etc/nginx/conf.d:ro-v `pwd`/log/nginx:/var/log/nginx--link fpm-hogege:php-app
nginxa2311d93476feb085ee27ca782fa7879ebf790a3c6503720d67b590c521e991a
$
console:2
-d:コンテナをバックグラウンドで実行する-p : コンテナのポートをホスト側に公開する(今回の実習では他の人が実行したコンテナと区別するために別々のホスト側ポートを指定します)-v ホスト側のディレクトリをコンテナ内にマウントする--link コンテナをリンクする
![Page 33: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/33.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 33
Let’s Try!コンテナの連携
$ sudo docker exec -ti a2311d93476f /bin/bashroot@a2311d93476f:/# ls /etc/nginx/conf.ddefault.conf
root@a2311d93476f:/# ls /var/www/htmlindex.php static.html
root@a2311d93476f:/# printenvPHP_APP_....
root@a2311d93476f:/# exit$ curl http://localhost:8081/<h1>Hello World!!!</h1><h3>PHP Version 7.0.3</h3><a href="/static.html">Static HTML Page</a>
$ ls log/nginxaccess.log error.log
console:2
ホスト側ディレクトリがマウントされていることを確認する
ホスト側ディレクトリにnginxのログが出力されていることを確認する
ホスト側ポートからnginxコンテナを経由してfpmコンテナにアクセスできることを確認する
リンクされたコンテナの情報が設定されていることを確認する
![Page 34: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/34.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 34
あれ???
fpmコンテナ内にコンテンツファイルを
コピーする必要なかったんじゃない?
![Page 35: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/35.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved. 35
Let’s Try!
![Page 36: 使ってわかる 今どきのdocker超入門](https://reader035.vdocuments.net/reader035/viewer/2022062522/5876d4cf1a28ab1d238b54c7/html5/thumbnails/36.jpg)
Copyright © 2016 Advanced IT Consortium to Evaluate, Apply and Drive All Rights Reserved.
http://aitc.jp
https://www.facebook.com/aitc.jp
ハルミンAITC非公式イメージキャラクター