継続は力なり

タイトル通り定期的な更新を心掛けるブログです。

Docker 実践活用ガイド 4章

タダです。

今回はDocker実践活用ガイドの4章を読んだ内容をまとめていきます。 ※記事は理解ができるたびに何度もアップデートしていこうと思います。 book.mynavi.jp

なお、副教材としてドットインストールも使っていこうと思います。

目次

なお、各章の内容は次の通りです。

  • 1章 Dockerとは
  • 2章 Dockerの仕組み
  • 3章 Dockerのインストール
  • 4章 Dockerを使ってみよう(コマンド編)
  • 5章 Dockerを使ってみよう(Kitematic(GUI)編)
  • 6章 Dockerイメージの操作
  • 7章 Dockerを使いこなす
  • 8章 複数のDokcerを使う(Docker Machine、Docker Swarm、Docker Compose)
  • 9章 Dockerをクラウドで使う(Docker Cloud)
  • 10章 DockerとJavaScriptウェブサービスを作る(簡易オンラインジャッジシステム)
  • 11章 DOckerを利用した実運用ウェブサービス構築事例(paizaオンラインジャッジシステム)
  • 12章 paizaの実行環境APIを使いウェブサービスを作る(簡易オンラインジャッッジシステム)
  • 13章 Dockerの内部

4.1

  • Dockerをコマンドを動作させる
  • 「hello-world」を実行した結果、次のような動作を行っている
    • "hello-world" Dockerイメージをみつける
    • コンテナ環境を作成する
    • コンテナ内の"/hello"コマンドを実行する
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/engine/userguide/

4.2

  • hello-worldコンテナはすぐに終了するため、次はUbuntuイメージでコンテナを起動する
    • root@b886a309743f: で中に入れたことを表している
docker run -it ubuntu /bin/bash
root@b886a309743f:/#
  • cat /etc/lsb-releast を実行すると次のような結果が帰ってくる(Ubuntu 16.04が動作していることがわかった)
root@b886a309743f:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"
  • 一度コンテナを終了しても、作成したファイルが残っているかを確認する
    • docker run コマンドは実行するたびに新しいコンテナを作るため、一度終了するとファイルは見えなくなる
root@b886a309743f:/# echo test > test.txt
root@b886a309743f:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  test.txt  usr
boot  etc  lib   media  opt  root  sbin  sys  tmp       var
root@b886a309743f:/# cat test.txt
test
root@b886a309743f:/#
exit
$ docker run -it ubuntu /bin/bash
root@70d67ab7c978:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@70d67ab7c978:/# cat test.txt
cat: test.txt: No such file or directory
root@70d67ab7c978:/#

4.3

  • 次にnginxサーバを動かしてみる
    • docker run -p 8080:80 nginx でDockerホストの8080番ポートに接続するとコンテナの80番ポートに接続できるようになる
$ docker run -p 8080:80 nginx
172.17.0.1 - - [12/Mar/2018:12:23:04 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" "-"
2018/03/12 12:23:04 [error] 6#6: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"
172.17.0.1 - - [12/Mar/2018:12:23:04 +0000] "GET /favicon.ico HTTP/1.1" 404 571 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" "-"
  • ブラウザでは以下のページが表示される f:id:sadayoshi_tada:20180312212528p:plain

4.4

  • 次はWordpressをDockerで動かす
    • WebサーバとDBサーバのコンテナを1つずつ起動し、WebからDBに接続する設定を行う
  • まずデータベースサーバを起動する
    • MYSQL_ROOT_PASSWORD 変数でパスワードを設定
    • --name オプションでコンテナに名前をつける
    • -d オプションでコンテナをバックグランドで動かす
    • docker ps をたたくと、dbというコンテナが動作していることを確認
$ docker run -d -e MYSQL_ROOT_PASSWORD=password --name db mysql
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
8176e34d5d92: Pull complete
17e372a8ec90: Pull complete
47b869561d3a: Pull complete
c90ab4483f28: Pull complete
d6af16572c5c: Pull complete
6d16794d04ac: Pull complete
aaf442a8fe75: Pull complete
7c6fa8f07ec4: Pull complete
ece17b689642: Pull complete
c55b06e76eaf: Pull complete
661fabfb4fc2: Pull complete
Digest: sha256:227d5c3f54ee3a70c075b1c3013e72781564000d34fc8c7ec5ec353c5b7ef7fa
Status: Downloaded newer image for mysql:latest
053b8c0e7fb4856abc7f7f00341fa0aef4bae0756fccc6456f6f15b773ced7c9
$ 
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
053b8c0e7fb4        mysql               "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp            db
  • 次に、Webサーバを動かす(WordPressのイメージコンテナを起動する)
    • -e WORDPRESS_DB_PASSWORD でDBのパスワードを指定する
    • コンテナ内のWebサーバが動作している80番ポートにDockerホストの8080番で接続する
    • db コンテナに mysql というホスト名でつなげるように --link db:mysql を指定する
$ docker run --link db:mysql -p 8080:80 -e WORDPRESS_DB_PASSWORD=password --name web wordpress
Unable to find image 'wordpress:latest' locally
latest: Pulling from library/wordpress
8176e34d5d92: Already exists
f6c81892adaa: Pull complete
c8125c73b868: Pull complete
5ef22f6299b6: Pull complete
8537460e9a8c: Pull complete
b837671b83f0: Pull complete
366ea9d8b411: Pull complete
c4dd539af472: Pull complete
445753fb3ee6: Pull complete
ba804c596fa7: Pull complete
568f4dfd5c18: Pull complete
eb809b1ba822: Pull complete
4fb812796f56: Pull complete
bbbbb844bbb2: Pull complete
7e9231ebc71d: Pull complete
eae4dc935b76: Pull complete
8df7d811b099: Pull complete
5016ac47c355: Pull complete
af9ff1e0ef4a: Pull complete
Digest: sha256:565f25325066d2e4711e119345c65636002e8aac1187f203d44a7350225ed1d9
Status: Downloaded newer image for wordpress:latest
WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Mon Mar 12 12:38:18.740621 2018] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.3 configured -- resuming normal operations
[Mon Mar 12 12:38:18.740927 2018] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
  • アクセスすると、Wordpressの画面が表示される f:id:sadayoshi_tada:20180312214127p:plain

4.4

  • Webサーバのコンテナは Control-C で終了できるが、DBサーバは個別に終了させる
    • docker ps で対象のコンテナを探し、docker kill <コンテナID> で削除する
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
053b8c0e7fb4        mysql               "docker-entrypoint.s…"   14 minutes ago      Up 14 minutes       3306/tcp            db
$ docker kill 053b8c0e7fb4
053b8c0e7fb4
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  • 次のようなコマンドで一気に削除も可能
    • docker kill $(docker ps -a) : すべてのコンテナを終了する
    • docker rm $(docker ps -a -q) : すべての終了しているコンテナを削除する
    • docker rm -f $(docker ps -a -a) : 動作しているコンテナを含めて、すべてのコンテナを削除する

4.6

次回は5章です。