-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerize-it
executable file
·171 lines (152 loc) · 5.27 KB
/
dockerize-it
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Simple wrapper for docker that use only one container and provide handy way to attach to started container
script_name="dockerize-it"
container_name="abn-container"
image_name="abn-image"
destination_path="/var/local/abn"
usage() {
echo "Usage: $script_name (clean | launch | shell | destroy | status | help)"
}
help() {
echo "Simple wrapper for docker that use only one container and provide handy way to attach to started container."
echo ""
echo "Usage: $script_name (clean | launch | shell | destroy | status | help)"
echo ""
echo "clean - remove existing docker container, and the image."
echo "launch - launch new container."
echo "shell - open new interactive terminal in the launched container."
echo "destroy - destroy the container."
echo "status - show information about the container."
echo "help - show this help information set."
echo ""
echo "Example flow:"
echo " $script_name launch"
echo " $script_name shell"
echo " # just do your work"
echo " # Ctrl+D to escape"
echo " $script_name destroy"
echo ""
echo "During your work you are using only one container."
echo "You can create as many shells as you want."
echo "If you exit the shell the container is still running."
echo ""
}
die() {
echo >&2 "$script_name: $@"
exit 1
}
# TODO: Here we are assuming that every container which is created from the images is managed by this script
# Instead of this let's check name of the container (container_name variable)
get_all_containers() {
local result=$(docker container ls -a | grep $container_name | awk '{print $1}')
[[ $(echo $result | wc -w) < 2 ]] || die "There are more than one container $container_name. Docker is in a vague state. Consider running $script_name clean."
echo $result
}
get_running_containers() {
local result=$(docker container ls -f "status=running" | grep $container_name | awk '{print $1}')
[[ $(echo $result | wc -w) < 2 ]] || die "There are more than one container $container_name. Docker is in a vague state. Consider running $script_name clean."
echo $result
}
get_images() {
local result=$(docker image ls -a | grep $image_name | awk '{print $1}')
[[ $(echo $result | wc -w) < 2 ]] || die "There are more than one image $image_name. Docker is in a vague state. Consider running $script_name clean."
echo $result
}
main() {
# input validation and locals set
[[ "$#" == 1 ]] || ! usage || die "Choose subcommand."
local subcommand=$1
local all=$(get_all_containers)
local running=$(get_running_containers)
local images=$(get_images)
case $subcommand in
clean)
# container cleaning
if [[ ! -z $all ]]; then
if [[ ! -z $running ]]; then
docker stop $running --time 0 > /dev/null
[[ $? == 0 ]] || die "Docker container stop failed."
fi
docker rm $all > /dev/null
[[ $? == 0 ]] || die "Docker container remove failed."
echo "Container cleaned."
fi
# image cleaning
if [[ ! -z $images ]]; then
docker image rm $images > /dev/null
[[ $? == 0 ]] || die "Docker image remove failed."
echo "Image cleaned."
fi
echo "Cleaning done."
;;
launch)
# TODO: select Dockerfile and its path (currently there is no way to run this script from eg. tools/ directory)
# check if container is currently running
[[ -z $running ]] || die "There is a working container $running. Destroy it first."
# build a docker image if needed
if [[ -z $images ]]; then
docker build -t $image_name .
[[ $? == 0 ]] || die "Docker container build failed."
fi
# create a container if needed
if [[ -z $all ]]; then
docker create -t -v $ABN_ROOT:$destination_path --name $container_name $image_name > /dev/null
[[ $? == 0 ]] || die "Docker container create failed."
fi
# start stopped or just created container
docker start $(get_all_containers) > /dev/null
[[ $? == 0 ]] || die "Docker container start failed."
echo "Container launched: $(get_running_containers)"
;;
shell)
# cannot to atach to not running container
[[ ! -z $running ]] || die "There is no running container here."
echo ""
echo "To escape press Ctrl+D."
# open bash in new interactive terminal
docker exec -it $running /bin/bash
[[ $? == 0 ]] || die "Docker execution in the container failed."
;;
destroy)
[[ ! -z $running ]] || die "There is no working container here."
# container cleaning
if [[ ! -z $all ]]; then
if [[ ! -z $running ]]; then
docker stop $running --time 0 > /dev/null
[[ $? == 0 ]] || die "Docker container stop failed."
fi
docker container rm $all
[[ $? == 0 ]] || die "Docker container remove failed."
echo "Container destroyed."
fi
;;
status)
# image information
if [[ ! -z $images ]]; then
echo "Available image: $images."
# container information
if [[ ! -z $running ]]; then
echo "Container $running is running."
else
if [[ -z $all ]]; then
echo "There is no container here."
echo "Consider running '$script_name launch'."
else
echo "Container $all is in a vague state."
echo "Consider running $script_name clear."
fi
fi
else
echo "There is no available images nor containers."
fi
;;
help)
help
;;
*)
usage
die "Wrong argument. Check subcommand list."
esac
}
# main function call
main $@