From 8a0a7558894afce8d25b7f0dc16775e899b72a94 Mon Sep 17 00:00:00 2001 From: Michael Manfre Date: Tue, 8 Nov 2022 23:05:51 -0500 Subject: [PATCH] Add basic docker support --- .dockerignore | 5 +++++ .gitignore | 1 + Dockerfile | 26 ++++++++++++++++++++++++++ docker-compose.yml | 42 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ scripts/start.sh | 7 +++++++ takahe/settings.py | 9 ++++++--- 7 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 scripts/start.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..dac9df9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +*.psql +*.sqlite3 +notes.md +.venv +.pre-commit-config.yaml diff --git a/.gitignore b/.gitignore index d6bf26a..3853bb5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.psql *.sqlite3 +.venv notes.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1f62240 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM python:3.9-bullseye as builder + +RUN mkdir -p /takahe +RUN python -m venv /takahe/.venv +RUN apt-get update && apt-get -y install libpq-dev python3-dev + +WORKDIR /takahe + +COPY requirements.txt requirements.txt + +RUN . /takahe/.venv/bin/activate \ + && pip install --upgrade pip \ + && pip install --upgrade -r requirements.txt + + +FROM python:3.9-slim-bullseye + +RUN apt-get update && apt-get install -y libpq5 + +COPY --from=builder /takahe /takahe +COPY . /takahe + +WORKDIR /takahe +EXPOSE 8000 + +CMD ["/takahe/scripts/start.sh"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f64bfb6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,42 @@ +version: "3" + +services: + db: + image: postgres:15-alpine + healthcheck: + test: ['CMD', 'pg_isready', '-U', 'postgres'] + volumes: + - dbdata:/var/lib/postgresql/data + networks: + - internal_network + restart: always + environment: + - "POSTGRES_DB=tahake" + - "POSTGRES_USER=postgres" + - "POSTGRES_PASSWORD=insecure_password" + + web: + build: . + image: tahake:latest + environment: + - "DJANGO_SETTINGS_MODULE=takahe.settings" + - "SECRET_KEY=insecure_secret" + - "POSTGRES_HOST=db" + - "POSTGRES_DB=tahake" + - "POSTGRES_USER=postgres" + - "POSTGRES_PASSWORD=insecure_password" + networks: + - external_network + - internal_network + restart: always + depends_on: + - db + ports: + - "8000:8000" + +networks: + internal_network: + external_network: + +volumes: + dbdata: diff --git a/requirements.txt b/requirements.txt index 09de1e6..53ab2b0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,6 @@ django-crispy-forms~=1.14 cryptography~=38.0 httpx~=0.23 pyOpenSSL~=22.1.0 +uvicorn~=0.19 +gunicorn~=20.1.0 +psycopg2==2.9.5 diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100644 index 0000000..99f1ed0 --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +. /takahe/.venv/bin/activate + +python manage.py migrate + +exec gunicorn takahe.asgi:application -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 diff --git a/takahe/settings.py b/takahe/settings.py index fea5244..62065d2 100644 --- a/takahe/settings.py +++ b/takahe/settings.py @@ -1,10 +1,11 @@ +import os from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = "insecure_secret" +SECRET_KEY = os.environ.get("SECRET_KEY", "insecure_secret") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -64,8 +65,10 @@ WSGI_APPLICATION = "takahe.wsgi.application" DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql_psycopg2", - "NAME": "takahe", - "USER": "postgres", + "HOST": os.environ.get("POSTGRES_HOST", "localhost"), + "NAME": os.environ.get("POSTGRES_DB", "takahe"), + "USER": os.environ.get("POSTGRES_USER", "postgres"), + "PASSWORD": os.environ.get("POSTGRES_PASSWORD"), } }