定番のhello-worldから始める
自身のmacOSのターミナルまたはWindowsのコマンドプロンプトから以下のコマンドを実行してみます。
1 |
$ docker run hello-world |
<<結果>>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f 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. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 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 ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ |
さて、これは何が起こったのか?
実行した結果dockerは何をしたのか?
- Dockerクライアント(ターミナル)からDockerデーモンに接続
- Dockerデーモンはhello-worldというイメージをローカルホストの中で探します
- hello-worldというイメージがローカルにないため、Dockerデーモンはパブリックなインターネットの世界に出ていき、Docker Hubからhello-worldのイメージをローカルにプルしてきます
- プルしたイメージからDockerデーモンが、コンテナを生成し、コンテナ上のプロセスがメッセージを出力します
- Dockerデーモンはコンテナの出力をDockerクライアントに送り、ターミナルで内容が表示されます。
ターミナルに表示された内容(上記の結果部分)は5の内容が表示されただけです。それまでに1〜4の作業が実際には行われています。
docker runのコマンド
- dockerコマンドを入力して、dockerを使う事をホスト側で宣言します。
- サブコマンド使って、dockerで何をするかを指定します。
- サブコマンドの後ろにイメージを指定する事でもう少し具体的な事を言っています。ここではhello-worldというイメージを起動しています。
docker run は複数のコマンドの集合体
- docker pull:イメージを取得する
- docker create:コンテナを作成する
- docker start:コンテナを起動する
まとめ
今回はあっさりと終わりましたが、どのプログラムやサーバー構築でもあるhello-worldをやってみました。なんとなく、手順通りに動かせば動かせるではなくて、きちんと意味理解をしたいので、ゆっくり確実に抑えていきたいと思います。次回はコンテナのライフサイクルをdockerで実践したいと思います。