-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.sh
72 lines (60 loc) · 1.29 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
#!/usr/bin/env bash
IMAGE_NAME=mysql
HOST_PORT=3306
MAP_PORT=3306
DATABASE_NAME=database.sql
err() {
echo $* >&2
}
usage() {
err "$(basename $0): [build|run|all|login]"
}
clean() {
IMAGE=$(docker ps -a -q --filter ancestor=${IMAGE_NAME} --format="{{.ID}}")
if ! test -z "$IMAGE"
then
docker rm $(docker stop ${IMAGE})
docker rmi -f ${IMAGE_NAME}
fi
}
build_docker() {
docker build -t ${IMAGE_NAME} .
}
launch() {
docker run -p ${HOST_PORT}:${MAP_PORT} -d ${IMAGE_NAME} --name mysql-server --env-file .env -d mysql
}
login() {
docker cp ${DATABASE_NAME} $(docker ps -q --filter ancestor=${IMAGE_NAME} --format="{{.ID}}"):${DATABASE_NAME}
docker exec -it $(docker ps -q --filter ancestor=${IMAGE_NAME} --format="{{.ID}}") /bin/bash
}
execute() {
local task=${1}
case ${task} in
build)
clean
build_docker
;;
run)
launch
;;
login)
login
;;
all)
clean
build_docker
launch
;;
*)
err "invalid task: ${task}"
usage
exit 1
;;
esac
}
main() {
[ $# -ne 1 ] && { usage; exit 1; }
local task=${1}
execute ${task}
}
main $@