This Flask 2 app has the following features:
- uses Gunicorn webserver
- Gunicorn is set to reload at every change to the app.py file
- has curl, netcat, ps and vi (vim-tiny) commands installed
- runs as the non root user "web"
- uses multistage build
- uses Python 3.10 Debian Bullseye Slim image (less than 150 MB)
- can be deployed to Kubernetes (with or without nginx ingress)
Reference for Flask and Gunicorn ENV variables:
# Create pipenv virtualenv
pipenv install -r requirements.txt
# Debug using Gunicorn
pipenv run gunicorn --reload --bind 0.0.0.0:8000 app:app
Build container and run in Docker
# Build container
export DOCKER_BUILDKIT=1
docker image build -t flask-gunicorn:v1 .
# Start the container in Docker
docker run -d -p 8000:8000 flask-gunicorn:v1
# Enter the container to test it
docker exec -it <container_id> /bin/bash
Deploy to Minikube / Kubernetes
# Enable Minikube docker
eval $(minikube docker-env)
# Build container
export DOCKER_BUILDKIT=1
docker image build -t flask-gunicorn:v1 .
# Create deployment and service
kubectl apply -f kubernetes/deployment.yaml -f kubernetes/service.yaml
# Create nginx ingress
kubectl apply -f kubernetes/ingress.yaml
# Update service to LoadBalancer (requires MetalLB on Minikube)
kubectl apply -f kubernetes/service-lb.yaml