Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Last active January 7, 2023 06:47
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ORESoftware/bb8f97354ff38ee4a0a1dd1589af571a to your computer and use it in GitHub Desktop.
Cleanup docker artifacts/items

Clean up docker images/containers/volumes/networks

#!/usr/bin/env bash


set +e;

handle_empty(){
  while read line; do
    if test -z "$line"; then
        echo 'There was an empty line, exiting.' # > /dev/stderr
        exit 0;
    fi
    echo "$line"
  done;
}

export -f handle_empty;


# remove all unused / orphaned images
docker images --no-trunc | grep "<none>" | awk "{print \$3}" | handle_empty | xargs docker rmi -f | cat;


# clean up containers
docker ps --filter status=dead --filter status=exited -aq | handle_empty  | xargs docker rm -v | cat;


# clean up volumes
docker volume ls -qf dangling=true | handle_empty | xargs docker volume rm | cat;


# clean up networks
docker network ls --format "{{json . }}" | jq -r '.Name' | while read name; do
     if test "$name" == "interos-test"*; then
        docker network rm "$name" | cat
     fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment