Skip to content

Commit a301c7e

Browse files
committed
Merge pull request #1814 from 0rax/develop
Update docker `socat` link creation
2 parents eac91a7 + f7c7837 commit a301c7e

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

docker/start.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@ for f in /data/gogs/data /data/gogs/conf /data/gogs/log /data/git /data/ssh; do
1313
done
1414

1515
# Bind linked docker container to localhost socket using socat
16-
env | sed -En 's|(.*)_PORT_([0-9]*)_TCP=tcp://(.*):(.*)|\1_\2 socat -ls TCP4-LISTEN:\2,fork,reuseaddr TCP4:\3:\4|p' | \
17-
while read NAME CMD; do
18-
mkdir -p /app/gogs/docker/s6/SOCAT_$NAME
19-
echo -e "#!/bin/sh\nexec $CMD" > /app/gogs/docker/s6/SOCAT_$NAME/run
20-
chmod +x /app/gogs/docker/s6/SOCAT_$NAME/run
21-
done
16+
USED_PORT="3000:22"
17+
while read NAME ADDR PORT; do
18+
if test -z "$NAME$ADDR$PORT"; then
19+
continue
20+
elif echo $USED_PORT | grep -E "(^|:)$PORT($|:)" > /dev/null; then
21+
echo "init:socat | Can't bind linked container ${NAME} to localhost, port ${PORT} already in use" 1>&2
22+
else
23+
SERV_FOLDER=/app/gogs/docker/s6/SOCAT_${NAME}_${PORT}
24+
mkdir -p ${SERV_FOLDER}
25+
CMD="socat -ls TCP4-LISTEN:${PORT},fork,reuseaddr TCP4:${ADDR}:${PORT}"
26+
echo -e "#!/bin/sh\nexec $CMD" > ${SERV_FOLDER}/run
27+
chmod +x ${SERV_FOLDER}/run
28+
USED_PORT="${USED_PORT}:${PORT}"
29+
echo "init:socat | Linked container ${NAME} will be binded to localhost on port ${PORT}" 1>&2
30+
fi
31+
done << EOT
32+
$(env | sed -En 's|(.*)_PORT_([0-9]+)_TCP=tcp://(.*):([0-9]+)|\1 \3 \4|p')
33+
EOT
2234

2335
# Exec CMD or S6 by default if nothing present
2436
if [ $# -gt 0 ];then

0 commit comments

Comments
 (0)