From 0e83ec2edfcc6dd030a38bcc6bd0f37b88db8829 Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Sat, 8 Aug 2020 23:23:11 +0200 Subject: [PATCH] Add Dockerfile and misc files to build docker image --- Dockerfile | 28 +++++++++ docker/entrypoint.sh | 15 +++++ docker/nginx.conf | 40 +++++++++++++ docker/settings.docker.py | 117 ++++++++++++++++++++++++++++++++++++++ docker/supervisord.conf | 18 ++++++ 5 files changed, 218 insertions(+) create mode 100644 Dockerfile create mode 100755 docker/entrypoint.sh create mode 100644 docker/nginx.conf create mode 100644 docker/settings.docker.py create mode 100644 docker/supervisord.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fac5e57 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM python:3.8 + +ENV GITLAB_HOST=git.zom.bi +ENV GITEA_HOST=gitea.zom.bi + +WORKDIR /usr/src/app + +RUN useradd migrate + +RUN mkdir -p /var/data/db && chown -R migrate /var/data + +RUN apt-get update && apt-get install --yes --no-install-recommends nginx supervisor +RUN pip install gunicorn + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY . /usr/src/app + +RUN mkdir ./logs && chown -R migrate ./logs && rm -f giteamigrate/settings.py && ln -s ../docker/settings.docker.py giteamigrate/settings.py + +USER migrate + +EXPOSE 8000 + +VOLUME ["/var/data"] + +ENTRYPOINT ["./docker/entrypoint.sh"] diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 0000000..bfdf9e8 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +cd /usr/src/app + +if ! python manage.py migrate ; then + echo "Unable to migrate" + [[ "$#" -eq 0 ]] && exit 1 +fi + + +if [[ "$#" -gt 0 ]]; then + exec "$@" +else + exec supervisord -c /usr/src/app/docker/supervisord.conf +fi diff --git a/docker/nginx.conf b/docker/nginx.conf new file mode 100644 index 0000000..88fff34 --- /dev/null +++ b/docker/nginx.conf @@ -0,0 +1,40 @@ +error_log /usr/src/app/logs/nginx.error.log warn; +pid /tmp/nginx.pid; +daemon off; +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + upstream appserver { + server localhost:8001; + } + + server { + listen 8000; + + access_log /usr/src/app/logs/nginx.access.log combined; + + client_body_temp_path /tmp/client_body; + fastcgi_temp_path /tmp/fastcgi; + proxy_temp_path /tmp/proxy; + scgi_temp_path /tmp/scgi; + uwsgi_temp_path /tmp/uwsgi; + + location / { + proxy_pass http://appserver; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + } + + location /static/ { + root /usr/src/app; + } + } +} diff --git a/docker/settings.docker.py b/docker/settings.docker.py new file mode 100644 index 0000000..56bc160 --- /dev/null +++ b/docker/settings.docker.py @@ -0,0 +1,117 @@ +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +GITLAB_HOST = os.getenv("GITLAB_HOST", "localhost") +GITEA_HOST = os.getenv("GITEA_HOST", "localhost") + +GITLAB_API = 'https://' + GITLAB_HOST + '/api/v4' +GITEA_API = 'https://' + GITEA_HOST + '/api/v1' +GITLAB_REPO_URL = 'https://%s:%s@' + GITLAB_HOST + '/%s.git' +GITEA_REPO_URL = 'https://%s:%s@' + GITEA_HOST + '/%s.git' + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = os.getenv("DJANGO_SECRET_KEY",None) + +DEBUG = False + +ALLOWED_HOSTS = [ + '*' +] + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'migrator', + 'workers', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'giteamigrate.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'giteamigrate.wsgi.application' + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': '/var/data/db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.0/howto/static-files/ + +STATIC_URL = '/static/' + +STATICFILES_DIRS = [ + os.path.join(BASE_DIR, "static") +] diff --git a/docker/supervisord.conf b/docker/supervisord.conf new file mode 100644 index 0000000..06703a4 --- /dev/null +++ b/docker/supervisord.conf @@ -0,0 +1,18 @@ +[supervisord] +nodaemon=true +logfile=/usr/src/app/logs/supervisord.log + +[program:nginx] +command=nginx -c /usr/src/app/docker/nginx.conf -p /usr/src/app + +[program:app_server] +directory=/usr/src/app +command=gunicorn -b 127.0.0.1:8001 giteamigrate.wsgi + +[program:background_worker] +directory=/usr/src/app +command=python manage.py runworkers +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 \ No newline at end of file