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.
/
returns a messageHello World
and/healthz
returns an empty string. This route is configured to implement liveness checks for the application.
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')
-
Create a directory named
src/
and inside this directory create a file namedapp.py
. Paste the above code it inapp.py
. -
Create another file named
requirements.txt
at the same level assrc/
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 .
Updated 10 months ago