Dockerイメージを全部消す

#Dockerイメージの削除

Dockerはこれから勉強なので色々入れていって、すぐにごちゃごちゃになると思うので、消し方を記録しておく。
とりあえず入れたのを全部消したい。

取得済みイメージの一覧表示

```
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wordpress           latest              2da59c54a06a        3 days ago          543MB
nginx               latest              08393e824c32        5 days ago          132MB
mysql               5.7                 718a6da099d8        5 days ago          448MB
```

イメージを削除する前にコンテナを削除する必要があるので
動いているコンテナの確認

```
docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS                  NAMES
4b66ce372544        wordpress           "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes              0.0.0.0:8080->80/tcp   some-wordpress
9f748d50ab07        mysql:5.7           "docker-entrypoint.s…"   8 minutes ago       Up 8 minutes              3306/tcp, 33060/tcp    some-mysql
76819ac5972a        nginx               "/docker-entrypoint.…"   23 hours ago        Exited (0) 21 hours ago                          some-nginx
```

nginxのみ`STATUS`列の`Exited (0) 21 hours ago`で、停止していることがわかる。nginxのコンテナのみ削除できる。

`nginx`の`CONTAINER ID`を指定してコンテナを削除

```
$ docker rm 76819ac5972a
76819ac5972a
```

削除できていることを確認

```
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
4b66ce372544        wordpress           "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        0.0.0.0:8080->80/tcp   some-wordpress
9f748d50ab07        mysql:5.7           "docker-entrypoint.s…"   11 minutes ago      Up 11 minutes       3306/tcp, 33060/tcp    some-mysql
```

`nginx`が無くなっているのが確認できた。

停止していないコンテナを削除してみる。

```
$ docker rm 9f748d50ab07
Error response from daemon: You cannot remove a running container 9f748d50ab078c4c3c63be83df6c7726f4ce8664d7c01478cf026c3812d12209. Stop the container before attempting removal or force remove
```

稼働中なので削除できないと怒られる。

無理やり止めていい状況であれば、無理やり止める。今回は全部消すのが目的なので無理やり止める。

```
$ docker kill 9f748d50ab07
9f748d50ab07
$ docker kill 4b66ce372544
4b66ce372544
```

確認

```
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
4b66ce372544        wordpress           "docker-entrypoint.s…"   8 minutes ago       Exited (137) 4 seconds ago                        some-wordpress
9f748d50ab07        mysql:5.7           "docker-entrypoint.s…"   12 minutes ago      Exited (137) 32 seconds ago                       some-mysql
```

`STATUS`列が2つとも止まった。

あらためて2つとも削除する。

```
$ docker rm 9f748d50ab07
9f748d50ab07
$ docker rm 4b66ce372544
4b66ce372544
```

確認

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

全部止まった

やっとイメージが消せるので消す

```
$ docker rmi 08393e824c32
Untagged: nginx:latest
Untagged: nginx@sha256:36b74457bccb56fbf8b05f79c85569501...
Deleted: sha256:08393e824c32d456ff69aec72c64d1ab63fecdad...
Deleted: sha256:16ea6e7b0ecc56682daf0e01d89ffe04aeb702f6...
Deleted: sha256:d1c30fbca15bed39f9d6a613d05885ab2de964ed...
Deleted: sha256:e860d6c3bb27d4d057f6c5412b92afa0ae15664f...
Deleted: sha256:0e6a092cd837c31fb9b85896c5849a2a02ba8913...
$ docker rmi 718a6da099d8
Untagged: mysql:5.7
Untagged: mysql@sha256:da58f943b94721d46e87d5de208dc0730...
Deleted: sha256:718a6da099d82183c064a964523c0deca80619cb...
Deleted: sha256:058d93ef2bfb943ba6a19d8b679c702be96e3433...
Deleted: sha256:7bca77783fcf15499a0386127dd7d5c679328a21...
Deleted: sha256:183d05512fa88dfa8c17abb9b6f09a79922d9e9e...
Deleted: sha256:165805124136fdee738ed19021a522bb53de75c2...
Deleted: sha256:904abdc2d0bea0edbb1a8171d1a1353fa6de2215...
Deleted: sha256:d26f7649f78cf789267fbbca8aeb234932e23010...
Deleted: sha256:7fcf7796e23ea5b42eb3bbd5bec160ba5f5f47ec...
Deleted: sha256:826130797a5760bcd2bb19a6c6d92b5f4860bbff...
Deleted: sha256:53e0181c63e41fb85bce681ec8aadfa323cd00f7...
Deleted: sha256:d6854b83e83d7eb48fb0ef778c58a8b839adb932...
$ docker rmi 2da59c54a06a
Untagged: wordpress:latest
Untagged: wordpress@sha256:8e0e83b3feee510eae7b7d68bac74...
Deleted: sha256:2da59c54a06a069eb6830f4d2f125840f9672e96...
Deleted: sha256:a0b975d5fd5ff77342aa6add51bc6cb819a1ca20...
Deleted: sha256:2cdc7837508703fe0d38179bfb46736ed5966f1b...
Deleted: sha256:6ba52464be861d5820d3993f1679fb1959825b73...
Deleted: sha256:c009eaa948f2fdeac83c727e23ecf54e3cea2255...
Deleted: sha256:c1dcd220a514e0a20c296ac98504ae1a060611d9...
Deleted: sha256:4ea2c0ce663f0f818e515817b07f4c9a8748fff6...
Deleted: sha256:87c45811775aae1757da1a94f55ecbbbdeba70d9...
Deleted: sha256:f6e183c191d75a55ea7b0695c32ba9bd4a6f2dae...
Deleted: sha256:ff09d507a3ca7496a332579227770965c6156064...
Deleted: sha256:ffa2c187679ddbeeda9b9d41725fa4916f2b07f2...
Deleted: sha256:eaae8b52ab6afc664a6584033bd8942c7a99911a...
Deleted: sha256:56a340d84f17f5b4e83bd02ffd40a3b1273912dc...
Deleted: sha256:f8899bfcfa880bd6caa5078c27b488a37f6abdce...
Deleted: sha256:08b50110935f318104dd652795f0b0a2c6d007b3...
Deleted: sha256:3bcc29238c6f1ce1602b88b2317fd7429228c311...
Deleted: sha256:e7854f919e1ba15b6ac0b0e70cd2cb1eac7ca4cd...
Deleted: sha256:45c01c7d544fd67d36753507065e5458be16dfcf...
Deleted: sha256:8e311722d5028e3cb61f5dea8786ed5fbc54a3e3...
Deleted: sha256:2f245d38723aa315787ea341d92c1abbcccc9cb6...
Deleted: sha256:d0f104dc0a1f9c744b65b23b3fd4d4d3236b4656...
```

確認

```
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
```

全部消えた
すっきり