-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.sh
executable file
·108 lines (94 loc) · 2.89 KB
/
docker.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
################################################################################
# docker.sh - A helper script to run common docker commands for this project.
#
# Usage: $ ./docker.sh <help|build|run|clean>
# Copyright (C) 2020 S.G. Skinner
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################################
LINE_MONITOR_REPO="$HOME/git/line_monitor"
#
# Print help info when asked to or when `command` is invalid
#
function _usage() {
echo "$ ./docker.sh <help|build|run|clean>"
echo " help: Print these lines"
echo " build: Rebuild docker image for when Dockerfile changes"
echo " run: Start a new container from a built image"
echo " clean: Delete all dangling images, i.e., non-tagged"
}
#
# Create and start a new container from the latest sgs/python-utils image.
#
function _run() {
docker run \
-d \
-v "$LINE_MONITOR_REPO/app:/usr/src/app" \
-e TARGET_HOST="$TARGET_HOST" \
--rm \
--name line_monitor \
sgskinner/line_monitor:latest
}
#
# Build/rebuild a new image from the Dockerfile. Note this will 'steal' the tag
# from the current image if it exists. This means the old image will have a name
# that is the same as its id in the `$ docker images` list. Use `_clean` to clear
# out these orphaned images.
#
function _build() {
docker build \
--file Dockerfile \
--tag sgskinner/line_monitor:latest \
.
}
#
# Prune (delete) all images that are 'dangling', i.e., has no associated
# container and has not tagged name. These are the images in the
# '$ docker images` list where name and id are the same. These become
# dangling by way of a new build always tagging the image as
# 'sgs/python-sandbox:latest` -- only the latest build will retain the tag.
#
function _clean() {
# shellcheck disable=SC2046
docker rmi $(docker images -f "dangling=true" -q)
}
#
# Driver function, reads user command and executes the related function.
#
function main() {
if [[ $# -lt 1 ]]; then
_usage
exit 1
fi
command="$1"
case "$command" in
'run')
_run
;;
'build')
_build
;;
'clean')
_clean
;;
'help')
_help
;;
*)
_usage
;;
esac
}
main "$@"