-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-auto.sh
executable file
·108 lines (90 loc) · 2.68 KB
/
docker-auto.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
#!/usr/bin/env bash
set -e
###############################################
# Extract Environment Variables from .env file
# Ex. REGISTRY_URL="$(getenv REGISTRY_URL)"
###############################################
getenv(){
local _env="$(printenv $1)"
echo "${_env:-$(cat .env | awk 'BEGIN { FS="="; } /^'$1'/ {sub(/\r/,"",$2); print $2;}')}"
}
DOCKER_COMPOSE_VERSION="1.29.2"
CONF_ARG="-f docker-compose.yml"
SCRIPT_BASE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
REGISTRY_URL="$(getenv REGISTRY_URL)"
cd "$SCRIPT_BASE_PATH"
########################################
# Install docker-compose
# DOCKER_COMPOSE_VERSION need to be set
########################################
install_docker_compose() {
sudo curl -L "https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
return 0
}
if ! command -v docker-compose >/dev/null 2>&1; then
install_docker_compose
elif [[ "$(docker-compose version --short)" != "$DOCKER_COMPOSE_VERSION" ]]; then
install_docker_compose
fi
usage() {
echo "Usage: $(basename "$0") [MODE] [OPTIONS] [COMMAND]"
echo
echo "Mode:"
echo
echo "Options:"
echo " --help Show this help message"
echo " --with-hub Use the hub service"
echo
echo "Commands:"
echo " up Start the services"
echo " down Stop the services"
echo " ps Show the status of the services"
echo " logs Follow the logs on console"
echo " login Log in to a Docker registry"
echo " remove-all Remove all containers"
echo " stop-all Stop all containers running"
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
for i in "$@"; do
case $i in
--with-hub)
CONF_ARG="$CONF_ARG -f docker-compose-with-hub.yml"
shift
;;
--help|-h)
usage
exit 1
;;
*)
;;
esac
done
echo "Arguments: $CONF_ARG"
echo "Command: $@"
if [ "$1" == "login" ]; then
docker login $REGISTRY_URL
exit 0
elif [ "$1" == "up" ]; then
docker-compose $CONF_ARG pull
docker-compose $CONF_ARG build --pull
docker-compose $CONF_ARG up -d --remove-orphans
exit 0
elif [ "$1" == "stop-all" ]; then
if [ -n "$(docker ps --format {{.ID}})" ]
then docker stop $(docker ps --format {{.ID}}); fi
exit 0
elif [ "$1" == "remove-all" ]; then
if [ -n "$(docker ps -a --format {{.ID}})" ]
then docker rm $(docker ps -a --format {{.ID}}); fi
exit 0
elif [ "$1" == "logs" ]; then
shift
docker-compose $CONF_ARG logs -f --tail 200 "$@"
exit 0
fi
docker-compose $CONF_ARG "$@"