Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #6 from meltwater/evenmorecleanup
Browse files Browse the repository at this point in the history
Cleanup dead and stuck-in-created containers
  • Loading branch information
alexandernilsson committed Dec 15, 2015
2 parents 70d3ad4 + b19c5a5 commit 5951e11
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ do
# Cleanup unused volumes
/docker-cleanup-volumes.sh

# Cleanup exited containers
EXITED_CONTAINERS_IDS="`docker ps -a -q -f status=exited | xargs echo`"
# Cleanup exited/dead containers
EXITED_CONTAINERS_IDS="`docker ps -a -q -f status=exited -f status=dead | xargs echo`"
if [ "$EXITED_CONTAINERS_IDS" != "" ]; then
echo "Removing exited containers"
docker rm -v $EXITED_CONTAINERS_IDS
fi

# Get all containers in "created" state
rm -f CreatedContainerIdList
docker ps -a -q -f status=created | sort > CreatedContainerIdList

# Get all image ID
ALL_LAYER_NUM=$(docker images -a | tail -n +2 | wc -l)
docker images -q --no-trunc | sort -o ImageIdList
Expand Down Expand Up @@ -77,9 +81,19 @@ do
mv ToBeCleanedImageIdList2 ToBeCleanedImageIdList
fi

# Wait and remove images being used by containers from the delete list again. This prevents the images being pulled from deleting
echo "=> About to clean images after ${DELAY_TIME} seconds"
# Wait before cleaning containers and images
echo "=> Waiting ${DELAY_TIME} seconds before cleaning"
sleep ${DELAY_TIME} & wait

# Remove created containers that haven't managed to start within the DELAY_TIME interval
rm -f CreatedContainerToClean
comm -12 CreatedContainerIdList <(docker ps -a -q -f status=created | sort) > CreatedContainerToClean
if [ -s CreatedContainerToClean ]; then
echo "=> Start to clean $(cat CreatedContainerToClean | wc -l) created/stuck containers"
docker rm -v $(cat CreatedContainerToClean)
fi

# Remove images being used by containers from the delete list again. This prevents the images being pulled from deleting
CONTAINER_ID_LIST=$(docker ps -aq --no-trunc)
rm -f ContainerImageIdList
touch ContainerImageIdList
Expand Down

0 comments on commit 5951e11

Please sign in to comment.