-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker-http-proxy
executable file
·173 lines (154 loc) · 4.65 KB
/
docker-http-proxy
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
172
173
#!/usr/bin/env bash
#
# Docker HTTP proxy wrapper script
#
# Copyright (c) 2016 Tomas Gerulaitis <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
########################################
# Variables
########################################
script=${0##*/}
command=$1
name="http-proxy"
image_name="codekitchen/dinghy-http-proxy"
certs_dir=$HOME/.docker/ssl
status=$(docker inspect --format "{{ .State.Running }}" $name 2> /dev/null)
color_reset="\033[0m"
color_positive="\033[32m"
color_negative="\033[31m"
color_neutral="\033[33m"
########################################
# Functions
########################################
# Print an informational message
function info {
echo -e "${color_reset}$@"
}
# Print a success message
function success {
echo -e "${color_positive}$@${color_reset}"
}
# Print an error message
function error {
echo -e "${color_negative}$@${color_reset}" >&2
}
# Print an error message and exit
function fail {
echo -e "${color_negative}$@${color_reset}" >&2
exit 1
}
########################################
# Usage
########################################
usage="\
Usage:
$script <command>
Available commands:
name Print out the name of the HTTP proxy container.
status Print out the status of the HTTP proxy container.
config Print out the current Nginx configuration.
start Start the HTTP proxy container, creating it if necessary.
stop Stop the HTTP proxy container.
rm Destroy a stopped HTTP proxy container, including its volumes.
Description:
$script is a wrapper to easily manage an Nginx proxy container. The
container, running $image_name, accepts incoming HTTP and HTTPS requests
and passes them to the appropriate docker container based on the request
Host header and the VIRTUAL_HOST container environment variable.
For HTTPS requests, the container uses an appropriate SSL certificate
mounted from $certs_dir.
For more information on the docker HTTP proxy container, see
https://github.com/jwilder/nginx-proxy
Author:
Tomas Gerulaitis
http://tgerulaitis.com/
"
########################################
# Main
########################################
case "$command" in
start)
case "$status" in
true)
fail "Proxy container is already running!"
;;
false)
docker start $name \
&& success "Proxy container started"
;;
*)
docker run -d \
-p 80:80 \
-p 443:443 \
-v $certs_dir:/etc/nginx/certs \
-v /var/run/docker.sock:/tmp/docker.sock \
-e CONTAINER_NAME=$name \
--restart always \
--name $name \
$image_name \
&& success "Proxy container created"
;;
esac
;;
stop)
if [ "$status" == "true" ]; then
docker stop $name \
&& success "Proxy container stopped"
else
fail "Proxy container is not running!"
fi
;;
rm)
if [ -n "$status" ]; then
docker rm -v $name
else
fail "Nothing to remove!"
fi
;;
name)
echo $name
;;
status)
case "$status" in
true)
info "Proxy container is running"
;;
false)
info "Proxy container is stopped"
;;
*)
info "Proxy container has not been setup"
;;
esac
;;
config)
if [ "$status" == "true" ]; then
docker exec $name cat /etc/nginx/conf.d/default.conf
else
fail "Proxy inactive!"
fi
;;
*)
fail "$usage"
;;
esac
exit 0