-
Notifications
You must be signed in to change notification settings - Fork 36
/
harbor.sh
executable file
·123 lines (109 loc) · 2.88 KB
/
harbor.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#/bin/bash
# work flow is:
# build -> run -> stop
PORT_DEFAULT=3000
port_inside=${PORT_DEFAULT}
port_outside=${PORT_DEFAULT}
img_name=mmt
function help
{
printf "run Docker\n\n"
layout="%4s %-7s %-15s %-20s\n"
printf "${layout}" flag option name note
printf "${layout}" ---- ------ ------------- ----
#printf "${layout}" -1 "" "Preset 1" "run build, list_builds, run"
printf "${layout}" -b "" "Build" "Step 1 - Build a new image, deleting the older one"
printf "${layout}" -r "" "run" "Step 2 - run ${img_name}"
printf "${layout}" -c "" "connect" "Step 3 - connect to ${img_name}"
printf "\n"
printf "${layout}" -B "" "list builds from step 1" ""
printf "${layout}" -C "" "list containers from step 3" ""
printf "${layout}" -d "" "delete" ""
printf "${layout}" -p "number" "Inside Port" "${port_inside}"
printf "${layout}" -P "number" "Outside Port" "${port_outside}"
printf "${layout}" -R "" "step 2, but run interactivly"
printf "${layout}" -h "" "this" ""
}
# Step 1 ; -b ; build an image from a Dockerfile
function build
{
#docker rmi --force mmt:latest
docker build . --tag mmt --rm
}
# -B ; list build image
function list_builds
{
docker image ls mmt:latest
}
# step 2 ; -r ; run the container, starting rails
function run
{
docker run \
--tty \
--publish ${port_inside}:${port_outside} \
--volume $(pwd):/build:cached \
--detach ${img_name} \
bash -c "cd /build && bundle install --path ~/bundle && rails s --bind 0.0.0.0"
}
# step 2 ; -R ; run the container, starting rails
function run_interactive
{
#"echo bundle install ; ./bin/rails s --bind 0.0.0.0; /bin/bash"
docker run \
--tty \
--interactive \
--publish ${port_inside}:${port_outside} \
--volume $(pwd):/build \
${img_name}
}
## Statistics on calling bundle install using a mounted directory:
#cached
# no parameter -
# cached - 37 s + 4'51"
# delegated - 5'11"
# outside of docker - 130 s
# inside to ~ - 2'7"
# --volume $(pwd):/build:delegated \
# -C ; list containers
function list_containers
{
docker container ls
}
# step 3 ; -c ; connect to a running container
function connect
{
id="$(docker container ls | grep mmt | head -n 1 | cut -f 1 -d ' ')"
if [ -z "${id}" ] ; then
echo container not found
bash ./harbor.sh -C
else
docker exec \
--interactive \
--tty \
"${id}" /bin/bash
fi
}
# remove
function delete
{
#doocker rm
#docker images ls
#docker images --filter dangling=true -q 2
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
}
while getopts "1bBcCdp:P:rR" opt; do
case ${opt} in
1) build ; run ; connect ;;
b) build ;;
B) list_builds ;;
c) connect ;;
C) list_containers ;;
d) delete ;;
p) port_inside=${OPTARG} ;;
P) port_outside=${OPTARG} ;;
r) run ;;
R) run_interactive ;;
h) help ; exit ;;
*) help ; exit ;;
esac
done