docker のイメージを以下の Qiita 記事のコマンドで削除したところ、

docker images を全削除する - Qiita
はじめに コンテナが動いているとイメージを削除できないので、本当に全削除したいときはコンテナから全削除する。 コンテナを全削除する docker ps -aq | xargs docker rm イメージを全削除する...
いくつかのイメージの削除が以下記事記載のエラーで失敗しました。
上記記事によると、リポジトリとタグを指定して削除すればいいよ。とのことでしたが、私の場合は対象のイメージが大量にあったので一つずつ指定するのが手間でした。
ということで以下のコマンドで削除できます。
リポジトリ名とタグ名を抽出して、コロンで連結して各イメージを削除しています。
ただしタグが<none>の場合は削除に失敗するのでご注意を。
docker rmi $(docker image ls | awk 'NR>1{print $1,$2}' | sed -e "s/ /:/g")
解説
docker rmi
1つまたは複数の イメージ を 削除 します。
https://docs.docker.jp/engine/reference/commandline/rmi.html
docker image ls
イメージを一覧表示します。
https://docs.docker.jp/engine/reference/commandline/image_ls.html
実際のコマンド結果は、以下のようにカラム形式で「REPOSITORY」「TAG」「IMAGE ID」「CREATED」「SIZE」が表示されます。
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
helloworldfunction rails-v1 1c79a6b02a3b 4 months ago 489MB
xxx.dkr.ecr.ap-northeast-1.amazonaws.com/railssample4b62256f/helloworldfunction19d43fc4repo helloworldfunction-1c79a6b02a3b-rails-v1 1c79a6b02a3b 4 months ago 489MB
xxx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world latest 7a8bcf113147 4 months ago 910MB
hello-world latest 7a8bcf113147 4 months ago 910MB
awk ‘NR>1{print $1,$2}’
パイプで docker image ls の内容を受け取り、NR>1 で先頭行を除外し、$1,$2 で1,2列目のみを出力します。
$ docker image ls | awk 'NR>1{print $1,$2}'
helloworldfunction rails-v1
xxx.dkr.ecr.ap-northeast-1.amazonaws.com/railssample4b62256f/helloworldfunction19d43fc4repo helloworldfunction-1c79a6b02a3b-rails-v1
xxx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world latest
hello-world latest
awk(1) manページ
sed -e “s/ /:/g”
docker rmi で削除するために、リポジトリ名とタグの間の空白を「:」に置換します。
$ docker image ls | awk 'NR>1{print $1,$2}' | sed -e "s/ /:/g"
helloworldfunction:rails-v1
xxx.dkr.ecr.ap-northeast-1.amazonaws.com/railssample4b62256f/helloworldfunction19d43fc4repo:helloworldfunction-1c79a6b02a3b-rails-v1
xxx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world:latest
hello-world:latest