Skip to content

Commit

Permalink
refactor: working (new) docker compose config
Browse files Browse the repository at this point in the history
  • Loading branch information
anorthall committed Feb 7, 2025
1 parent 564dd7f commit 3253ea0
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 61 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.*
README.md
LICENCE
justfile
40 changes: 19 additions & 21 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# syntax=docker/dockerfile:1
FROM python:3.13-slim
COPY --from=ghcr.io/astral-sh/uv:0.5.29 /uv /uvx /bin/

ARG UV_VERSION=0.5.11
ENV UV_COMPILE_BYTECODE=1
ENV UV_FROZEN=1
ENV UV_LINK_MODE=copy
ENV UV_PROJECT_ENVIRONMENT=/app/.venv
ENV VIRTUAL_ENV=${UV_PROJECT_ENVIRONMENT}
ENV PYTHONPATH="/app/:/app/reportdb/"
ENV PATH="/app/.venv/bin:${PATH}"
ENV DJANGO_SETTINGS_MODULE=conf.settings

ENV PATH="/root/.local/bin:${PATH}"
ENV UV_PROJECT_ENVIRONMENT=/opt/uv/
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# Create directories
RUN mkdir -p /app /app/logs /app/src /opt/uv \
/app/staticfiles /app/mediafiles

ADD https://astral.sh/uv/${UV_VERSION}/install.sh /uv-installer.sh
RUN chmod +x /uv-installer.sh && /uv-installer.sh && rm /uv-installer.sh
RUN mkdir -p /app /opt/uv /app/staticfiles /app/mediafiles

WORKDIR /app

Expand All @@ -25,15 +23,15 @@ RUN apt-get update && apt-get install -y \
netcat-traditional \
tini

COPY ./pyproject.toml ./uv.lock /app/
RUN groupadd app && useradd -g app -d /app app && chown app -R /app
RUN uv sync --frozen && chown app:app -R /opt/uv

COPY etc/run.sh /app
RUN chmod +x /app/run.sh

COPY ./reportdb/ /app/
COPY uv.lock pyproject.toml /app/
RUN uv sync --frozen
COPY . /app/
RUN uv run /app/reportdb/manage.py collectstatic --noinput && \
groupadd app && useradd -g app -d /app app && \
chmod +x /app/conf/run.sh && \
chown app -R /app

USER app

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/app/run.sh", "start"]
CMD ["/app/conf/run.sh", "start"]
2 changes: 2 additions & 0 deletions conf/dev.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Development environment for Incident Database
#

DJANGO_SETTINGS_MODULE = "conf.settings"

DEBUG = 1
SECRET_KEY = "django-insecure-c3+z5mi8bc3ah4@_eb11ure9l(6nip1mfri!_vgbe0k-8_*1_l"

Expand Down
51 changes: 21 additions & 30 deletions conf/run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
CONF_ROOT="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
PROJECT_ROOT="${CONF_ROOT}/.."
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
PROJECT_ROOT="$SCRIPT_DIR/.."
APP_ROOT="${PROJECT_ROOT}/reportdb"

if [ -z "$VIRTUAL_ENV" ]
Expand Down Expand Up @@ -38,40 +38,31 @@ then
fi

cd "$PROJECT_ROOT" || exit 1
echo "Starting server..."

# shellcheck disable=SC2030
granian --interface wsgi \
--host 0.0.0.0 \
--port "${PORT:=8000}" \
--workers 1 \
--respawn-failed-workers \
--reload \
--reload-paths backend/src \
--no-ws \
--threading-mode workers \
conf.wsgi:application &
gunicorn \
--bind "0.0.0.0:$PORT" \
--workers 2 \
--reload \
--reload-engine inotify \
--name incidentdb-django \
conf.wsgi:application
fi

# Run production server
if [ "$1" = "start" ]
then
cd "$APP_ROOT" || exit 1
NUM_CORES=$(nproc)
NUM_WORKERS=$((NUM_CORES * 2 + 1))
echo "Detected $NUM_CORES, running with $NUM_WORKERS workers"

echo "Collecting static files..."
python manage.py collectstatic --no-input

cd "$PROJECT_ROOT" || exit 1
echo "Starting server..."
# shellcheck disable=SC2031
granian --interface wsgi \
--host 0.0.0.0 \
--port "${PORT:=8000}" \
--workers 4 \
--respawn-failed-workers \
--no-reload \
--no-ws \
--threading-mode workers \
--workers-lifetime 600 \
conf.wsgi:application &
gunicorn \
--bind "0.0.0.0:$PORT" \
--workers "$NUM_WORKERS" \
--preload \
--max-requests 1000 \
--max-requests-jitter 200 \
--keep-alive 10 \
--name incidentdb-django \
conf.wsgi:application
fi
7 changes: 3 additions & 4 deletions conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

django_stubs_ext.monkeypatch()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

DJANGO_ROOT = BASE_DIR / "reportdb"

SECRET_KEY = os.environ.get("SECRET_KEY", "insecure-secret-key")
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", "http://127.0.0.1").split()
Expand Down Expand Up @@ -58,7 +57,7 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": ["templates"],
"DIRS": [DJANGO_ROOT / "templates"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
Expand Down Expand Up @@ -115,7 +114,7 @@
# Static and media files (CSS, JavaScript, Images)
STATIC_URL = "/static/"
STATIC_ROOT = os.environ.get("STATIC_ROOT", "/app/staticfiles")
STATICFILES_DIRS = [BASE_DIR / "static"]
STATICFILES_DIRS = [DJANGO_ROOT / "static"]

AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_STORAGE_BUCKET_NAME")
AWS_S3_REGION_NAME = os.environ.get("AWS_S3_REGION_NAME")
Expand Down
13 changes: 7 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
services:
db:
image: postgres:17
volumes:
- postgres:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres

web:
build: .
command: /app/run.sh devserver
command: /app/conf/run.sh devserver
volumes:
- ./reportdb/:/app/src/
- media:/app/mediafiles/
- static:/app/staticfiles/
- ./reportdb/:/app/reportdb/
ports:
- "8000:8000"
environment:
- PORT=8000
env_file:
- conf/dev.env
depends_on:
- db

volumes:
media:
static:
postgres:
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies = [
"django-storages[s3]",
"gunicorn",
"humanize",
"inotify>=0.2.10",
"openai",
"orjson",
"Pillow",
Expand Down
20 changes: 20 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3253ea0

Please sign in to comment.