Dockerizace! (zatím alespoň pro lokální vývoj) #37
Open
borek
wants to merge 6 commits from dockerizace
into master
6 changed files with 129 additions and 0 deletions
@ -0,0 +1,36 @@ |
|||||
|
FROM python:3.9.18-slim-bullseye |
||||
|
|
||||
|
# set work directory |
||||
|
WORKDIR /usr/mamweb-docker |
||||
|
|
||||
|
# set environment variables |
||||
|
ENV PIP_DISABLE_PIP_VERSION_CHECK 1 |
||||
|
ENV PYTHONDONTWRITEBYTECODE 1 |
||||
|
ENV PYTHONUNBUFFERED 1 |
||||
|
|
||||
|
# install dependencies |
||||
|
RUN apt-get update && apt-get install -y \ |
||||
|
libpq-dev \ |
||||
|
gcc \ |
||||
|
locales \ |
||||
|
imagemagick \ |
||||
|
netcat \ |
||||
|
postgresql-client |
||||
|
RUN pip install --upgrade pip |
||||
|
COPY ./requirements.txt . |
||||
|
COPY ./constraints.txt . |
||||
|
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 |
||||
|
ENV LANGUAGE en_US:en |
||||
|
ENV LC_ALL cs_CZ.UTF-8 |
||||
|
|
||||
|
# copy project |
||||
|
COPY . . |
||||
|
|
||||
|
# create test data once db is ready |
||||
|
RUN chmod +x /usr/mamweb-docker/docker_entrypoint.sh |
||||
|
ENTRYPOINT ["/usr/mamweb-docker/docker_entrypoint.sh"] |
@ -0,0 +1,23 @@ |
|||||
|
|
||||
|
services: |
||||
|
web: |
||||
|
build: . |
||||
|
command: python manage.py runserver 0.0.0.0:8000 |
||||
|
volumes: |
||||
|
- .:/usr/mamweb-docker |
||||
|
ports: |
||||
|
- 8000:8000 |
||||
|
depends_on: |
||||
|
- db |
||||
|
|
||||
|
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: |
@ -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 "$@" |
@ -0,0 +1,15 @@ |
|||||
|
Lokální vývoj pomocí dockeru |
||||
|
============================ |
||||
|
|
||||
|
Přijde vám standardní zprovoznění painful? Docker comes to the rescue. Stačí mít nainstalovaný ``docker`` a spustit ``docker compose up``. |
||||
|
|
||||
|
Co se děje under the hood |
||||
|
------------------------- |
||||
|
|
||||
|
- ``docker-compose.yml`` specifikuje, že chceme kontejner pro web, který je závislý na kontejneru s PostgreSQL db |
||||
|
- ``web`` se buildí z ``Dockerfilu``, mountuje si kořen repa jako svůj volume (takže vidí změny), na konci spouští webserver a vysatvuje ho na port 8000 localhosta |
||||
|
- ``db`` je z ``postgres:13-bullseye`` a má nastavené nějaké parametry, svoje data ukládá jako docker volume |
||||
|
- ``Dockerfile`` staví ``web`` na pythonu3.9 a debianu bullseye (mělo by odpovídat gimlimu) - nainstaluje dependencies, nastaví locale a entrypoint (co se má vykonat při spouštění kontejneru) |
||||
|
- ``docker_entrypoint.sh`` počká na Postgres ready v ``db``, podívá se jestli jsou v něm testdata a když ne, tak je vygeneruje, pak spustí command z compose (i.e. webserver) |
||||
|
- ``mamweb/settings.py`` vybere django settings podle cesty (jako doteď), vybere ``mamweb/settings_docker.py``, které importuje všechno z ``mamweb/settings_local.py``, přepíše jen ``DATABASES`` a ``SECRET_KEY``, aby se jako DB Engine používal Postgres ve vedlejším kontejneru, aby se celý web choval stejně jako ``_test`` a ``_prod`` (``_local`` aktuálně používá DB Engine sqlite3, které se v něčem chová trochu rozdílně) |
||||
|
- kontejner ``web`` se musí nějak připojovat k postgresu v ``db``, k tomu slouží user ``mam-web``, náhodně vygenerované heslo (v ``docker-compose.yml`` a ``docker_entrypoint.sh``, musí se shodovat), db se jmenuje ``mam_docker`` |
@ -0,0 +1,33 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
# |
||||
|
# 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 |
||||
|
|
||||
|
from mamweb.settings_local import * # Import all the settings for local development |
||||
|
|
||||
|
# SECURITY WARNING: keep the secret key used in production secret! |
||||
|
SECRET_KEY = 'EZfSzeuDCycKr5ZjiCQ^45ZqFU@8Ke#YDwn9ThqerfEpu^yV#p' |
||||
|
|
||||
|
# 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", |
||||
|
}, |
||||
|
} |
Loading…
Reference in new issue