Skip to content

Commit

Permalink
Add options to remove all exited containers
Browse files Browse the repository at this point in the history
Use the `-e` flag to remove all exited containers. If used in
conjunction with `-c`, it will find all containers that exited
or match the `PATTERN` provided.
  • Loading branch information
michael-yx-wu committed Feb 9, 2016
1 parent db67e06 commit 8d284d7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ A simple utility to make it easier to cleanup Docker images and containers.

- `-c PATTERN`: Remove all containers matched by `PATTERN`
- `-d`: Remove all images tagged as `<none>`
- `-e`: Remove all exited containers
- `-r`: By default `dockerclean` executes in `dry-run` mode to give you a chance
to preview any images or containers that will get deleted. Use the `-r` flag
to remove matched images and containers.
- `-i PATTERN`: Remove all images matched by `PATTERN`
- `-h`: Print usage and options

If `-e` and `-c` are used in conjunction, `dockerclean` will find all containers
that have exited or match the `PATTERN` provided.

## Build status
![master](https://circleci.com/gh/michael-yx-wu/dockerclean/tree/master.png?style=shield&circle-token=810386c47ffeb705bf8c4e52a88c0d2177e82230) (master)

Expand Down
26 changes: 18 additions & 8 deletions dockerclean
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ grepContainer=false
grepContainerPattern=''
grepImage=false
grepImagePattern=''
exitedContainers=false

function displayUsageAndSummary {
printf "%s\n\n%s\n" "$usage" "$summary"
Expand All @@ -19,7 +20,7 @@ function displayHelp {
displayUsageAndSummary
echo ''
declare -A optionsTable
local numFlags=5
local numFlags=6
optionsTable["0,0"]='-c PATTERN'
optionsTable["0,1"]='Remove all containers matched by PATTERN'
optionsTable["1,0"]='-d'
Expand All @@ -28,6 +29,8 @@ function displayHelp {
optionsTable["2,1"]='If the -r option is not given, display all images or containers that would be removed'
optionsTable["3,0"]='-i PATTERN'
optionsTable["3,1"]='Remove all images matched by PATTERN'
optionsTable["5,0"]='-e'
optionsTable["5,1"]='Remove all exited containers'
optionsTable["4,0"]='-h'
optionsTable["4,1"]='Print usage and options'
i=0
Expand Down Expand Up @@ -83,26 +86,30 @@ function removeImages {
}

function gatherContainersToRemove {
if ! "$grepContainer"; then
if "$grepContainer" && "$exitedContainers"; then
docker ps -a | grep -E "$grepContainerPattern|Exited"
elif "$grepContainer"; then
docker ps -a | grep -E "$grepContainerPattern"
elif "$exitedContainers"; then
docker ps -a | grep -E "Exited"
else
echo ''
return 0
fi
docker ps -a | grep "$grepContainerPattern"
}

function removeContainers {
if ! "$grepContainer"; then
if ! "$grepContainer" && ! "$exitedContainers"; then
echo ''
return 0
fi
if [ -z "$1" ]; then
echo 'No matching containers to remove'
return 0
fi
docker rm -f $(docker ps -a | grep $grepContainerPattern | awk '{print $NF}')
docker rm -f $(echo "$1" | awk '{print $1}')
}

while getopts ":c:dri:h" option; do
while getopts ":c:deri:h" option; do
case "$option" in
h)
displayHelp
Expand All @@ -118,6 +125,9 @@ while getopts ":c:dri:h" option; do
grepContainer=true
grepContainerPattern="$OPTARG"
;;
e)
exitedContainers=true
;;
i)
grepImage=true
grepImagePattern="$OPTARG"
Expand All @@ -137,7 +147,7 @@ while getopts ":c:dri:h" option; do
esac
done

if ! { "$dangling" || "$grepContainer" || "$grepImage"; } then
if ! { "$dangling" || "$grepContainer" || "$grepImage" || "$exitedContainers"; } then
echo 'Nothing will be removed: no removal strategy specified'
echo ''
displayHelpPrompt
Expand Down
5 changes: 5 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ cirros030:
container_name: cirros.030.dockerclean
image: cirros:0.3.0
command: sleep 300

cirrosexited:
container_name: cirros.exited.dockerclean
image: cirros:0.3.0
command: sleep 0
11 changes: 11 additions & 0 deletions tests/dockerclean-test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ setup() {
export alpine_26_container_id=$(docker inspect --format={{.Id}} alpine.26.dockerclean)
export alpine_latest_container_id=$(docker inspect --format={{.Id}} alpine.32.dockerclean)
export cirros_latest_container_id=$(docker inspect --format={{.Id}} cirros.030.dockerclean)
export cirros_exited_container_id=$(docker inspect --format={{.Id}} cirros.exited.dockerclean)
}

@test "matches containers without removing" {
Expand All @@ -69,6 +70,16 @@ setup() {
nothingRemoved
}

@test "matches exited containers without removing" {
results=$(./dockerclean -e)
num_results=$(echo "$results" | grep cirros | grep Exited | wc -l)
[ "$num_results" -eq 1 ]
cirros_exited_matched_container_id=$(docker inspect --format={{.Id}} $(echo "$results" | grep cirros | grep Exited | awk '{print $1}'))
[ "$alpine_exited_matched_container_id" = "$alpine_exited_container_id" ]

nothingRemoved
}

@test "matches images without removing" {
results=$(./dockerclean -i alpine)
num_results=$(echo "$results" | grep alpine | wc -l)
Expand Down

0 comments on commit 8d284d7

Please sign in to comment.