From 5ca7e7c6634e1a76d28b8519726969c62c0df6eb Mon Sep 17 00:00:00 2001 From: Tom Irgang Date: Mon, 17 Apr 2023 20:57:55 +0200 Subject: [PATCH] Docker image support for repapp --- .gitignore | 7 +++++++ Dockerfile | 28 ++++++++++++++++++++++++++++ README.md | 19 +++++++++++++++++++ nginx.default | 13 +++++++++++++ rc_hip/rc_hip/settings.py | 21 +++++++++++++++++---- rc_hip/rc_hip/urls.py | 2 +- requirements_dev.txt | 6 ++++++ requirements_prod.txt | 2 ++ start_server.sh | 14 ++++++++++++++ 9 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 Dockerfile create mode 100644 nginx.default create mode 100644 requirements_dev.txt create mode 100644 requirements_prod.txt create mode 100644 start_server.sh diff --git a/.gitignore b/.gitignore index c9d82bb..f2d17b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,10 @@ + +rc_hip/static +rc_hip/static/** + +rc_hip/data +rc_hip/data/** + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f88897f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM python:3-bullseye + +# install nginx +RUN apt-get update && apt-get install nginx vim -y --no-install-recommends +COPY nginx.default /etc/nginx/sites-available/default +RUN ln -sf /dev/stdout /var/log/nginx/access.log \ + && ln -sf /dev/stderr /var/log/nginx/error.log + +# copy source and install dependencies +RUN mkdir -p /opt/app +RUN mkdir -p /opt/app/pip_cache +RUN mkdir -p /opt/app/rc_hip + +COPY rc_hip /opt/app/rc_hip +RUN rm -f /opt/app/rc_hip/data/db.sqlite3 + +WORKDIR /opt/app + +COPY requirements_prod.txt /opt/app/ +RUN pip install -r requirements_prod.txt --cache-dir /opt/app/pip_cache + +COPY start_server.sh /opt/app/ +RUN chmod +x /opt/app/start_server.sh + +RUN chown -R www-data:www-data /opt/app + +# start server +CMD ["/opt/app/start_server.sh"] diff --git a/README.md b/README.md index a9bbbc1..7f5df64 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,21 @@ # RepApp + Web App um Reparaturtermine zu vereinbaren. + +For documentation see https://makes-hacks-hip.github.io/RepApp/. + +## Build Docker image + +- Build the image: `docker build -t repapp .` +- Run the image: + +```bash +docker run --rm -d \ + -p 127.0.0.1:8020:8020 \ + --name repapp \ + --env DJANGO_SUPERUSER_USERNAME=suba \ + --env DJANGO_SUPERUSER_PASSWORD=ThePassword \ + --env DJANGO_SUPERUSER_EMAIL=noreply@example.com \ + repapp` +``` +- Open `http://127.0.0.1:8020/` in your browser diff --git a/nginx.default b/nginx.default new file mode 100644 index 0000000..1bd3943 --- /dev/null +++ b/nginx.default @@ -0,0 +1,13 @@ +server { + listen 8020; + server_name example.org; + + location / { + proxy_pass http://127.0.0.1:8010; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + location /static { + root /opt/app/rc_hip; + } +} diff --git a/rc_hip/rc_hip/settings.py b/rc_hip/rc_hip/settings.py index dcc181e..93c2f90 100644 --- a/rc_hip/rc_hip/settings.py +++ b/rc_hip/rc_hip/settings.py @@ -9,21 +9,28 @@ https://docs.djangoproject.com/en/4.2/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.2/ref/settings/ """ - +import mimetypes +import os from pathlib import Path +mimetypes.add_type("text/css", ".css", True) + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +STATIC_ROOT = os.path.join(BASE_DIR, "static") +MEDIA_ROOT = os.path.join(BASE_DIR, "media") # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-ura&_-(r(&&b$0s!a=07c@k6c!8=8m7$5(4#w8m@79-e%rn6s*' +# SECRET_KEY = 'django-insecure-ura&_-(r(&&b$0s!a=07c@k6c!8=8m7$5(4#w8m@79-e%rn6s*' +SECRET_KEY = os.environ.get( + "DJANGO_SECRET_KEY", 'django-insecure-ura&_-(r(&&b$0s!a=07c@k6c!8=8m7$5(4#w8m@79-e%rn6s*') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.getenv("DJANGO_DEBUG", 'true').lower() in ('true', '1', 't') ALLOWED_HOSTS = [] @@ -77,7 +84,7 @@ WSGI_APPLICATION = 'rc_hip.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', + 'NAME': BASE_DIR / 'data' / 'db.sqlite3', } } @@ -122,3 +129,9 @@ STATIC_URL = 'static/' # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + + +CSRF_TRUSTED_ORIGINS = [ + 'https://repapp.rc-hip.de', + 'http://127.0.0.1:8020' +] diff --git a/rc_hip/rc_hip/urls.py b/rc_hip/rc_hip/urls.py index 17d7371..52331bf 100644 --- a/rc_hip/rc_hip/urls.py +++ b/rc_hip/rc_hip/urls.py @@ -18,6 +18,6 @@ from django.contrib import admin from django.urls import path, include urlpatterns = [ - path("repapp/", include("repapp.urls")), + path("", include("repapp.urls")), path('admin/', admin.site.urls), ] diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..2541791 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,6 @@ +autopep8==2.0.2 +Django==4.2 +pep8==1.7.1 +pylint==2.17.2 +pylint-django==2.5.3 +pylint-plugin-utils==0.7 diff --git a/requirements_prod.txt b/requirements_prod.txt new file mode 100644 index 0000000..04a6796 --- /dev/null +++ b/requirements_prod.txt @@ -0,0 +1,2 @@ +Django==4.2 +gunicorn==20.1.0 diff --git a/start_server.sh b/start_server.sh new file mode 100644 index 0000000..214f0a5 --- /dev/null +++ b/start_server.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +cd /opt/app/rc_hip + +python manage.py makemigrations --noinput +python manage.py migrate --noinput +python manage.py collectstatic --noinput + +if [ -n "$DJANGO_SUPERUSER_USERNAME" ] && [ -n "$DJANGO_SUPERUSER_PASSWORD" ] ; then + (python manage.py createsuperuser --no-input) +fi + +chown -R www-data:www-data /opt/app + +(gunicorn rc_hip.wsgi --user www-data --bind 0.0.0.0:8010 --workers 3) & nginx -g "daemon off;"