Skip to content

Commit

Permalink
Allow a custom container port to be specified (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood authored Nov 26, 2024
1 parent 2486768 commit 2118516
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docker/tna-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 9 additions & 6 deletions docker/tna-python/bin/tna-run
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand All @@ -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"

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 2118516

Please sign in to comment.