Last active
April 3, 2024 16:19
-
-
Save douglascayers/d9327b49103abe611969dc701065c4bc to your computer and use it in GitHub Desktop.
π Delete all docker services, containers, images, and volumes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# Exit when any command fails | |
set -e | |
# https://docs.docker.com/engine/reference/commandline | |
SCRIPT_NAME=$(basename ${0}) | |
DELETE_BUILDER_CACHE=0 | |
usage() { | |
echo "Usage: ${SCRIPT_NAME} [OPTIONS]" 1>&2 | |
echo "" | |
echo "Deletes docker containers, images, and volumes." | |
echo "" | |
echo "Options:" | |
echo " --cache Also delete builder cache" | |
echo " --help, -h Show this menu" | |
exit 1 | |
} | |
while [ "$1" != "" ]; do | |
case ${1} in | |
--cache) | |
DELETE_BUILDER_CACHE=1 | |
;; | |
--help | -h) | |
usage | |
;; | |
esac | |
shift | |
done | |
# ------------------------------------------------------------------------------ | |
# Composed Services | |
# ------------------------------------------------------------------------------ | |
echo "Stopping services..." | |
# The `docker compose` commands require knowing where the config file is. | |
# By default, it'll find `docker-compose.yml` in the current directory. | |
# But if you're wanting to stop services whose config files are elsewhere | |
# then we need to parse out those locations. | |
COMPOSE_FILES=($( | |
docker compose ls --all --format json \ | |
| jq -r '[ .[].ConfigFiles | split(",") ] | flatten | unique | .[]' | |
)) | |
for CFILE in "${COMPOSE_FILES[@]}"; do | |
if [ -f "${CFILE}" ]; then | |
docker compose -f "${CFILE}" stop | |
fi | |
done | |
echo "" | |
# ------------------------------------------------------------------------------ | |
# Containers | |
# ------------------------------------------------------------------------------ | |
echo "Removing containers..." | |
docker container prune --force | |
CONTAINERS=($(docker container ls --all -q)) | |
for CONTAINER in "${CONTAINERS[@]}"; do | |
docker container rm --force --volumes ${CONTAINER} | |
done | |
echo "" | |
# ------------------------------------------------------------------------------ | |
# Images | |
# ------------------------------------------------------------------------------ | |
echo "Removing images..." | |
docker image prune --all --force | |
IMAGES=($(docker image ls --all -q)) | |
for IMAGE in "${IMAGES[@]}"; do | |
docker image rm --force ${IMAGE} | |
done | |
echo "" | |
# ------------------------------------------------------------------------------ | |
# Volumes | |
# ------------------------------------------------------------------------------ | |
echo "Removing volumes..." | |
docker volume prune --force | |
VOLUMES=($(docker volume ls -q)) | |
for VOLUME in "${VOLUMES[@]}"; do | |
docker volume rm --force ${VOLUME} | |
done | |
echo "" | |
# ------------------------------------------------------------------------------ | |
# Builder Cache | |
# ------------------------------------------------------------------------------ | |
# This is behind a flag because it's a very time-consuming step. | |
# You may need to delete the builder cache in scenarios where you've | |
# used build secrets (--secret), you've changed the contents on disk | |
# of that secret, but docker isn't picking up the changes. | |
if [ ${DELETE_BUILDER_CACHE} = 1 ]; then | |
echo "Removing builder cache..." | |
docker builder prune --all --force | |
docker system prune --all --volumes --force | |
echo "" | |
fi | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment