Skip to content

Commit e750a3a

Browse files
authored
Merge pull request #13 from EventAccess/dockerfile
Add dockerfile and related settings
2 parents c7023bb + ea130f5 commit e750a3a

6 files changed

Lines changed: 90 additions & 6 deletions

File tree

Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
FROM python:3.12.7 AS base
2+
3+
WORKDIR /app
4+
5+
ENV PYTHONDONTWRITEBYTECODE 1
6+
ENV PYTHONUNBUFFERED 1
7+
8+
RUN apt-get update \
9+
&& apt-get install -y \
10+
git \
11+
netcat-openbsd \
12+
&& rm -rf /var/lib/apt/lists/*
13+
14+
COPY ./requirements.txt .
15+
COPY ./requirements-dev.txt .
16+
RUN pip install -r requirements-dev.txt
17+
18+
19+
FROM base AS collectstatic
20+
COPY . .
21+
RUN DJANGO_STATIC_ROOT=/static python manage.py collectstatic --no-input
22+
RUN find /static -ls
23+
24+
FROM ghcr.io/static-web-server/static-web-server:2.33.1 AS static-server
25+
WORKDIR /srv/http
26+
ENV SERVER_ROOT=/srv/http
27+
28+
COPY --from=collectstatic /static /srv/http/static
29+
30+
31+
FROM base AS dev
32+
ENV RUN_MODE=dev
33+
COPY ./entrypoint.sh .
34+
ENTRYPOINT ["/app/entrypoint.sh"]
35+
36+
37+
FROM dev AS server
38+
ENV RUN_MODE=prod
39+
40+
COPY . .

Dockerfile.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.env
2+
/docker-compose.yaml
3+
/compose.yaml
4+
/Dockerfile
5+
/Dockerfile.dockerignore
6+
/*.sqlite3

config/settings.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
https://docs.djangoproject.com/en/5.0/ref/settings/
1111
"""
1212

13+
import os
1314
from pathlib import Path
1415

1516
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -20,17 +21,20 @@
2021
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
2122

2223
# SECURITY WARNING: keep the secret key used in production secret!
23-
SECRET_KEY = "django-insecure-w^mv=r_(x-se8p#@i*dxfu1^*8$fci+116it+fkj*nx!o2h*r1"
24-
24+
SECRET_KEY = os.getenv(
25+
"DJANGO_SECRET_KEY",
26+
"django-insecure-w^mv=r_(x-se8p#@i*dxfu1^*8$fci+116it+fkj*nx!o2h*r1",
27+
)
2528
# SECURITY WARNING: don't run with debug turned on in production!
26-
DEBUG = True
29+
DEBUG = os.getenv("DJANGO_DEBUG", "False").lower() == "true"
2730

28-
ALLOWED_HOSTS = ["*"]
31+
ALLOWED_HOSTS = os.getenv("DJANGO_HOSTS", "*").split(",")
2932

3033

3134
# Application definition
3235

3336
INSTALLED_APPS = [
37+
"daphne",
3438
"django.contrib.admin",
3539
"django.contrib.auth",
3640
"django.contrib.contenttypes",
@@ -71,15 +75,20 @@
7175
]
7276

7377
WSGI_APPLICATION = "config.wsgi.application"
78+
ASGI_APPLICATION = "config.asgi.application"
7479

7580

7681
# Database
7782
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
7883

7984
DATABASES = {
8085
"default": {
81-
"ENGINE": "django.db.backends.sqlite3",
82-
"NAME": BASE_DIR / "db.sqlite3",
86+
"ENGINE": os.getenv("DJANGO_DATABASE_ENGINE", "django.db.backends.sqlite3"),
87+
"HOST": os.getenv("DJANGO_DATABASE_HOST"),
88+
"PORT": os.getenv("DJANGO_DATABASE_PORT", 5432),
89+
"NAME": os.getenv("POSTGRES_DB", BASE_DIR / "db.sqlite3"),
90+
"USER": os.getenv("POSTGRES_USER"),
91+
"PASSWORD": os.getenv("POSTGRES_PASSWORD"),
8392
}
8493
}
8594

@@ -119,6 +128,7 @@
119128
# https://docs.djangoproject.com/en/5.0/howto/static-files/
120129

121130
STATIC_URL = "static/"
131+
STATIC_ROOT = os.getenv("DJANGO_STATIC_ROOT")
122132

123133
# Default primary key field type
124134
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

entrypoint.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
if [ "$DJANGO_DATABASE_ENGINE" = "django.db.backends.postgresql" ]; then
4+
echo "Waiting for postgres..."
5+
while ! nc -z $DJANGO_DATABASE_HOST "${DJANGO_DATABASE_PORT:-5432}"; do
6+
sleep 0.1
7+
done
8+
9+
echo "PostgreSQL is running chief!"
10+
fi
11+
12+
python manage.py migrate
13+
14+
python manage.py check --deploy
15+
16+
exec "$@"
17+

requirements-dev.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-r requirements.txt
2+
3+
black==24.10.0
4+
pytest==8.3.3
5+
mypy==1.13.0
6+
django-stubs[compatible-mypy]==5.1.1
7+
# TODO: Change djangorestframework-stubs to pypi release
8+
# Django rest framework stubs does not yet have a mypy 1.13 compatible release
9+
djangorestframework-stubs[compatible-mypy] @ git+https://github.com/typeddjango/djangorestframework-stubs.git@93b4ffe1441f43a646138ad302144a849bc6ecc0

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Django[all]==5.1.3
22
django-urlconfchecks==0.11.0 # TODO: Add to CI
3+
daphne==4.1.2
4+
psycopg2-binary==2.9.10

0 commit comments

Comments
 (0)