Skip to content

Instantly share code, notes, and snippets.

@GhazanfarMir
Last active October 23, 2024 05:56
Show Gist options
  • Save GhazanfarMir/da901b7fcdec40f07a5ba221372862eb to your computer and use it in GitHub Desktop.
Save GhazanfarMir/da901b7fcdec40f07a5ba221372862eb to your computer and use it in GitHub Desktop.
Single shell script to remove all containers, images and volumes used by containers. The script first tries to stop containers if there is any running, then remove the containers, followed by images removal and finally the container volumes.
#!/bin/bash
###########################################
#
# Simple Shell script to clean/remove all container/images
#
# The script will
# - first stop all running containers (if any),
# - remove containers
# - remove images
# - remove volumes
#
# stop all running containers
echo '####################################################'
echo 'Stopping running containers (if available)...'
echo '####################################################'
docker stop $(docker ps -aq)
# remove all stopped containers
echo '####################################################'
echo 'Removing containers ..'
echo '####################################################'
docker rm $(docker ps -aq)
# remove all images
echo '####################################################'
echo 'Removing images ...'
echo '####################################################'
docker rmi $(docker images -q)
# remove all stray volumes if any
echo '####################################################'
echo 'Revoming docker container volumes (if any)'
echo '####################################################'
docker volume rm $(docker volume ls -q)
@fromage9747
Copy link

I have docker volume rm $(docker volume ls -f dangling=true -q) in a script. If I run it just in the shell and not in the script, it works as expected. when I run the script it gives me:

image

Any idea why it does not work in the script?

@carlos3g
Copy link

@fromage9747 docker volume ls -f dangling=true -q is returning nothing.

@Stefen-Taime
Copy link

#!/bin/bash

Functions

stop_and_remove_containers() {
local pattern="$1"
if [ -z "$pattern" ]; then
containers=$(docker ps -a -q)
else
containers=$(docker ps -a | grep "$pattern" | awk '{print $1}')
fi

if [ -n "$containers" ]; then
echo "Stopping and removing containers..."
docker stop $containers
docker rm $containers
fi
}

remove_images() {
local pattern="$1"
if [ -z "$pattern" ]; then
images=$(docker images -a -q)
else
images=$(docker images -a | grep "$pattern" | awk '{print $3}')
fi

if [ -n "$images" ]; then
echo "Removing images..."
for image in $images; do
associated_containers=$(docker ps -a --filter=ancestor="$image" -q)
if [ -n "$associated_containers" ]; then
echo "Stopping and removing containers associated with image $image..."
docker stop $associated_containers
docker rm $associated_containers
fi
docker rmi "$image"
done
fi
}

Main script

echo "Cleaning up resources..."

Stop and remove containers based on a pattern

Leave the pattern empty to target all containers

stop_and_remove_containers "" # You can add a pattern, e.g., "my-container"

Remove images based on a pattern

Leave the pattern empty to target all images

remove_images "" # You can add a pattern, e.g., "my-image"

Remove dangling images

echo "Removing dangling images..."
docker image prune -f

Remove unused volumes

echo "Removing unused volumes..."
docker volume prune -f

Remove unused networks

echo "Removing unused networks..."
docker network prune -f

echo "All resources cleaned up successfully."

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