From f1e884070720d57d06cefbebc093c0d7e6c63765 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Tue, 26 Nov 2024 13:08:28 +0000 Subject: [PATCH] Allow a custom container port to be specified --- CHANGELOG.md | 1 + docker/tna-python/README.md | 1 + docker/tna-python/bin/tna-run | 15 +++++++++------ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2650155..fa0ad6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `git` is now available in `tna-python-dev` +- Ability to change port by specifying a `PORT` environment variable ### Changed diff --git a/docker/tna-python/README.md b/docker/tna-python/README.md index 444e9fd..10c9bdb 100644 --- a/docker/tna-python/README.md +++ b/docker/tna-python/README.md @@ -37,6 +37,7 @@ Any other alphanumeric string is considered a valid environment name but won't h | `SSL_KEY_FILE` | The location of the SSL key | `/home/app/ssl/key.pem` | `/home/app/ssl/key.pem` | `/home/app/ssl/key.pem` | `/home/app/ssl/key.pem` | | `SSL_CERTIFICATE_FILE` | The location of the SSL certificate | `/home/app/ssl/cert.pem` | `/home/app/ssl/cert.pem` | `/home/app/ssl/cert.pem` | `/home/app/ssl/cert.pem` | | `ALLOW_INSECURE` | If `true`, allow servers above development to run HTTP rather than HTTPS | `false` | `false` | _ignored_ | `false` | +| `PORT` | Set the port used by the container (only used outside of AWS) | `8080` | `8080` | `8080` | `8080` | [^1]: [Gunicorn docs - How Many Workers?](https://docs.gunicorn.org/en/latest/design.html#how-many-workers) diff --git a/docker/tna-python/bin/tna-run b/docker/tna-python/bin/tna-run index f3cc0b3..d0d7c9e 100755 --- a/docker/tna-python/bin/tna-run +++ b/docker/tna-python/bin/tna-run @@ -52,6 +52,9 @@ DEFAULT_THREADS=$((DEFAULT_WORKERS * 2)) [[ -z $WORKERS ]] && WORKERS=$DEFAULT_WORKERS [[ -z $THREADS ]] && THREADS=$DEFAULT_THREADS +# Set the port to 8080 if one is not defined +[[ -z $PORT ]] && PORT=8080 + if [ "$ENVIRONMENT" == 'develop' ] then # Development environment @@ -67,7 +70,7 @@ then if poetry show django ; then echo "Django found, starting server" - poetry run python /app/manage.py runserver 0.0.0.0:8080 + poetry run python /app/manage.py runserver "0.0.0.0:$PORT" fi echo "Django not found" @@ -76,7 +79,7 @@ then if poetry show flask ; then echo "Flask found, starting server" - poetry run flask --app "$APPLICATION" run --debug --host 0.0.0.0 --port 8080 + poetry run flask --app "$APPLICATION" run --debug --host 0.0.0.0 --port "$PORT" fi echo "Flask not found" @@ -85,13 +88,13 @@ then if poetry show fastapi ; then echo "FastAPI found, starting server" - poetry run uvicorn "$APPLICATION" --workers "$WORKERS" --log-level "$LOG_LEVEL" --timeout-keep-alive "$KEEP_ALIVE" --host 0.0.0.0 --port 8080 --reload + poetry run uvicorn "$APPLICATION" --workers "$WORKERS" --log-level "$LOG_LEVEL" --timeout-keep-alive "$KEEP_ALIVE" --host 0.0.0.0 --port "$PORT" --reload fi echo "FastAPI not found" # Fall back to using Gunicorn echo "No framework found, using Gunicorn to serve development application..." - poetry run gunicorn "$APPLICATION" --workers "$WORKERS" --threads "$THREADS" --log-level "$LOG_LEVEL" --timeout "$TIMEOUT" --keep-alive "$KEEP_ALIVE" --bind 0.0.0.0:8080 --worker-class="$WORKER_CLASS" --reload + poetry run gunicorn "$APPLICATION" --workers "$WORKERS" --threads "$THREADS" --log-level "$LOG_LEVEL" --timeout "$TIMEOUT" --keep-alive "$KEEP_ALIVE" --bind "0.0.0.0:$PORT" --worker-class="$WORKER_CLASS" --reload elif [ "$ENVIRONMENT" == 'production' ] then # Production environment @@ -117,7 +120,7 @@ if [ "$ALLOW_INSECURE" = 'true' ] then # Start the server echo "Starting $ENVIRONMENT server (insecure)..." - poetry run gunicorn "$APPLICATION" --workers "$WORKERS" --threads "$THREADS" --log-level "$LOG_LEVEL" --timeout "$TIMEOUT" --keep-alive "$KEEP_ALIVE" --access-logfile - --bind 0.0.0.0:8080 --worker-class="$WORKER_CLASS" + poetry run gunicorn "$APPLICATION" --workers "$WORKERS" --threads "$THREADS" --log-level "$LOG_LEVEL" --timeout "$TIMEOUT" --keep-alive "$KEEP_ALIVE" --access-logfile - --bind "0.0.0.0:$PORT" --worker-class="$WORKER_CLASS" else [[ -z $SSL_KEY_FILE ]] && SSL_KEY_FILE=/home/app/ssl/key.pem [[ -z $SSL_CERTIFICATE_FILE ]] && SSL_CERTIFICATE_FILE=/home/app/ssl/cert.pem @@ -136,5 +139,5 @@ else # Start the server echo "Starting $ENVIRONMENT server..." - poetry run gunicorn "$APPLICATION" --workers "$WORKERS" --threads "$THREADS" --log-level "$LOG_LEVEL" --timeout "$TIMEOUT" --keep-alive "$KEEP_ALIVE" --access-logfile - --bind 0.0.0.0:8080 --worker-class="$WORKER_CLASS" --keyfile="$SSL_KEY_FILE" --certfile="$SSL_CERTIFICATE_FILE" + poetry run gunicorn "$APPLICATION" --workers "$WORKERS" --threads "$THREADS" --log-level "$LOG_LEVEL" --timeout "$TIMEOUT" --keep-alive "$KEEP_ALIVE" --access-logfile - --bind "0.0.0.0:$PORT" --worker-class="$WORKER_CLASS" --keyfile="$SSL_KEY_FILE" --certfile="$SSL_CERTIFICATE_FILE" fi