-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerPortForward.sh
204 lines (159 loc) · 5.54 KB
/
dockerPortForward.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
function _version()
{
echo "0.1.0"
exit
}
function _usage()
{
echo -e "Run docker for access port in container\n"
echo -e "Usage:"
echo -e " bash $0 [OPTIONS...] CONTAINER\n"
echo -e "Options:"
echo -e " -p, --port=<public-port>:<private-port>\tPort for connect to node application"
echo -e " \t\t\t\t\t\tExample: -p 5858-5860:5858-5860 -p 9229:3229"
echo -e " -v, --version\t\t\t\t\tShow version information and exit"
echo -e " -h, --help\t\t\t\t\tShow help options"
echo ""
echo -e "Examples:"
echo -e "> bash $0 -p 5858:35858 container-name"
echo -e "> bash $0 -p 5858-5860:35858-35860 container-name"
echo -e "> bash $0 -p 5858-5860:35858-35860 -p 9229:3229 container-name"
exit
}
### Detect script parameters
###################################################
declare -a PORT
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case ${key} in
# Container name
################
-c|--container)
CONTAINER="$2"
shift
shift
;;
--container=*)
CONTAINER="${key#*=}"
shift
;;
# Forward port number (Node js container forward port)
##################################################
-p|--port)
PORT+=("$2")
shift
shift
;;
--port=*)
PORT+=("${key#*=}")
shift
;;
# Other options
###############
-v|--version)
_version
shift
;;
-h|--help)
_usage
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
if [[ ${#POSITIONAL[@]} -eq 0 ]]; then
_usage
fi
CONTAINER=${POSITIONAL[0]}
if [[ -z ${CONTAINER} ]]; then
echo "[ERR] Container name is required!"
exit 1
fi
if [[ -z ${PORT[@]} ]]; then
echo "[ERR] Forward port must be set!"
exit 1
fi
docker inspect ${CONTAINER} >> /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "[ERR] Container name \"${CONTAINER}\" not exist!"
exit 1
fi
trap removeContainer EXIT HUP INT QUIT PIPE TERM
function removeContainer() {
docker rm -fv ${CONTAINER}-private-forward >> /dev/null 2>&1
docker rm -fv ${CONTAINER}-public-forward >> /dev/null 2>&1
}
declare -i defaultPort=4848
declare -a accessPort
declare dockerPort
declare -a privateClientPort
declare -a privateDockerPort
declare -a publicClientPort
declare -a publicDockerPort
declare -a publicForwardPort
for i in ${PORT[@]}; do
IFS=':' read -ra accessPort <<< "${i}"
IFS='-' read -ra privateClientPort <<< "${accessPort[1]}"
IFS='-' read -ra publicClientPort <<< "${accessPort[0]}"
dockerPort+="-p ${accessPort[0]}:${accessPort[0]} "
if [[ ${#privateClientPort[@]} -ne ${#publicClientPort[@]} ]]; then
echo "Fail port forwarding! Public and private forward port index not equal"
exit 1
fi
if [[ ${#privateClientPort[@]} -eq 2 ]]; then
for j in $(seq 0 $((${privateClientPort[1]} - ${privateClientPort[0]}))); do
publicForwardPort+=(`expr ${publicClientPort[0]} + ${j}`)
privateDockerPort+=(${defaultPort} `expr ${privateClientPort[0]} + ${j}`)
publicDockerPort+=(`expr ${publicClientPort[0]} + ${j}` ${defaultPort})
defaultPort=$((defaultPort + 1))
done
else
publicForwardPort+=(${publicClientPort[0]})
privateDockerPort+=(${defaultPort} ${privateClientPort[0]})
publicDockerPort+=(${publicClientPort[0]} ${defaultPort})
defaultPort=$((defaultPort + 1))
fi
done
removeContainer
docker run -d -i --name ${CONTAINER}-private-forward --network container:${CONTAINER} docker.loc:5000/socat:1.0.3-alpine /bin/sh >> /dev/null 2>&1
if [[ $? -ne 0 ]]; then
removeContainer
echo "[ERR] Can't create container \"${CONTAINER}-private-forward\""
exit 1
fi
docker run -d -i --name ${CONTAINER}-public-forward ${dockerPort}docker.loc:5000/socat:1.0.3-alpine /bin/sh >> /dev/null 2>&1
if [[ $? -ne 0 ]]; then
removeContainer
echo "[ERR] Can't create container \"${CONTAINER}-public-forward\""
exit 1
fi
ip=`docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER}`
network=`docker inspect --format='{{range .NetworkSettings.Networks}}{{.NetworkID}}{{end}}' ${CONTAINER}`
function bindContainer() {
sleep 6
docker network disconnect ${2} ${CONTAINER}-public-forward >> /dev/null 2>&1
docker network connect ${2} ${CONTAINER}-public-forward >> /dev/null 2>&1
echo ${privateDockerPort[@]} | xargs -n 2 docker exec -d ${CONTAINER}-private-forward /bin/sh -c 'socat TCP-LISTEN:$1,fork TCP:127.0.0.1:$2' argv0
echo ${publicDockerPort[@]} | xargs -n 2 docker exec -d ${CONTAINER}-public-forward /bin/sh -c 'socat TCP-LISTEN:$1,fork TCP:'${1}':$2' argv0
echo "[INFO] Ready to access with public port(s): ${publicForwardPort[@]}"
echo "Processing container event..."
}
bindContainer ${ip} ${network}
docker events --filter "container=${CONTAINER}" --filter "event=restart" --format "{{.ID}}" | while read container_id
do
name=`docker inspect --format='{{.Name}}' ${container_id}`
if [[ ! ${name:1} = ${CONTAINER} ]]; then
continue
fi
docker restart ${CONTAINER}-private-forward >> /dev/null 2>&1
docker restart ${CONTAINER}-public-forward >> /dev/null 2>&1
ip=`docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER}`
network=`docker inspect --format='{{range .NetworkSettings.Networks}}{{.NetworkID}}{{end}}' ${CONTAINER}`
bindContainer ${ip} ${network}
done