From 1937f3a4c995fda982e1942f3a9f48b3f6e78ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borek=20Po=C5=BE=C3=A1r?= Date: Mon, 2 Oct 2023 13:27:39 +0200 Subject: [PATCH] =?UTF-8?q?Docker=20p=C5=99esv=C4=9Bd=C4=8Den,=20aby=20pou?= =?UTF-8?q?=C5=BE=C3=ADval=20Postgres?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vyrobeno nové `mamweb/settings_docker.py` jako kříženec mezi `_local` a `_test`. Úměrně tomu upraveno `mamweb/settings.py`, aby ho používalo při volání z kontejneru.`docker_entrypoint.sh` počká na start konejneru s Postgresem, zkontroluje existenci testdat (případně je vygeneruje) a spustí command specifikovaný v compose, tedy spustí webserver. --- Dockerfile | 20 +++---- docker-compose.yml | 28 +++++---- docker_entrypoint.sh | 19 +++++++ mamweb/settings.py | 3 + mamweb/settings_docker.py | 117 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 25 deletions(-) create mode 100755 docker_entrypoint.sh create mode 100644 mamweb/settings_docker.py diff --git a/Dockerfile b/Dockerfile index c385c34f..b288c765 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM python:3.9.18-slim-bullseye # set work directory -WORKDIR /usr/src +WORKDIR /usr/mamweb-docker # set environment variables ENV PIP_DISABLE_PIP_VERSION_CHECK 1 @@ -13,7 +13,9 @@ RUN apt-get update && apt-get install -y \ libpq-dev \ gcc \ locales \ - imagemagick + imagemagick \ + netcat \ + postgresql-client RUN pip install --upgrade pip COPY ./requirements.txt . COPY ./constraints.txt . @@ -22,15 +24,13 @@ RUN pip install -r requirements.txt # allow correct locales RUN sed -i '/cs_CZ.UTF-8/s/^# //g' /etc/locale.gen && \ locale-gen -ENV LANG cs_CZ.UTF-8 \ - LANGUAGE en_US:en \ - LC_ALL cs_CZ.UTF-8 +ENV LANG cs_CZ.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL cs_CZ.UTF-8 # copy project COPY . . -# create test data -RUN ./manage.py testdata -RUN ./manage.py loaddata data/* - # make/sync_prod_flatpages && \ - # ./manage.py load_org_permissions deploy_v2/admin_org_prava.json \ No newline at end of file +# create test data once db is ready +RUN chmod +x /usr/mamweb-docker/docker_entrypoint.sh +ENTRYPOINT ["/usr/mamweb-docker/docker_entrypoint.sh"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 72851cd8..f916a735 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,25 +1,23 @@ -# TODO: -# - [ ] Pořídit vhodný django settings.py pro docker, aby to používalo postgress services: web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - - .:/usr/src + - .:/usr/mamweb-docker ports: - 8000:8000 - # env_file: - # - ./.env.dev + depends_on: + - db -# db: -# image: postgres:13-bullseye -# volumes: -# - postgres_data:/var/lib/postgresql/data/ -# environment: -# - POSTGRES_USER=hello_django -# - POSTGRES_PASSWORD=hello_django -# - POSTGRES_DB=hello_django_dev + db: + image: postgres:13-bullseye + volumes: + - postgres_data:/var/lib/postgresql/data/ + environment: + - POSTGRES_USER=mam-web + - POSTGRES_PASSWORD=RoEGG5g7&b # Random generated string corresponding with Django settings + - POSTGRES_DB=mam_docker -# volumes: -# postgres_data: \ No newline at end of file +volumes: + postgres_data: \ No newline at end of file diff --git a/docker_entrypoint.sh b/docker_entrypoint.sh new file mode 100755 index 00000000..eb5842a3 --- /dev/null +++ b/docker_entrypoint.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +echo "Waiting for Postgres..." + +while ! nc -z "db" "5432"; do + sleep 0.1 +done + +echo "PostgreSQL started" + +PGPASSWORD="RoEGG5g7&b" # Random generated, corresponds to the one in docker-compose.yml +if psql "postgresql://mam-web:$PGPASSWORD@db:5432/mam_docker" -t -c '\dt' | cut -d \| -f 2 | grep -qw "seminar_cisla"; then + echo "\nExistuje tabulka 'seminar_cisla' v db, testdata pravděpodobně byla vygenerována.\n" +else + python ./manage.py testdata + python ./manage.py loaddata data/* +fi + +exec "$@" diff --git a/mamweb/settings.py b/mamweb/settings.py index 6f5d302f..98f70f91 100644 --- a/mamweb/settings.py +++ b/mamweb/settings.py @@ -14,6 +14,9 @@ if "mamweb-test" in os.path.abspath(__file__): elif "mamweb-prod" in os.path.abspath(__file__): from .settings_prod import * +elif "mamweb-docker" in os.path.abspath(__file__): + from .settings_docker import * + else: from .settings_local import * diff --git a/mamweb/settings_docker.py b/mamweb/settings_docker.py new file mode 100644 index 00000000..913135d5 --- /dev/null +++ b/mamweb/settings_docker.py @@ -0,0 +1,117 @@ +# -*- coding: utf-8 -*- + +import os.path + +# +# Docker nastaveni settings.py +# +# Pro vyber tohoto nastaveni muzete pouzit tez: +# DJANGO_SETTINGS_MODULE=mamweb.settings_docker ./manage.py ... +# + +# Import common settings +from .settings_common import * # zatim nutne, casem snad vyresime # noqa + +MIDDLEWARE += ( + 'debug_toolbar.middleware.DebugToolbarMiddleware', + ) + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ + +INSTALLED_APPS += ( + 'debug_toolbar', + 'django_extensions', + ) + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'EZfSzeuDCycKr5ZjiCQ^45ZqFU@8Ke#YDwn9ThqerfEpu^yV#p' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True +INTERNAL_IPS = ['127.0.0.1'] + +TEMPLATES[0]['OPTIONS']['debug'] = True + +from ipaddress import ip_network +ALLOWED_HOSTS = [str(ip) for ip in ip_network('192.168.0.0/16')] +ALLOWED_HOSTS.append('127.0.0.1') +ALLOWED_HOSTS.append('localhost') + +# Database +# https://docs.djangoproject.com/en/1.7/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'mam_docker', + 'USER': 'mam-web', + 'PASSWORD': 'RoEGG5g7&b', # Random generated string corresponding with docker-compose + 'TEST': { + 'NAME': 'mam-docker-testdb', + }, + "HOST": "db", + "PORT": "5432", + }, +} + +# import os +# SERVER_EMAIL = 'mamweb-test-errors@mam.mff.cuni.cz' +# ADMINS = [ +# ('M&M ERRORs', 'mam-errors@mam.mff.cuni.cz'), +# ] + +# SECURITY: only send sensitive cookies via HTTPS +# SESSION_COOKIE_SECURE = True +# CSRF_COOKIE_SECURE = True + +# LOGGING +LOGGING = { + 'version': 1, + 'disable_existing_loggers': True, + 'filters': { + 'require_debug_false': { + '()': 'django.utils.log.RequireDebugFalse' + } + }, + 'formatters': { + 'simple': { + 'format': '%(asctime)s - %(name)s - %(levelname)-8s - %(message)s', + }, + }, + 'handlers': { + 'dummy': { + 'class': 'logging.NullHandler', + }, + 'console': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'simple', + }, + }, + 'loggers': { + # Vypisovani databazovych dotazu do konzole + #'django.db.backends': { + # 'level': 'DEBUG', + # 'handlers': ['console'], + # 'propagate': False, + #}, + 'werkzeug': { + 'handlers': ['console'], + 'level': 'DEBUG', + 'propagate': True, + }, + '': { + 'handlers': ['console'], + 'level': 'DEBUG', + 'propagate': False, + }, + }, +} + +# FILE_UPLOAD_PERMISSIONS = 0o440 + +# Testování e-mailů +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' +SEND_EMAIL_NOTIFICATIONS = True +LOCAL_TEST_PROD = "local"