Skip to content

Instantly share code, notes, and snippets.

@taikedz
Last active March 20, 2019 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taikedz/f2641a6c87e770f04722bad9b80299d5 to your computer and use it in GitHub Desktop.
Save taikedz/f2641a6c87e770f04722bad9b80299d5 to your computer and use it in GitHub Desktop.
Clean out docker images with `<none>` tags,. Removes leftover containers too.

Remove docker <none> images

After rebuilding an image several times and retagging it the same, you can have a whole load of images with name/tag as <none>/<none>.

THis script removes all stopped containers referencing those images, and then removes the images themselves, cleaning up your image and container lists.

#!/usr/bin/env bash
noneimages() {
docker images --format '{{.ID}} {{.Repository}}/{{.Tag}}'|grep '<none>/<none>'|while read id name; do
echo $id
done
}
nonecontainers() {
local images="$(noneimages|xargs echo|sed -r 's/ /|/g')";
docker ps -a --format '{{.ID}} {{.Image}}'|grep -E "$images"|while read cid imid; do
echo $cid
done
}
nonecontainers | xargs docker rm
noneimages | xargs docker rmi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment