Skip to content

Instantly share code, notes, and snippets.

@kuznero
Last active April 1, 2023 12:28
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save kuznero/0c751283ea77177a1db9 to your computer and use it in GitHub Desktop.
Save kuznero/0c751283ea77177a1db9 to your computer and use it in GitHub Desktop.
Cleanup docker images and containers after failed builds

Cleanup docker images and containers after failed builds

Sometimes, there are some untagged images left behind after failed builds. In order to get rid of those, this script can be used.

#!/bin/bash
docker rm $(docker ps -aq)
docker rmi $(docker images | grep "^<none>" | awk '{print $3}')
@agordeev
Copy link

agordeev commented Jan 24, 2018

This will remove all already existing containers as well, not only intermediate ones.

@jeanlucburot
Copy link

Yes, for instance if a container is run based on a working image and then stopped by the user, your first command will delete this container, which is not what you want as you only want to remove containers remaining of failed builds.

@phuctranfxvn
Copy link

Hi,
One option that can be used during the image building period is --force-rm which will remove the intermediate containers despite the image is successfully built or not.

--force-rm                Always remove intermediate containers

So we dont need to remove unused intermediate containers

@StanisLove
Copy link

Hi! The last command could be:

docker rmi $(docker images -f "dangling=true" -q)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment