-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-clean
More file actions
executable file
·80 lines (71 loc) · 1.85 KB
/
docker-clean
File metadata and controls
executable file
·80 lines (71 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh
#
# Copyright (c) 2017-2019 Gaël PORTAY
#
# SPDX-License-Identifier: MIT
#
set -e
usage() {
cat <<EOF
Usage: ${0##*/} [OPTIONS] [--] [DOCKER_RM_RMI_EXTRA_ARGUMENTS]
Remove exited containers and dangling images.
Options:
-E or --no-exited Do not remove exited containers.
-D or --no-dangling Do not remove dangling images.
-c or --created Remove created containers.
--running Remove running containers.
--all Remove all containers except running.
ie. exited, dangling, and created.
-h or --help Print usage.
EOF
}
exited=true
dangling=true
created=false
running=false
while [ "$#" -ne 0 ]; do
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 0
elif [ "$1" = "-E" ] || [ "$1" = "--no-exited" ]; then
exited=false
elif [ "$1" = "-D" ] || [ "$1" = "--no-dangling" ]; then
dangling=false
elif [ "$1" = "-c" ] || [ "$1" = "--created" ]; then
created=true
elif [ "$1" = "--running" ]; then
running=true
elif [ "$1" = "--all" ]; then
exited=true
dangling=true
created=true
elif [ "$1" = "--" ]; then
shift
break
else
usage
echo "$1: Invalid argument!" >&2
exit 1
fi
shift
done
# Remove exited containers
if $exited; then
docker ps --quiet --all --filter "status=exited" | \
while read -r id; do docker rm "$id" "$@"; done
fi
# Remove dangling images
if $dangling; then
docker images --quiet --filter "dangling=true" | \
while read -r id; do docker rmi "$id" "$@"; done
fi
# Remove created containers
if $created; then
docker ps --quiet --all --filter "status=created" | \
while read -r id; do docker rm "$id" "$@"; done
fi
# Remove running containers
if $running; then
docker ps --quiet --all --filter "status=running" | \
while read -r id; do docker rm "$id" "$@" --force; done
fi