This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
_dockerTaskGolang.sh
76 lines (69 loc) · 2.02 KB
/
_dockerTaskGolang.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
imageName="<%= imageName %>"
# Kills all running containers of an image and then removes them.
cleanAll () {
# List all running containers that use $imageName, kill them and then remove them.
docker kill $(docker ps -a | awk '{ print $1,$2 }' | grep $imageName | awk '{ print $1}') > /dev/null 2>&1;
docker rm $(docker ps -a | awk '{ print $1,$2 }' | grep $imageName | awk '{ print $1}') > /dev/null 2>&1;
}
# Builds the Docker image.
buildImage () {
echo "Building the image $imageName."
docker build -t $imageName .
}
# Runs docker-compose.
compose () {
echo "Composing."
docker-compose up -d
<%= openWebSiteCommand %>
}
# Runs the container.
runContainer () {
# Check if container is already running, stop it and run a new one.
docker kill $(docker ps -a | awk '{ print $1,$2 }' | grep $imageName | awk '{ print $1}') > /dev/null 2>&1;
# Create a container from the image.
<%= runImageCommand %>
<%= openWebSiteCommand %>
}
# Shows the usage for the script.
showUsage () {
echo "Description:"
echo " Builds and runs a Docker image."
echo ""
echo "Options:"
echo " build: Builds a Docker image ('$imageName')."
echo " run: Runs a container based on an existing Docker image ('$imageName')."
echo " buildrun: Builds a Docker image and runs the container."
echo " compose: Runs docker-compose."
echo " clean: Removes the image '$imageName' and kills all containers based on that image."
echo ""
echo "Example:"
echo " ./dockerTask.sh build"
echo ""
echo " This will:"
echo " Build a Docker image named $imageName."
}
if [ $# -eq 0 ]; then
showUsage
else
case "$1" in
"compose")
compose
;;
"build")
buildImage
;;
"run")
runContainer
;;
"clean")
cleanAll
;;
"buildrun")
buildImage
runContainer
;;
*)
showUsage
;;
esac
fi