-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathcontainer.sh
More file actions
executable file
·130 lines (114 loc) · 2.77 KB
/
container.sh
File metadata and controls
executable file
·130 lines (114 loc) · 2.77 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh
set -e
container_name="libkqueue-dev"
dev_image="libkqueue-clang-cmake-dev:latest"
# Verbosity
verbose=0
debug() {
if [ "$verbose" -eq 1 ]; then
echo "$@"
fi
}
error() {
echo "$@" >&2;
}
# Parse flags
while [ $# -gt 0 ]; do
case "$1" in
-v)
verbose=1
shift
;;
start|stop|restart|status)
cmd="$1"
shift
;;
*)
echo "Usage: $0 [-v] {start|stop|restart|status}"
exit 1
;;
esac
done
[ -z "$cmd" ] && cmd="start"
hash_file() {
# portable sha256
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{print $1}'
else
# macOS has openssl by default
openssl dgst -sha256 "$1" | awk '{print $NF}'
fi
}
image_build() {
[ -f Dockerfile ] || { error "no Dockerfile in $(pwd)"; exit 1; }
hash=$(hash_file Dockerfile)
debug "(re-)building image with -t $dev_image --build-arg IMAGE_FINGERPRINT=$hash ."
docker build -t "$dev_image" --build-arg "IMAGE_FINGERPRINT=$hash" .
}
run_container() {
debug "Starting container '$container_name' with image '$dev_image'"
docker run --rm -it \
--name "$container_name" \
--hostname "$container_name" \
-u "$(id -u)":"$(id -g)" \
--privileged \
-e HOME=/home/dev \
-w /home/dev \
-v "$PWD":/home/dev \
"$dev_image"
}
start_container() {
expected=$(hash_file Dockerfile)
current=$(docker image inspect "$dev_image" \
--format '{{ index .Config.Labels "dev.fingerprint" }}' 2>/dev/null || true)
if [ -z "$current" ]; then
debug "Image missing or unlabeled, building"
image_build
run_container
fi
if [ "$current" != "$expected" ]; then
debug "fingerprint changed ($current -> $expected), rebuilding"
image_build
docker rm -f "$container_name"
start_container
fi
if ! docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then
run_container
else
debug "Getting shell on existing container '$container_name'"
docker exec -it ${container_name} /bin/bash
fi
}
stop_container() {
if docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then
debug "Stopping and removing container '$container_name'"
docker rm -f "$container_name" > /dev/null
else
debug "Container '$container_name' is not running"
fi
}
status_container() {
if docker ps --format '{{.Names}}' | grep -q "^${container_name}$"; then
echo "Container '$container_name' is running"
else
echo "Container '$container_name' is not running"
exit 1
fi
}
case "$cmd" in
start)
start_container
;;
stop)
stop_container
;;
restart)
stop_container
start_container
;;
status)
status_container
;;
esac