A Flask App with MongoDB

This is a complete step by step guide to deploy a basic flask based python application on facets.cloud. After the successful deployment, you should be able to access the application from the outside world.

Step 1: Create a simple flask application

We have a very simple flask application written in python. The application currently has two routes configured.

  1. / returns a message Hello World and
  2. /healthz returns an empty string. This route is configured to implement liveness checks for the application. :point-down:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello World'
@app.route('/healthz')
def healthz():
    return ''
# main driver function
if __name__ == '__main__':
    app.run(host='0.0.0.0')
  1. Create a directory named src/ and inside this directory create a file named app.py. Paste the above code it in app.py.

  2. Create another file named requirements.txt at the same level as src/ directory and put all the required application dependencies in it. For this application we only require flask.

Step 2: Dockerize the application

In order to deploy applications on Facets, the applications need to be containerized. For this we need to create images for various components of our application. For our demo application, we only have one component and hence we will create one docker image for it.

Create a file named Dockerfile at the same level as src/ directory.
Place the following content in the Dockerfile.

# Stage 1 - Install build dependencies
FROM python:3.7-alpine AS builder
WORKDIR /app
RUN python -m venv .venv && .venv/bin/pip install --no-cache-dir -U pip setuptools
COPY requirements.txt .
RUN .venv/bin/pip install --no-cache-dir -r requirements.txt && find /app/.venv \( -type d -a -name test -o -name tests \) -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) -exec rm -rf '{}' \+
# Stage 2 - Copy only necessary files to the runner stage
FROM python:3.7-alpine
RUN apk add --update --no-cache bind-tools
WORKDIR /app
COPY --from=builder /app /app
COPY src/ .
ENV PATH="/app/.venv/bin:$PATH"
CMD ["python", "app.py"]

Build and tag the docker image by running the following command in the same directory where you created Dockerfile.

docker build -t facets-python-demo:1 .

Did this page help you?