Last active
August 25, 2017 17:58
-
-
Save Hades32/82b4f4115ed9045a0d15 to your computer and use it in GitHub Desktop.
Start a docker container with environment variables for external port and IP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Start a docker container with environment variables for external port and IP | |
if [ -z $1 ] | |
then | |
echo pass in starting port | |
exit 1 | |
fi | |
START_PORT=$1 | |
MAX_PORT=$(($START_PORT+10)) | |
# assumes datacenter IPs look like 10.x.y.z | |
HOST=$(ip addr | grep ' 10\.' | sed 's/.*\(10\..*\)\/.*/\1/') | |
for (( PORT=$START_PORT; $PORT <= $MAX_PORT; PORT++ )) | |
do | |
MSG=$(docker run -e PORT_80_EXTERNAL=$PORT -e HOST_EXTERNAL=$HOST -v /var/tmp/:/usr/share/nginx/html:ro -d -p $PORT:80 nginx 2>&1) | |
RES=$? | |
echo $MSG | grep 'port is already allocated' > /dev/null | |
PORT_ISSUE=$? | |
#echo res $RES msg $MSG port issue $PORT_ISSUE | |
if [ $RES -eq 0 ] | |
then | |
echo successfully started on external port $PORT on host $HOST | |
exit 0 | |
fi | |
if [ $PORT_ISSUE -ne 0 ] && [ $RES -ne 0 ] | |
then | |
echo error starting docker! | |
exit $RES | |
fi | |
done | |
echo found no free port between $START_PORT and $MAX_PORT | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment