-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.sh
executable file
·64 lines (55 loc) · 1.45 KB
/
main.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
#!/bin/bash -e
function build {
# $1 = image name, $2 = external port
if [ $# -lt 2 ]; then
echo "Error Format..."
echo "./main.sh build <image_name> <external_port>"
exit
fi
echo "Image name: $1"
echo "External port: $2"
# build the image from Dockerfile
docker build -f Dockerfile -t $1 .
# build the container from this image
docker run -d -p $2:22 --privileged --name $1"_c" $1 > /dev/null
# find which port mapping to 22
docker port $1"_c" 22
}
function clean {
# stop and rm
docker container stop u109_c
docker container rm u109_c
# rm this image
docker image rm u109
}
function run {
# build from existed image
docker run -d -p 9487:22 --privileged --name u109_c u109 > /dev/null
# find which port mapping to 22
docker port u109_c 22
}
function cleanall {
# stop all container
docker stop $(docker ps -a -q)
# remove all container
docker rm $(docker ps -a -q)
}
function delall {
# delete all docker images (with --force)
docker rmi -f $(docker images -q)
}
# main
# '$#' refer to the number of parameters (receive at runtime)
# '$1' denote the first command line argument passed
if [ $# -eq 0 ]; then
echo "Usage: $(basename $0) {build|run|clean|cleanall}"
echo "\t./main.sh build <image_name> <external_port>"
exit
fi
if [ "$1" == "build" ]; then
# $2 = image name
# $3 = external port
$1 $2 $3
exit
fi
$1