Last active
December 3, 2024 10:02
-
-
Save pancudaniel7/3e2e195a756a1e944a0cde5c2a5fe1e6 to your computer and use it in GitHub Desktop.
Docker clean bash function
This file contains hidden or 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
#!/bin/bash | |
# define the function | |
function docker_clean { | |
echo "Start cleaning..." | |
process_count=$(docker ps -q | wc -l) | |
if [ "$process_count" -ne "0" ]; then | |
docker kill $(docker ps -q) > /dev/null | |
echo "$process_count process/es had been clean" | |
fi | |
background_process_count=$(docker ps -a -q | wc -l) | |
if [ "$background_process_count" -ne "0" ]; then | |
docker rm -f $(docker ps -a -q) > /dev/null | |
echo "$background_process_count background process/es had been clean" | |
fi | |
volumes_count=$(docker volume ls -q | wc -l) | |
if [ "$volumes_count" -ne "0" ]; then | |
docker volume rm -f $(docker volume ls -q) > /dev/null | |
echo "$volumes_count volume/s had been clean" | |
fi | |
echo "Finish cleaning" | |
} | |
# call the function | |
docker_clean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment