Skip to content

Instantly share code, notes, and snippets.

@mlabouardy
Last active April 20, 2016 07:37
Show Gist options
  • Save mlabouardy/5a63bb35b221473169bc645e9fe37810 to your computer and use it in GitHub Desktop.
Save mlabouardy/5a63bb35b221473169bc645e9fe37810 to your computer and use it in GitHub Desktop.
Docker clean up
#!/bin/bash
docker_version_regex='^(1\.[4-8]\.)'
docker_version=`docker --version | awk '{print \$3}'`
log_directory='/logs'
log_file="$log_directory"/cleanup.log
if [ ! -d "$log_directory" ]; then
mkdir "$log_directory"
fi
echo -e "\n------------------------------------------------" >> "$log_file"
echo -e "------------------------------------------------" >> "$log_file"
echo "Starting docker cleanup at: $(date)" >> "$log_file"
if ! [[ $docker_version =~ $docker_version_regex ]]
then
echo "Docker version is not compatible. Program will stop." >> "$log_file"
exit 1
fi
disk_space_before=`df -k / | awk 'NR==2 {print \$4}'`
echo -e "\nStarting local repo cleanup..." >> "$log_file"
#remove unused docker images
echo -e "\nRemoving unused docker images" >> "$log_file"
docker rmi $(docker images -qa) &>> "$log_file"
#remove dangling images
echo -e "\nRemoving dangling docker images" >> "$log_file"
docker rmi $(docker images -q -f dangling=true)
#run cleanup volumes
echo -e "\nCleaning up volumes" >> "$log_file"
docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes &>> "$log_file"
echo -e "\n\nStarting private registry cleanup...\n" >> "$log_file"
sh /opt/ccvrp/maintenance/cleanup-routines/remove-orphan-images.sh &>> "$log_file"
disk_space_after=`df -k / | awk 'NR==2 {print \$4}'`
freed_diskspace=$(($disk_space_after - $disk_space_before))
echo -e "\nDocker cleanup finished. Total Disk space freed : $freed_diskspace Ko." >> "$log_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment