LoginSignup
1
0

More than 1 year has passed since last update.

Dockerイメージ・コンテナ・ネットワークをフィルターして削除する方法

Posted at

環境

Docker version 20.10.17, build 100c701

コンテナ

特定イメージを用いるコンテナ一覧

docker ps -a -f="ancestor=${TARGET_IMAGE}"

rust:1-alpine3.16の場合

docker ps -a -f="ancestor=rust:1-alpine3.16

特定イメージを用いるコンテナ一覧を削除

docker ps -a -f="ancestor=${TARGET_IMAGE}" | awk 'NR>1{print $1}' | xargs docker rm

rust:1-alpine3.16の場合

docker ps -a -f="ancestor=rust:1-alpine3.16" | awk 'NR>1{print $1}' | xargs docker rm

イメージ

一覧

docker images

指定のイメージ名

docker images ${TARGET_IMAGE_NAME}

タグを指定せずとも使える。

$ docker images mcr.microsoft.com/openjdk/jdk
REPOSITORY                      TAG         IMAGE ID       CREATED       SIZE
mcr.microsoft.com/openjdk/jdk   17-ubuntu   7e7e1637f85a   2 weeks ago   413MB
mcr.microsoft.com/openjdk/jdk   11-ubuntu   4c64ada6086f   2 weeks ago   398MB
# 完全一致の必要がある。Tabキーで補完が効く。
$ docker images jdk
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

レポジトリが<none>を表示

$ docker images | awk 'NR>1{ if($1 == "<none>") {print $0} }'
<none>                                          <none>         7c837e1dd39d   11 days ago     1.32GB
<none>                                          <none>         5449976ef777   11 days ago     1.27GB
<none>                                          <none>         17710c13d665   11 days ago     1.32GB         

レポジトリが<none>を削除

docker images | awk 'NR>1{ if($1 == "<none>") {print $3} }' | xargs docker rmi

ネットワーク

一覧

docker networks ls
# 又は
docker network list

指定のものを表示

# 名前。部分検索をしてくれる。
$ docker network ls -f="name=none"
NETWORK ID     NAME      DRIVER    SCOPE
d6399d0e8f30   none      null      local
# driver
$ docker network ls -f="driver=bridge"
NETWORK ID     NAME                     DRIVER    SCOPE
136344c54e59   bridge                   bridge    local

指定のものを削除

docker network ls -f="name=none" | awk 'NR>1{print $1}' | xargs docker network rm

参考

https://docs.docker.jp/v19.03/engine/reference/commandline/ps.html

お役立ち

https://qiita.com/mom0tomo/items/911b92cc18871f52a2a0

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0