このページで解説している内容は、以下の YouTube 動画の解説で見ることができます。

コンテナの作成と実行・停止・削除

Dockerの処理の流れ

ここでは、コンテナの作成と実行、停止、削除のDockerの処理の流れを確認していきます。

下図の流れを、ざっくりと確認していきます。

①~④までの処理

 「とりあえずDockerを動かしてみる」では、「docker run hello-world」コマンドを入力してコンテナを作成して実行させてみました。

この1つのコマンドだけで、①~④までの処理が行われていたのです。

コンテナのライフサイクルは、次のように遷移します。

①イメージのダウンロード(pull)
   ↓
②コンテナの作成
   ↓
③コンテナの実行
   ↓
④コンテナの停止
   ↓
⑤コンテナの削除

各ステップを順に確認していきます。

①~④の遷移について

Docker Hub には様々なイメージが存在します。

 まず、必要なDockerイメージをタグ(バージョン)を指定してダウンロード(pull)する必要があります。タグを指定して、ダウンロードします。この際、タグを省略した場合はイメージの最新バージョン(latest)がダウンロードされます。

しかし、

まだ、Dockerイメージをダウンロードをしていません。

①と②のコンテナのダウンロードも、作成もしていないのに、

「docker run」してコンテナを実行しようとしています。

「docker run」コマンド

ここで、「docker run」コマンドについて解説しておきます。

 「docker run」とは、指定したイメージを起動するためのコマンドで、 実際にはイメージを取得するための「docker pull」や、イメージから新しいコンテナを作成する「docker create」、コンテナ中でプログラムを起動する「docker start」などのコマンドを合成したものです。

「docker run hello-world」コマンドを実行したことにより、

 ①の「docker pull」が行われ、続いて②の「docker create」が行われ、その後③の「docker start」が行われたのです。そして、コンテナの実行が終了したため、④の停止の状態に推移したのです。

 イメージには、「hello-world」を指定しており、タグでバージョン(厳密にはべージョン情報ではありません)を指定していないため、デフォルトの「latest」タグが適用されて、「hello-world:latest」イメージがダウンロードされます。

「docker run hello-world」コマンド

それでは、「docker run hello-world」コマンドを実行してみましょう。

PS C:\Users\joeac> docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:d000bc569937abbe195e20322a0bde6b2922d805332fd6d8a68b19f524b7d21d
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/

上の出力を確認します。

Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete

と表示されています。これを日本語に直すと、

イメージ「hello-world:latest」がローカルに見つかりません。
最新: ライブラリ「/hello-world」 からの取得
c1ec31eb5944: プルが完了しました。

であり、これは、イメージ「hello-world:latest」がダウンロード(pull)されたことを意味しています。

また、次の出力を

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.


日本語に翻訳すると、

Docker からこんにちは!
このメッセージは、インストールが正しく動作していることを示しています。

このメッセージを生成するために、Docker は次の手順を実行しました。
  1. Docker クライアントが Docker デーモンに接続しました。
  2. Docker デーモンは、Docker Hub から「hello-world」イメージをプルしました。
     (amd64)
  3. Docker デーモンは、そのイメージから新しいコンテナを作成し、
     現在読んでいる出力を生成する実行可能ファイル。
  4. Docker デーモンはその出力を Docker クライアントにストリーミングし、Docker クライアントがそれを送信しました。
     端末に。


つまり、このメッセージから、①のpullと②のcreate、そして③の「docker start」が行われたことが分かります。

そして、実行が終了したため、④の状態になっているのです。

⑤のコンテナの削除

それでは、作成されたコンテナを削除します。

「docker container ls -a」コマンドでコンテナの一覧を確認します。

PS C:\Users\joeac> docker container ls -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
5aee81e0ad0c   hello-world   "/hello"   18 minutes ago   Exited (0) 18 minutes ago             musing_feistel

 「 docker container rm (コンテナ名) 」でコンテナを削除できますが、ここでは、コンテナ名を指定せずに、コンテナIDを指定して削除します。

「 docker container rm (コンテナID)」コマンド

 「 docker container rm 5aee81e0ad0c 」を実行します。コンテナIDは実行環境で異なります。

PS C:\Users\joeac> docker container rm  5aee81e0ad0c
5aee81e0ad0c

コンテナIDを指定してもコンテナを削除することができます。

「 docker container rm -f(コンテナ名)」コマンド

今回は、すんなりとコンテナを削除することができましたが、削除できない場合もあります。

その場合は、強制削除を行います。

強制削除を行うには、「-f」オプションを指定します。

 「docker container rm -f(コンテナ名)」コマンド、もしくは「docker container rm -f(コンテナ名)」コマンドで削除します。

イメージの削除

作成されたイメージも削除しておきましょう。

「docker image ls」コマンドで現在、ホストマシン上にあるイメージの一覧を確認します。

PS C:\Users\joeac> docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   10 months ago   13.3kB

「docker image rm (イメージID)」コマンド

 「 docker image rm (イメージ名) 」でイメージを削除できますが、ここでは、イメージ名を指定せずに、イメージIDを指定して削除します。

「docker image rm d2c94e258dcb」コマンドを実行します。イメージIDは実行環境で異なります。

PS C:\Users\joeac> docker image rm d2c94e258dcb
Untagged: hello-world:latest
Untagged: hello-world@sha256:d000bc569937abbe195e20322a0bde6b2922d805332fd6d8a68b19f524b7d21d
Deleted: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a
Deleted: sha256:ac28800ec8bb38d5c35b49d45a6ac4777544941199075dff8c4eb63e093aa81e

「docker image ls」コマンドで現在、ホストマシン上にあるイメージの一覧を確認します。

PS C:\Users\joeac> docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE

イメージの一覧がありません。「hello-world」イメージが削除されました。