Dockerを使っていると分かるのですが、イメージを作ったりコンテナを立ち上げたりしていると次第に履歴がゴミのようにたまっていきます。もちろん必要な場合もあるのですが、開発途中は不要だったりします。 それらの整理をしてくれるのがDocker-Cleanです。簡単なコマンドでDocker上の不要なリソースを整理してくれます。

Docker-Cleanの使い方

Docker-Cleanでイメージを削除します。

$ docker-clean images
No containers To clean!
Cleaning images...
Error: No such image or container: 178ab09fdd10
Error: No such image or container: 178ab09fdd10
Error: No such image or container: 518a026fd577

ヘルプです。基本的にはrunまたはallだけで良さそうです。

$ docker-clean --help

  Docker Clean Usage Options: 
-------------------------------

-h or --help        Opens this help menu
-v or --version     Prints the current docker-clean version

 Running without any options will remove dangling volumes and untagged images only.
 All of the options are option, and while they overlap they can all be run concurrently.
 NOTE: By default, created containers will always be included, see -d, --created.

stop         Stops and removes all containers, cleans dangling volumes, and networks

images       Removes all tagged and untagged images, stopped containers, 
             dangling volumes, and networks

run          Removes all stopped containers, untagged images, dangling volumes, and networks

all          Stops and removes all containers, images, volumes and networks

Additional Flag options:

-H   or --host        Specifies the docker host to run against
                      Useful for docker swarm maintenance
                      ie: -H 127.0.0.1:4000

-n   or --dry-run     Adding this additional flag will list items to be
                      removed without executing any stopping or removing commands

-s   or --stop        Stops all running containers

-c   or --containers  Removes all stopped containers

-i   or --images      Removes all untagged images

-net or --networks    Removes all empty Networks (all network cleans are only empty)

-r   or --restart     Restarts the docker machine/daemon

-d   or --created     By default, CREATED containers are set to be removed.  Adding this
                      flag will ensure that all created containers are not cleaned

-t   or --tagged      Removes all tagged images

-a   or --all         Stops and removes all Containers, Images, AND Restarts docker

-l   or --log         Adding this as an additional flag will list all
                      image, volume, and container deleting output

Docker-Cleanを使えば不要な設定やリソースが綺麗に整理されます。dry-runを使って一旦削除されそうなものを確認し、問題なければ後は自動で運用することもできるでしょう。Dockerを使っていて定期的にリソースの削除を行っていた方はぜひ試してみましょう。

Docker-CleanはDocker用、MIT Licenseのオープンソース・ソフトウェアです。

ZZROTDesign/docker-clean: A script that cleans docker containers, images, volumes, and networks.