-
Notifications
You must be signed in to change notification settings - Fork 0
/
dup
93 lines (89 loc) · 2.68 KB
/
dup
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
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
. ~/.bashrc
# Help File
Help()
{
# Display Help
echo
echo " docker compose V2 [ Pull | Down | Up | rmi ]"
echo
echo -e " Syntax: ${GREEN}dup [-h|o|e|i] [SERVICE...]${NC}"
echo " options:"
echo " -h Print this Help."
echo
echo " -i Prints Current image list"
echo
echo " -o Only runs on a specific container"
echo -e " Example: ${GREEN}dup -o plex${NC} #Only update the Plex image"
echo
echo " -e Excludes a specific container"
echo -e " Example: ${GREEN}dup -e plex${NC} #Keeps plex running while shutting down and updating all other containers."
echo
echo -e " <none> Updates ALL containers in ${CYAN}docker-compose.yaml${NC}"
echo -e " Example: ${GREEN}dup${NC}"
echo
}
# Set Variables
ERROR_FILE="/tmp/docker-update.error"
only=''
exclude=''
service=''
CYAN='\033[0;36m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Get the options
while getopts "hio:e:" option; do
case $option in
h) # display Help
Help
exit;;
o) service=${OPTARG}
only="True";;
e) service=${OPTARG}
exclude="True";;
i) echo -e "$(docker ps -q | xargs -n 1 docker inspect --format '{{ .Name }}' | sed 's/\///')"
exit;;
\?) # incorrect option
echo "Error: Invalid option"
exit;;
esac
done
if [[ $only ]]
then
echo -e "${CYAN}Pulling $service container...${NC}"
docker compose pull $service
echo -e "${CYAN}Stopping $service container...${NC}"
docker compose rm -fsv $service
echo -e "${CYAN}Starting $service container...${NC}"
docker compose up -d --force-recreate $service
elif [[ $exclude ]]
then
# get a list of docker images that are currently installed
IMAGES_OUTPUT=$(docker ps -q | xargs -n 1 docker inspect --format '{{ .Name }}' | grep -v $service | grep -v "<none>" | sed 's/\///')
for IMAGE in $IMAGES_OUTPUT; do
echo "*****"
echo -e "Updating $IMAGE"
docker pull $IMAGE 2> $ERROR_FILE
docker compose rm -sfv $IMAGE
docker compose up -d --force-recreate $IMAGE
if [ $? != 0 ]; then
ERROR=$(cat $ERROR_FILE | grep "not found")
if [ "$ERROR" != "" ]; then
echo -e "WARNING: Docker image $IMAGE not found in repository, skipping"
else
echo -e "ERROR: docker pull failed on image - $IMAGE"
exit 2
fi
fi
echo "*****"
echo
done
else
docker compose pull
docker compose down
docker compose up -d --force-recreate
fi
echo -e "${CYAN}Deleting the old unused container images...${NC}"
docker system prune -af
echo -e "${GREEN}Successfully cleaned up${NC}"
echo -e "${GREEN}FINISHED${NC}"