Skip to content

Commit

Permalink
with nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
msaunby committed Jun 14, 2023
1 parent 2c1107c commit 4afae2a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
18 changes: 12 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Base Image
FROM python:3.11-slim-bullseye
# See https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/
FROM tiangolo/uwsgi-nginx-flask:python3.11

# Build args
ARG VERSION

# Working Directory
WORKDIR /app
# WORKDIR /app

# Install packages from requirements.txt
COPY requirements.txt /app/requirements.txt
Expand All @@ -15,7 +15,13 @@ RUN pip install --no-cache-dir --upgrade pip &&\
# Copy source code to working directory
COPY ./app /app

# Default env vars and command.
ENV PORT=5000
# Default env vars
ENV PORT=80
ENV VERSION=$VERSION
CMD ["python", "/app/main.py"]

#CMD pipenv run gunicorn --bind 0.0.0.0:8000 --timeout 120 --workers 2 cbwg.wsgi

# Use this CMD to only run the python app
#CMD pipenv run python /app/main.py
# Or this CMD if the requirements have been 'pip install'ed.
#CMD python /app/main.py
21 changes: 21 additions & 0 deletions Dockerfile-no-nginx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Base Image
FROM python:3.11-slim-bullseye

# Build args
ARG VERSION

# Working Directory
WORKDIR /app

# Install packages from requirements.txt
COPY requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade pip &&\
pip install --no-cache-dir --trusted-host pypi.python.org -r requirements.txt

# Copy source code to working directory
COPY ./app /app

# Default env vars and command.
ENV PORT=5000
ENV VERSION=$VERSION
CMD ["python", "/app/main.py"]
53 changes: 53 additions & 0 deletions PRODUCTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
When a Flask app is run you'll see
```
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
```

These notes explain why, and how, to use a production server.

## Why not use the built-in Flask web server?

The build in, development, server is single threaded, and quite likely has other performance and security issues.

It's very easy to create a suitable production server in a Docker container.

## Other considerations

### Cloud hosting

### HTTP/2

By default, Google Cloud Run downgrades HTTP/2 requests to HTTP/1 when those requests are sent to the container. There is a deployment setting, under Networking, that will override this.

The size of requests (uploads) can be unlimited if HTTP/2 is used.
Websockets can be used.

<https://cloud.google.com/run/quotas>

### HTTPS

Probably required for HTTP/2


## Tests

<https://cloud.google.com/run/docs/configuring/http2>

``curl -i --http2-prior-knowledge http://localhost:PORT
```
## Examples
<https://blog.miguelgrinberg.com/post/running-your-flask-application-over-https>
<https://medium.com/python-pandemonium/how-to-serve-http-2-using-python-5e5bbd1e7ff1>
## Docker test
When running Docker locally it's likely you will create a lot of test containers. To remove all stopped containers.
```
% docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N]
```
7 changes: 5 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from flask import Flask, render_template
from flask import Flask, render_template, make_response
import os

app = Flask(__name__)
version = os.environ.get('VERSION', 'v-.-.-')

@app.route("/")
def home():
return render_template('home.html', version=version)
response = make_response(render_template('home.html', version=version))
return response
# return render_template('home.html', version=version)


if __name__ == "__main__":
port = os.environ.get('PORT')
Expand Down

0 comments on commit 4afae2a

Please sign in to comment.