provide example flask project

This commit is contained in:
Daniel afx 2022-02-04 00:27:25 +02:00
parent 648e8ded1b
commit 24a92139eb
14 changed files with 256 additions and 10 deletions

View file

@ -1,14 +1,14 @@
version: '3.8'
services:
web:
flask:
build:
context: ./services/web
context: ./flask
dockerfile: Dockerfile.prod
command: gunicorn --bind 0.0.0.0:5000 manage:app
volumes:
- static_volume:/home/app/web/project/static
- media_volume:/home/app/web/project/media
- static_volume:/home/app/web/forest/static
- media_volume:/home/app/web/forest/media
expose:
- 5000
env_file:
@ -22,10 +22,10 @@ services:
env_file:
- ./.env.prod
nginx:
build: ./services/nginx
build: ./nginx
volumes:
- static_volume:/home/app/web/project/static
- media_volume:/home/app/web/project/media
- static_volume:/home/app/web/forest/static
- media_volume:/home/app/web/forest/media
ports:
- 1337:80
depends_on:

View file

@ -1,11 +1,11 @@
version: '3.8'
services:
web:
build: ./services/web
flask:
build: ./flask
command: python manage.py run -h 0.0.0.0
volumes:
- ./services/web/:/usr/src/app/
- ./flask/:/usr/src/app/
ports:
- 5000:5000
env_file:

23
flask/Dockerfile Normal file
View file

@ -0,0 +1,23 @@
# pull official base image
FROM python:3.9.5-slim-buster
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install system dependencies
RUN apt-get update && apt-get install -y netcat
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
# copy project
COPY . /usr/src/app/
# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]

69
flask/Dockerfile.prod Normal file
View file

@ -0,0 +1,69 @@
###########
# BUILDER #
###########
# pull official base image
FROM python:3.9.5-slim-buster as builder
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install system dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends gcc
# lint
RUN pip install --upgrade pip
RUN pip install flake8==3.9.1
COPY . /usr/src/app/
RUN flake8 --ignore=E501,F401 .
# install python dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
#########
# FINAL #
#########
# pull official base image
FROM python:3.9.5-slim-buster
# create directory for the app user
RUN mkdir -p /home/app
# create the app user
RUN addgroup --system app && adduser --system --group app
# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
# install dependencies
RUN apt-get update && apt-get install -y --no-install-recommends netcat
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*
# copy entrypoint-prod.sh
COPY ./entrypoint.prod.sh $APP_HOME
# copy project
COPY . $APP_HOME
# chown all the files to the app user
RUN chown -R app:app $APP_HOME
# change to the app user
USER app
# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]

14
flask/entrypoint.prod.sh Executable file
View file

@ -0,0 +1,14 @@
#!/bin/sh
if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."
while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done
echo "PostgreSQL started"
fi
exec "$@"

16
flask/entrypoint.sh Executable file
View file

@ -0,0 +1,16 @@
#!/bin/sh
if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."
while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done
echo "PostgreSQL started"
fi
python manage.py create_db
exec "$@"

57
flask/forest/__init__.py Normal file
View file

@ -0,0 +1,57 @@
import os
from werkzeug.utils import secure_filename
from flask import (
Flask,
jsonify,
send_from_directory,
request,
redirect,
url_for
)
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object("project.config.Config")
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(128), unique=True, nullable=False)
active = db.Column(db.Boolean(), default=True, nullable=False)
def __init__(self, email):
self.email = email
@app.route("/")
def hello_world():
return jsonify(hello="world")
@app.route("/static/<path:filename>")
def staticfiles(filename):
return send_from_directory(app.config["STATIC_FOLDER"], filename)
@app.route("/media/<path:filename>")
def mediafiles(filename):
return send_from_directory(app.config["MEDIA_FOLDER"], filename)
@app.route("/upload", methods=["GET", "POST"])
def upload_file():
if request.method == "POST":
file = request.files["file"]
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["MEDIA_FOLDER"], filename))
return """
<!doctype html>
<title>upload new File</title>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file><input type=submit value=Upload>
</form>
"""

11
flask/forest/config.py Normal file
View file

@ -0,0 +1,11 @@
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL", "sqlite://")
SQLALCHEMY_TRACK_MODIFICATIONS = False
STATIC_FOLDER = f"{os.getenv('APP_FOLDER')}/forest/static"
MEDIA_FOLDER = f"{os.getenv('APP_FOLDER')}/forest/media"

View file

View file

@ -0,0 +1 @@
hi!

23
flask/manage.py Normal file
View file

@ -0,0 +1,23 @@
from flask.cli import FlaskGroup
from project import app, db, User
cli = FlaskGroup(app)
@cli.command("create_db")
def create_db():
db.drop_all()
db.create_all()
db.session.commit()
@cli.command("seed_db")
def seed_db():
db.session.add(User(email="daniel@deflax.net"))
db.session.commit()
if __name__ == "__main__":
cli()

4
flask/requirements.txt Normal file
View file

@ -0,0 +1,4 @@
Flask==1.1.2
Flask-SQLAlchemy==2.5.1
gunicorn==20.1.0
psycopg2-binary==2.8.6

4
nginx/Dockerfile Normal file
View file

@ -0,0 +1,4 @@
FROM nginx:1.19-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

24
nginx/nginx.conf Normal file
View file

@ -0,0 +1,24 @@
upstream forest {
server flask:5000;
}
server {
listen 80;
location / {
proxy_pass http://forest;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /home/app/web/project/static/;
}
location /media/ {
alias /home/app/web/project/media/;
}
}