-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker-purge-image.sh
57 lines (48 loc) · 1.79 KB
/
docker-purge-image.sh
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
#!/bin/sh
# =======================================================================================
# Purge Docker image
#
# This script stops and removes all container based on the Docker image, before removing
# the Docker image itself
# =======================================================================================
# ------------------------------------------------------
# Common parameters
export IMAGE_NAME=arm-qbittorrentvpn
if [[ "$1" = "h" ]] || [[ "$1" = "help" ]] || [[ "$1" = "-h" ]] || [[ "$1" = "-help" ]] || [[ "$1" = "--h" ]] || [[ "$1" = "--help" ]]; then
echo 'Remove a Docker image with all its Docker containers (automatically stops and removes them).'
echo ''
echo 'Usage:'
echo ' docker-purge-image.sh [IMAGE_NAME]'
echo ' docker-purge-image.sh h | help | -h | -help | --h | --help'
echo ''
echo 'Options:'
echo " IMAGE_NAME Name of the image [default: ${IMAGE_NAME}]"
echo ''
exit 1
fi
if [[ $1 ]]; then
IMAGE_NAME=$1
else
echo "Using default image name: ${IMAGE_NAME}"
fi
# ------------------------------------------------------
# Commands
if [[ $(docker ps -a --no-trunc | awk '{ print $1,$2 }' | grep -w ${IMAGE_NAME} | awk '{ print $1 }' | wc -l) != 0 ]] ; then
# Stop all containers based on image
echo "Stop all containers based on image"
docker stop $(docker ps -a --no-trunc | awk '{ print $1,$2 }' | grep -w ${IMAGE_NAME} | awk '{ print $1 }')
RESULT=$?
if [[ ${RESULT} != 0 ]] ; then
exit 1
fi
# Remove all containers based on image
echo "Remove all containers based on image"
docker rm $(docker ps -a --no-trunc | awk '{ print $1,$2 }' | grep -w ${IMAGE_NAME} | awk '{ print $1 }')
RESULT=$?
if [[ ${RESULT} != 0 ]] ; then
exit 1
fi
fi
# Remove image
echo "Remove image: ${IMAGE_NAME}"
docker rmi ${IMAGE_NAME}