はじめに
dockerのbuildサーバでstorage不足に陥った。不要なファイルを削除する必要があるが、安全に容量を確保するコマンドがあるので紹介。
まず対応すべきこと
現状把握
dockerのナニがdiskを圧迫しているのかの確認
“` docker system df “` コマンドでdockerに関係するdisk使用状況を確認することができる。
# docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 9 1 2.014GB 1.962GB (97%) Containers 2 0 0B 0B Local Volumes 4 2 824.6MB 781.2MB (94%) Build Cache 0 0 0B 0B
対処
止まってるコンテナ、使われてないボリューム、使われてないネットワーク、使われてないイメージを削除してくれる。
“` docker system prune “`
ただし、使われていないボリュームと、イメージの削除はオプションが必要である。
# docker system prune --help Usage: docker system prune [OPTIONS] Remove unused data Options: -a, --all Remove all unused images not just dangling ones --filter filter Provide filter values (e.g. 'label=<key>=<value>') -f, --force Do not prompt for confirmation --volumes Prune volumes
volumesの削除
355.4MBの削減できた。
553d15cd6b78ad0f21788a22e9ce16a7295c4bab97609973 deleted: sha256:7ae9338aed73a0f33568db53740431038d3a1f779c4dae40d27433984e1cd97c deleted: sha256:b1be54c8cadff1e50b87b93559320a1ae57b8d0dd326507148f7ca81d707beed deleted: sha256:86d78f10b9718618eaae056f5dfa1edae518949aee4578e4147268e9db2e75f0 Total reclaimed space: 355.4MB
しかし、これではstorageは削除できない。
“` –volumes “` オプションを追加することによってストレージも削除することができる。
docker system prune --volumes WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all volumes not used by at least one container - all dangling images - all build cache Are you sure you want to continue? [y/N] y
imagesの削除
“` –all “` オプションでimageの削除が可能。
# docker system prune --all WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all images without at least one container associated to them - all build cache Are you sure you want to continue? [y/N]
作業後
不要なファイルを削除し終わると0になる
# docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 0 0 0B 0B Containers 0 0 0B 0B Local Volumes 0 0 0B 0B Build Cache 0 0 0B 0B
参考情報
オライリー本より読みやすく、Dockerについて体系的に学ぶことができるのでおすすめ。