Created
December 7, 2020 16:19
-
-
Save Hermsi1337/fd3e6492e549cf221e256b098474a600 to your computer and use it in GitHub Desktop.
Docker Cleanup Script for Gitlab-Runners
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
#!/usr/bin/env bash | |
if [[ -n "${DEBUG}" ]]; then | |
set -x | |
fi | |
# Run container removal | |
CONTAINER_NAMES_TO_EXCLUDE=() | |
CONTAINER_NAMES_TO_EXCLUDE+=("gitlab-runner") | |
docker container ps -aq | while read ROW_CONTAINER_ID ; do | |
ROW="$(docker ps -a | grep "${ROW_CONTAINER_ID}")" | |
# check if container name is excluded | |
for i in "${CONTAINER_NAMES_TO_EXCLUDE[@]}"; do | |
if echo "${ROW}" | grep -q "${i}"; then continue 2; fi | |
done | |
# if the container is exited, remove it | |
if [[ "$(docker container inspect "${ROW_CONTAINER_ID}" --format "{{.State.Status}}")" != "running" ]]; then | |
docker container rm "${ROW_CONTAINER_ID}" | |
continue | |
fi | |
# if the container is running seconds or minutes (<60), then skip | |
if echo "${ROW}" | egrep -i "up\s[0-9]+\s(seconds|minutes)" ; then | |
continue | |
fi | |
# if the container is running longer than an hour, kill it with fire | |
docker container stop "${ROW_CONTAINER_ID}" || continue | |
docker container rm "${ROW_CONTAINER_ID}" || continue | |
done | |
# Run volume removal | |
VOLUME_NAMES_TO_EXCLUDE=() | |
docker volume ls -q | while read ROW_VOLUME_NAME ; do | |
ROW="$(docker volume ls | grep "${ROW_VOLUME_NAME}")" | |
# check if volume name is excluded | |
for i in "${VOLUME_NAMES_TO_EXCLUDE[@]}"; do | |
if echo "${ROW}" | grep -q "${i}"; then continue 2; fi | |
done | |
# remove the volume and skip on error (e.g. when the volume is attached) | |
docker volume rm "${ROW_VOLUME_NAME}" || continue | |
done | |
# Run image removal | |
IMAGE_NAMES_TO_EXCLUDE=() | |
IMAGE_NAMES_TO_EXCLUDE+=("gitlab/gitlab-runner") | |
docker image ls -q | while read ROW_IMAGE_NAME ; do | |
ROW="$(docker image ls | grep "${ROW_IMAGE_NAME}")" | |
# check if image name is excluded | |
for i in "${IMAGE_NAMES_TO_EXCLUDE[@]}"; do | |
if echo "${ROW}" | grep -q "${i}"; then continue 2; fi | |
done | |
# if the image is not older then a week, skip | |
if ! echo "${ROW}" | egrep -i "[0-9]+\s(week|month|year)s?\sago" ; then | |
continue | |
fi | |
# remove the image and skip on error (e.g. when the image is still used) | |
# we need to use -f here because some images can't be deleted due to parent-image-construct-foo | |
docker image rm -f "${ROW_IMAGE_NAME}" || continue | |
done | |
## also remove all dangling images | |
docker image prune -f | |
# Run network removal | |
NETWORK_NAMES_TO_EXCLUDE=() | |
docker network ls -q | while read ROW_NETWORK_ID ; do | |
ROW="$(docker network ls | grep "${ROW_NETWORK_ID}")" | |
# check if image name is excluded | |
for i in "${NETWORK_NAMES_TO_EXCLUDE[@]}"; do | |
if echo "${ROW}" | grep -q "${i}"; then continue 2; fi | |
done | |
# remove the network and skip on error (e.g. when the network is still used) | |
docker network rm "${ROW_NETWORK_ID}" || continue | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment