Browse Source

Docker přesvědčen, aby používal Postgres

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.
dockerizace
Borek Požár 1 year ago
parent
commit
1937f3a4c9
  1. 20
      Dockerfile
  2. 28
      docker-compose.yml
  3. 19
      docker_entrypoint.sh
  4. 3
      mamweb/settings.py
  5. 117
      mamweb/settings_docker.py

20
Dockerfile

@ -1,7 +1,7 @@
FROM python:3.9.18-slim-bullseye FROM python:3.9.18-slim-bullseye
# set work directory # set work directory
WORKDIR /usr/src WORKDIR /usr/mamweb-docker
# set environment variables # set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1 ENV PIP_DISABLE_PIP_VERSION_CHECK 1
@ -13,7 +13,9 @@ RUN apt-get update && apt-get install -y \
libpq-dev \ libpq-dev \
gcc \ gcc \
locales \ locales \
imagemagick imagemagick \
netcat \
postgresql-client
RUN pip install --upgrade pip RUN pip install --upgrade pip
COPY ./requirements.txt . COPY ./requirements.txt .
COPY ./constraints.txt . COPY ./constraints.txt .
@ -22,15 +24,13 @@ RUN pip install -r requirements.txt
# allow correct locales # allow correct locales
RUN sed -i '/cs_CZ.UTF-8/s/^# //g' /etc/locale.gen && \ RUN sed -i '/cs_CZ.UTF-8/s/^# //g' /etc/locale.gen && \
locale-gen locale-gen
ENV LANG cs_CZ.UTF-8 \ ENV LANG cs_CZ.UTF-8
LANGUAGE en_US:en \ ENV LANGUAGE en_US:en
LC_ALL cs_CZ.UTF-8 ENV LC_ALL cs_CZ.UTF-8
# copy project # copy project
COPY . . COPY . .
# create test data # create test data once db is ready
RUN ./manage.py testdata RUN chmod +x /usr/mamweb-docker/docker_entrypoint.sh
RUN ./manage.py loaddata data/* ENTRYPOINT ["/usr/mamweb-docker/docker_entrypoint.sh"]
# make/sync_prod_flatpages && \
# ./manage.py load_org_permissions deploy_v2/admin_org_prava.json

28
docker-compose.yml

@ -1,25 +1,23 @@
# TODO:
# - [ ] Pořídit vhodný django settings.py pro docker, aby to používalo postgress
services: services:
web: web:
build: . build: .
command: python manage.py runserver 0.0.0.0:8000 command: python manage.py runserver 0.0.0.0:8000
volumes: volumes:
- .:/usr/src - .:/usr/mamweb-docker
ports: ports:
- 8000:8000 - 8000:8000
# env_file: depends_on:
# - ./.env.dev - db
# db: db:
# image: postgres:13-bullseye image: postgres:13-bullseye
# volumes: volumes:
# - postgres_data:/var/lib/postgresql/data/ - postgres_data:/var/lib/postgresql/data/
# environment: environment:
# - POSTGRES_USER=hello_django - POSTGRES_USER=mam-web
# - POSTGRES_PASSWORD=hello_django - POSTGRES_PASSWORD=RoEGG5g7&b # Random generated string corresponding with Django settings
# - POSTGRES_DB=hello_django_dev - POSTGRES_DB=mam_docker
# volumes: volumes:
# postgres_data: postgres_data:

19
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 "$@"

3
mamweb/settings.py

@ -14,6 +14,9 @@ if "mamweb-test" in os.path.abspath(__file__):
elif "mamweb-prod" in os.path.abspath(__file__): elif "mamweb-prod" in os.path.abspath(__file__):
from .settings_prod import * from .settings_prod import *
elif "mamweb-docker" in os.path.abspath(__file__):
from .settings_docker import *
else: else:
from .settings_local import * from .settings_local import *

117
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"
Loading…
Cancel
Save