-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart
executable file
·79 lines (67 loc) · 2.03 KB
/
start
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
#!/bin/bash
SUDO=
declare -a ARGS=()
IMAGE=postgis12-python
NAME="$IMAGE"
DATA_DIR=
POSTGRES_USER=postgres
POSTGRES_PASSWORD=mysecretpassword
DOCKER_DATA_DIR=/var/lib/postgresql/data
POSTGRES_IP=127.0.0.1
POSTGRES_PORT=5432
USAGE="$(cat <<EOF
usage: $0 [-n <name>] [-d <directory>] [-h <ip>] [-p <port>] [-P <password>]
optional arguments:
-n <name> set docker container name (default: ${NAME})
-d <directory> set data directory (default: \${HOME}/.postgres/<name>)
-h <ip> set postgres ip (default: ${POSTGRES_IP})
-p <port> set postgres port (default: ${POSTGRES_PORT})
-P <password> set postgres user password (default: ${POSTGRES_PASSWORD})
EOF
)"
( id -Gn | grep -q docker ) || SUDO=sudo
parse_args() {
local OPTIND OPTARG OPT
while getopts "n:h:p:P:" OPT; do
case "$OPT" in
n) NAME="${OPTARG}";;
d) DATA_DIR="${OPTARG}";;
h) POSTGRES_IP="${OPTARG}";;
p) POSTGRES_PORT="${OPTARG}";;
P) POSTGRES_PASSWORD="${OPTARG}";;
*)
echo "$USAGE" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
[[ -z $DATA_DIR ]] && DATA_DIR="${HOME}/.postgres/${NAME}"
}
find_stopped_instances() {
echo "finding stopped instances..." >&2
for c in $(${SUDO} docker ps -a -q --filter ancestor="$IMAGE" --filter name="$NAME"); do
running="$(${SUDO} docker inspect -f {{.State.Running}} ${c})"
if [[ $running == true ]]; then
echo "${c} is already running" >&2
exit
else
echo "restarting ${c}..." >&2
exec ${SUDO} docker start "${c}"
fi
done
}
prepare_docker_parameters() {
if [[ ! -d $DATA_DIR ]]; then
mkdir -pv "$DATA_DIR" && chmod 755 "$DATA_DIR" || exit 1
fi
ARGS+=("--publish=${POSTGRES_IP}:${POSTGRES_PORT}:5432")
ARGS+=("--env=POSTGRES_PASSWORD=${POSTGRES_PASSWORD}")
ARGS+=("--env=PGDATA=${DOCKER_DATA_DIR}")
ARGS+=("--volume=${DATA_DIR}:${DOCKER_DATA_DIR}")
}
parse_args "$@"
find_stopped_instances
prepare_docker_parameters
echo "starting ${IMAGE}..."
${SUDO} docker run -d "${ARGS[@]}" --name "$NAME" "$IMAGE" "$@"