LoginSignup
90
53

More than 5 years have passed since last update.

Docker入門 ~Hello World~

Posted at

はじめに

この記事ではDockerでhello-worldを実行し、何をしているのかを簡単に説明していきます。

仮想化環境

従来のホスト型仮想化とDockerにはどのような違いがあるのでしょうか?

ホスト型仮想化

ホストOS上でゲストOS動作させることによって仮想化環境を作る。
物理サーバーに近い環境を作ることができるが、その分起動や停止が遅い。

Docker(コンテナ型仮想化)

ゲストOSを必要としないため非常に軽量。
動作に必要な物がDockerイメージにまとまっているため、Dockerがインストールされている環境なら同じように動作する。
これによって開発環境と本番環境の違いを気にせず開発に集中できる。

Dockerを始めよう

インストール

まずはDocker公式サイトから登録を行い、
Macの場合Docker for Macを、
Windows10 Proの場合Docker for Windowsをインストールします。
Windows10 Homeの場合Docker Toolboxをインストールします。こちらは古いバージョンですので、他の要件を満たさない場合に使います。

$ docker -v
Docker version 18.09.0, build 4d60db4

Dockerが無事起動できていればバージョンが表示されます。

hello-world

まずはdocker runコマンドでイメージを取得、コンテナの実行をしてみましょう。
今回は公式のhello-worldコンテナを実行します。

コンテナとはアプリやインフラなどを入れた箱であり、イメージとはコンテナを実行するために必要なファイルシステムのことです。


$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
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/

hello-worldコンテナを実行することができました。
メッセージにはコマンド実行後に起こったことの説明が書かれています。
上の5行は初めてイメージを取得する場合表示されるメッセージです。
簡単に解説すると、

  • ローカルにhello-world:latestが存在しないためDocker Hubよりイメージを取得
  • イメージをsha256でハッシュ化
  • hello-wolrd:latestがダウンロードされたことを表しています。

後ろに存在するlatestはタグです。コマンドをタグ指定なしで実行した場合、タグは自動的にlatestが選択されます。
現在のイメージとコンテナを表示して確認してみます。

# イメージの確認
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

latestタグを持ったhello-worldイメージが確認できました。
起動中のコンテナを確認してみましょう。


$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

何も存在しません。
hello-worldコンテナはメッセージ出力が終わるとコンテナを停止してしまうため、起動中ではありません。
-a オプションを付けて停止中のコンテナも表示してみます。


$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
822cff2da062        hello-world         "/hello"            10 minutes ago      Exited (0) 10 minutes ago                       pedantic_joliot

これでhello-worldイメージとコンテナを確認できました!
最後にこれらを削除して終わりにします。


# コンテナを削除する
# docker ps -a コマンドで確認したコンテナIDを指定
$ docker rm 822cff2da062
822cff2da062

# イメージを削除する
$ docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Deleted: sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f
Deleted: sha256:428c97da766c4c13b19088a471de6b622b038f3ae8efa10ec5a37d6d31a2df0b

docker rm [コンテナID]でコンテナの削除を、
docker rmi [イメージ名]でイメージの削除を行いました。
これでDockerのhello-worldは完了です。

90
53
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
90
53