Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove dangling images and volumes silently
#!/bin/bash
# Remove exited containers
EXITED_CONTAINERS=$(docker ps -a -f status=exited -q)
if [ ! -z "${EXITED_CONTAINERS}" ]
then
docker rm ${EXITED_CONTAINERS} &2>1
echo "Remove exited containers."
else
echo "...No exited containers."
fi
# Remove docker dangling images
DANGLING_IMAGES=$(docker images -f dangling=true -q)
if [ ! -z "${DANGLING_IMAGES}" ]
then
docker rmi ${DANGLING_IMAGES} &2>1
echo "Remove dangling images."
else
echo "...No dangling images."
fi
# Remove docker dangling volumes
DANGLING_VOLUMES=$(docker volume ls -f dangling=true -q)
if [ ! -z "${DANGLING_VOLUMES}" ]
then
docker volume rm ${DANGLING_VOLUMES} &2>1
echo "Remove dangling volumes."
else
echo "...No dangling volumes."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.