Switch to UWSGI and improve Docker definitions

This commit is contained in:
Mark Veidemanis 2022-10-18 07:22:22 +01:00
parent d3694d1821
commit 8ce0066c38
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
7 changed files with 93 additions and 32 deletions

View File

@ -1,5 +1,6 @@
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
FROM python:3 FROM python:3
ARG OPERATION
RUN useradd -d /code xf RUN useradd -d /code xf
RUN mkdir -p /code RUN mkdir -p /code
@ -15,9 +16,13 @@ USER xf
ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1
WORKDIR /code WORKDIR /code
COPY requirements.prod.txt /code/ COPY requirements.txt /code/
RUN python -m venv /venv RUN python -m venv /venv
RUN . /venv/bin/activate && pip install -r requirements.prod.txt RUN . /venv/bin/activate && pip install -r requirements.txt
# CMD . /venv/bin/activate && uwsgi --ini /conf/uwsgi.ini # CMD . /venv/bin/activate && uwsgi --ini /conf/uwsgi.ini
CMD . /venv/bin/activate && uvicorn --reload --reload-include *.html --workers 2 --uds /var/run/socks/app.sock app.asgi:application
CMD if [ "$OPERATION" = "uwsgi" ] ; then . /venv/bin/activate && uwsgi --ini /conf/uwsgi.ini ; else . /venv/bin/activate && exec python manage.py runserver 0.0.0.0:8000; fi
# CMD . /venv/bin/activate && uvicorn --reload --reload-include *.html --workers 2 --uds /var/run/socks/app.sock app.asgi:application
# CMD . /venv/bin/activate && gunicorn -b 0.0.0.0:8000 --reload app.asgi:application -k uvicorn.workers.UvicornWorker # CMD . /venv/bin/activate && gunicorn -b 0.0.0.0:8000 --reload app.asgi:application -k uvicorn.workers.UvicornWorker

View File

@ -1,26 +1,26 @@
run: run:
docker-compose -f docker/docker-compose.prod.yml --env-file=stack.env up -d docker-compose --env-file=stack.env up -d
build: build:
docker-compose -f docker/docker-compose.prod.yml --env-file=stack.env build docker-compose --env-file=stack.env build
stop: stop:
docker-compose -f docker/docker-compose.prod.yml --env-file=stack.env down docker-compose --env-file=stack.env down
log: log:
docker-compose -f docker/docker-compose.prod.yml --env-file=stack.env logs -f docker-compose --env-file=stack.env logs -f
test: test:
docker-compose -f docker/docker-compose.prod.yml --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py test $(MODULES)" docker-compose --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py test $(MODULES)"
migrate: migrate:
docker-compose -f docker/docker-compose.prod.yml --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py migrate" docker-compose --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py migrate"
makemigrations: makemigrations:
docker-compose -f docker/docker-compose.prod.yml --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py makemigrations" docker-compose --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py makemigrations"
auth: auth:
docker-compose -f docker/docker-compose.prod.yml --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py createsuperuser" docker-compose --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py createsuperuser"
token: token:
docker-compose -f docker/docker-compose.prod.yml --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py addstatictoken m" docker-compose --env-file=stack.env run --rm app sh -c ". /venv/bin/activate && python manage.py addstatictoken m"

View File

@ -4,15 +4,19 @@ services:
app: app:
image: xf/fisk:prod image: xf/fisk:prod
container_name: fisk container_name: fisk
build: ${PORTAINER_GIT_DIR}/docker/prod build:
context: .
args:
OPERATION: ${OPERATION}
volumes: volumes:
- ${PORTAINER_GIT_DIR}:/code - ${PORTAINER_GIT_DIR}:/code
- ${PORTAINER_GIT_DIR}/docker/uwsgi.ini:/conf/uwsgi.ini
- ${APP_DATABASE_FILE}:/conf/db.sqlite3 - ${APP_DATABASE_FILE}:/conf/db.sqlite3
- app_static:${STATIC_ROOT} - fisk_static:${STATIC_ROOT}
#ports: #ports:
# - "8000:8000" # uwsgi socket # - "8000:8000" # uwsgi socket
env_file: env_file:
- ../stack.env - stack.env
volumes_from: volumes_from:
- tmp - tmp
depends_on: depends_on:
@ -22,30 +26,39 @@ services:
condition: service_started condition: service_started
collectstatic: collectstatic:
condition: service_started condition: service_started
networks:
- default
- xf
migration: migration:
image: xf/fisk:prod image: xf/fisk:prod
container_name: migration_fisk container_name: migration_fisk
build: ./docker/prod build:
context: .
args:
OPERATION: ${OPERATION}
command: sh -c '. /venv/bin/activate && python manage.py migrate --noinput' command: sh -c '. /venv/bin/activate && python manage.py migrate --noinput'
volumes: volumes:
- ${PORTAINER_GIT_DIR}:/code - ${PORTAINER_GIT_DIR}:/code
- ${APP_DATABASE_FILE}:/conf/db.sqlite3 - ${APP_DATABASE_FILE}:/conf/db.sqlite3
- app_static:${STATIC_ROOT} - fisk_static:${STATIC_ROOT}
env_file: env_file:
- ../stack.env - stack.env
collectstatic: collectstatic:
image: xf/fisk:prod image: xf/fisk:prod
container_name: collectstatic_fisk container_name: collectstatic_fisk
build: ./docker/prod build:
context: .
args:
OPERATION: ${OPERATION}
command: sh -c '. /venv/bin/activate && python manage.py collectstatic --noinput' command: sh -c '. /venv/bin/activate && python manage.py collectstatic --noinput'
volumes: volumes:
- ${PORTAINER_GIT_DIR}:/code - ${PORTAINER_GIT_DIR}:/code
- ${APP_DATABASE_FILE}:/conf/db.sqlite3 - ${APP_DATABASE_FILE}:/conf/db.sqlite3
- app_static:${STATIC_ROOT} - fisk_static:${STATIC_ROOT}
env_file: env_file:
- ../stack.env - stack.env
nginx: nginx:
image: nginx:latest image: nginx:latest
@ -59,10 +72,13 @@ services:
hard: 65535 hard: 65535
volumes: volumes:
- ${PORTAINER_GIT_DIR}:/code - ${PORTAINER_GIT_DIR}:/code
- ${PORTAINER_GIT_DIR}/docker/nginx/conf.d:/etc/nginx/conf.d - ${PORTAINER_GIT_DIR}/docker/nginx/conf.d/${OPERATION}.conf:/etc/nginx/conf.d/default.conf
- app_static:${STATIC_ROOT} - fisk_static:${STATIC_ROOT}
volumes_from: volumes_from:
- tmp - tmp
networks:
- default
- xf
depends_on: depends_on:
app: app:
condition: service_started condition: service_started
@ -102,8 +118,9 @@ services:
networks: networks:
default: default:
external: driver: bridge
name: xf xf:
external: true
volumes: volumes:
app_static: {} fisk_static: {}

View File

@ -1,6 +1,7 @@
upstream django { upstream django {
#server app:8000; #server app:8000;
server unix:///var/run/socks/app.sock; #server unix:///var/run/socks/app.sock;
server app:8000;
} }
server { server {

View File

@ -0,0 +1,24 @@
upstream django {
server app:8000;
#server unix:///var/run/socks/app.sock;
}
server {
listen 9999;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /conf;
}
location / {
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
uwsgi_pass django;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
}
}

13
docker/uwsgi.ini Normal file
View File

@ -0,0 +1,13 @@
[uwsgi]
chdir=/code
module=app.wsgi:application
env=DJANGO_SETTINGS_MODULE=app.settings
master=1
pidfile=/tmp/project-master.pid
socket=0.0.0.0:8000
processes=5
harakiri=20
max-requests=5000
vacuum=1
home=/venv

View File

@ -1,13 +1,14 @@
wheel wheel
uwsgi
django django
pre-commit pre-commit
django-crispy-forms==1.14.0 django-crispy-forms==1.14.0
crispy-bulma crispy-bulma
stripe stripe
django-rest-framework django-rest-framework
uvloop uvloop #
uvicorn[standard] uvicorn[standard] #
gunicorn gunicorn #
django-htmx django-htmx
cryptography cryptography
django-debug-toolbar django-debug-toolbar
@ -22,4 +23,4 @@ pydantic
alpaca-py alpaca-py
oandapyV20 oandapyV20
glom glom
watchfiles watchfiles #