Stand-alone Application to deploy



examples/deploy-stand-alone-python/app.py
from flask import Flask, request, jsonify
import os
import datetime

version = 1

app = Flask(__name__)

filename = 'counter.txt'
dirname = os.environ.get('COUNTER_DIR')
if dirname:
    filename = os.path.join(dirname, filename)

@app.route("/", methods=['GET'])
def main():
    now = datetime.datetime.now()

    counter = 0
    if os.path.exists(filename):
        with open(filename) as fh:
            counter = int(fh.read())
    counter += 1
    with open(filename, 'w') as fh:
        fh.write(str(counter))

    return f'''
        <html>
          <head><title>Demo v{version}</title></head>
          <body>Demo v{version} at {now} count: {counter}</body>
        </html>
    '''

examples/deploy-stand-alone-python/test_app.py
import app


def test_app():
    web = app.app.test_client()

    rv = web.get('/')
    assert rv.status == '200 OK'
    assert b'<body>Demo' in rv.data

examples/deploy-stand-alone-python/requirements.txt
flask

examples/deploy-stand-alone-python/Dockerfile
FROM python:3.7

COPY requirements.txt /opt/
RUN pip3 install -r /opt/requirements.txt


COPY app.py /opt/

WORKDIR /opt
CMD ["flask", "run", "--host", "0.0.0.0", "--port", "5000"]

examples/deploy-stand-alone-python/.dockerignore
__pycache__
.git
.pytest_cache
counter.txt

Locally


docker build -t flasker .
docker run --rm  -p5000:5000 flasker
http://localhost:5000/