神戸学院大学 経営学部 林坂ゼミ

Docker 入門トップページ

« 戻る 次へ »

Docker 入門

Docker はじめの一歩

Docker で Hello World

Docker のはじめの一歩として,Hello World イメージをダウンロードし,そのイメージからコンテナを実行します.具体的には次のコマンドで hello-world イメージがダウンロードされ,そのコンテナを実行し,画面に文字列が表示されてコンテナが終了します.

% docker run hello-world ⏎
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
198f93fd5094: Pull complete
Digest: sha256:a0dfb02aac212703bfcb339d77d47ec32c8706ff250850ecc0e19c8737b18567
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.
    (arm64v8)
 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 イメージがまだローカル環境にあるかを確認します.ローカル環境になかったことから,Docker のレポジトリから Docker イメージがダウンロードされました.ローカル環境にあるイメージの一覧を確認します.ここで ls コマンドは Bash の ls コマンドと同じようなものであると理解すると良いでしょう.

% docker image ls ⏎
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    ca9905c726f0   4 weeks ago   5.2kB

次に,docker container ls -a というコマンドで実行終了したコンテナも含めてすべてのコンテナを一覧で確認します.このとき,NAME には lucid_mayer が付けられましたが,この名称は指定しない限り毎回異なります.また CONTAINER ID5ff2bd0fdf0d という値も毎回異なります.

% docker container ls -a ⏎
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
5ff2bd0fdf0d   hello-world   "/hello"   41 seconds ago   Exited (0) 40 seconds ago             lucid_mayer

なお,上のコマンドで -a オプションは終了したコンテナも表示するというものです.このオプションを付けなければ実行中のコンテナだけが表示されるため,今は何も表示されないはずです.この lsls -a の関係は Bash の ls コマンドの考え方とほぼ同じです.

% docker container ls ⏎
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

もう一度同じイメージからコンテナを作成して実行します.今度はイメージが既にローカル環境に存在することから,短時間でコンテナが作成されて実行されるはずです.

% 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.
    (arm64v8)
 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/

コンテナの一覧を確認します.すると,2個のコンテナがあることが分かります.

% docker container ls -a ⏎
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
81436562b438   hello-world   "/hello"   4 seconds ago   Exited (0) 3 seconds ago             vigilant_curie
5ff2bd0fdf0d   hello-world   "/hello"   2 minutes ago   Exited (0) 2 minutes ago             lucid_mayer

現時点では,イメージは設計図で,コンテナはその実行環境である,という程度の理解で良いでしょう.