ろきメモ【ROKI MEMO】- ろきsanの備忘録 -

ろきさんの備忘録。プログラミング学習記録や開発記録、および学んだ知識等のアウトプットとシェアを目的に書いています。たまに普通のことも書きます。

【Dockerの基本のキ】サンプルイメージの実行から削除まで(コンテナ削除が必要)

スポンサーリンク

昨日、プロキシにハマりながらもUbuntuにDockerをインストールして、サンプルである「helo-world」イメージをDocker Hub から取得して実行するまでを記述した。

ahrk-izo.hatenablog.com


せっかくなので、今日はこの実行のちょっとした補足と、この実行されたコンテナの削除、イメージの削除までを備忘録として残していく。
ということで今日の目次

スポンサーリンク



1. サンプルイメージ「hello-world」の実行

改めて実行した内容を記述する。

$ sudo docker run hello-world
[sudo] user のパスワード: 
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752
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
1b930d010525: Pull complete 
Digest: sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752
Status: Downloaded newer image for hello-world:latest

の部分は、取得の結果の表示。(パスワードの部分はsudo実行だから聞かれているやーつ)

ざっくり訳すと
「ローカルに最新の'hello-world'ないな」
「ライブラリ(Docker Hub)からhello-world(https://hub.docker.com/_/hello-world)を持ってくるよ」
「持ってきた。完了!」
「Digestはこれ」
「ステータスは、新しいイメージをダウンロードしたとこ」

つまり、ローカルになかったから、Docker Hubからそのイメージを取得している(そこにもなければエラーになる)

そして「Hello from Docker!」以降は、このhello-worldイメージをビルドして実行した結果の出力。

もう一度実行してみると、

$ sudo 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.
    (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/

と、もうイメージは取得済みなので、結果出力の部分のみ出力している。

2. イメージの削除(失敗)

先程取得したDockerイメージのIDを確認して、削除までしてみる。
ここでは敢えて失敗する。

まず、Dockerイメージの一覧表示

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB

このコマンドで、現在ローカルにあるDockerイメージの

  • REPOSITORY
  • TAG
  • IMAGE ID
  • CREATED
  • SIZE

が確認できる。

イメージの削除には、「IMAGE ID」を使う(ここでは「bf756fb1ae65」)

$ sudo docker rmi bf756fb1ae65
Error response from daemon: conflict: unable to delete bf756fb1ae65 (must be forced) - image is being used by stopped container 8fbd46b10da1

rmi : イメージ削除(参考 : https://docs.docker.jp/engine/reference/commandline/rmi.html
失敗した。
エラー文を読みと、どうやら「8fbd46b10da1」のコンテナを停止する必要があるようだ。

3. コンテナの停止

「8fbd46b10da1」はなにか?
Dockerコンテナの一覧表示をしてみる。

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
8fbd46b10da1        hello-world         "/hello"            About a minute ago   Exited (0) About a minute ago                       distracted_noyce

※ps : コンテナ一覧表示
※-a : 全コンテナ表示オプション(デフォルトは実行中のみ)
参考:https://docs.docker.jp/engine/reference/commandline/ps.html

この結果を見ると、「8fbd46b10da1」はやはり「hello-world」イメージのコンテナだね。
このコンテナというのは、「docker run」の実行によってイメージから作られたもの。

じゃ、このコンテナを停止して、改めてイメージを削除してみる。

コンテナの停止

$ sudo docker stop 8fbd46b10da1
8fbd46b10da1

停止は成功したようだ。

イメージの削除

$ sudo docker rmi bf756fb1ae65
Error response from daemon: conflict: unable to delete bf756fb1ae65 (must be forced) - image is being used by stopped container 8fbd46b10da1

また失敗

どうやら停止だけでなく、コンテナの削除も必要らしい。

スポンサーリンク



4. コンテナを削除して、イメージを削除する。

Dockerイメージを削除するには、実行によって作られたコンテナの削除が必要。
なので、コンテナ削除してから、イメージの削除を行ってみる。

コンテナの削除

$ sudo docker rm 8fbd46b10da1
8fbd46b10da1

コンテナの一覧表示

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

コンテナがなくなった。


3度目の正直。イメージ削除

$ sudo docker rmi bf756fb1ae65
Untagged: hello-world:latest
Untagged: hello-world@sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63

削除が成功した。


最後に削除されたかイメージ確認をする

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

よし、きれいになくなった。



これがサンプルイメージを使った実行と削除の基本。
次は自分でDockerファイルを作成してビルドして実行するのを残そうかな。


以上。



Dockerのおすすめ本ではこちらの本がいろんなところで紹介されている。