Skip to content

Commit

Permalink
Add Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrgn committed Nov 15, 2021
1 parent 492dca9 commit 5270ecc
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Dockerfile
docker-compose.yml
venv/
VENV/
__pycache__/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
__pycache__
.env
interna/media/
venv/
VENV/
.mypy_cache/
40 changes: 40 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Docker image for Interna.
#
# Please set the following env vars:
#
# - SECRET_KEY=...
# - DATABASE_URL='postgres://<postgres-host>/<database-name>'
# - SITE_DOMAIN='https://<domain>[:<port>]'
# - ALLOWED_HOST='<domain>'
# - SMTP_HOST=...
# - SMTP_PORT=...
# - SMTP_USER=...
# - SMTP_PASS=...
#
# See docker-compose.yml as an example on how to run this image.

FROM docker.io/python:3.9-slim-bullseye

# Add requirements file
ADD requirements.txt /code/requirements.txt
WORKDIR /code

# Install dependencies
RUN apt-get update -qq \
&& apt-get install -yq --no-install-recommends \
dumb-init \
&& rm -rf /var/lib/apt/lists/*
RUN pip install -r requirements.txt

# Add code
ADD . /code

# Set env vars
ENV DJANGO_DEBUG=False

# Collect static files
RUN SECRET_KEY=tmp interna/manage.py collectstatic

# Entry point
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["/bin/bash", "entrypoint.sh"]
41 changes: 41 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Note: This config is not currently set up for development,
# it's meant mostly as an example for a productive deployment.
#
# This setup currently does not serve static files. In order to serve static
# files, mount the /code/interna/static directory to a directory on the host
# and configure your web server to serve these files on /static/.

version: "3.9"

volumes:
interna-data:

services:
db:
image: docker.io/postgres:14-alpine
volumes:
- interna-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U interna"]
interval: 30s
timeout: 30s
retries: 3
environment:
- POSTGRES_DB=interna
- POSTGRES_USER=interna
- POSTGRES_PASSWORD=TODO-CHANGEME
web:
build: .
ports:
- "8000:8000"
environment:
SECRET_KEY: TODO-CHANGEME
DATABASE_URL: postgres://interna:TODO-CHANGEME@db/interna
SITE_DOMAIN: http://localhost:8000
ALLOWED_HOST: localhost
SMTP_HOST: TODO-CHANGEME
SMTP_PORT: 1234
SMTP_USER: TODO-CHANGEME
SMTP_PASS: TODO-CHANGEME
depends_on:
- db
48 changes: 48 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
#
# Entry point script for Docker
set -euo pipefail

if [ -z "${SECRET_KEY:-}" ]; then
echo 'Error: Missing SECRET_KEY env variable.'
echo 'You can generate a secret key with `openssl rand -hex 32`.'
exit 1
fi
if [ -z "${DATABASE_URL:-}" ]; then
echo 'Error: Missing DATABASE_URL env variable.'
exit 1
fi
if [ -z "${SITE_DOMAIN:-}" ]; then
echo 'Error: Missing SITE_DOMAIN env variable.'
exit 1
fi
if [ -z "${SMTP_HOST:-}" ] || [ -z "${SMTP_USER:-}" ] || [ -z "${SMTP_PASS:-}" ] || [ -z "${SMTP_PORT:-}" ]; then
echo 'Error: Missing SMTP_* env variables.'
exit 1
fi

cd interna

# We need to wait for the database to be ready until we can run migrations.
# To do this, retry up to 15 times to run the migrations. Once that worked,
# we can assume that the database is up and ready.
success=0
retries=15
for i in $(seq $retries); do
echo "Running migrations…"
./manage.py migrate && success=1 && break || true
echo "Database not yet ready, waiting ($i/$retries)…"
sleep 1
done
if [ "$success" -eq 1 ]; then
echo "Database is up, all migrations applied"
else
echo "Timed out waiting for database"
exit 1
fi

echo "Create cache table…"
./manage.py createcachetable

echo "Start server…"
gunicorn config.wsgi:application -n interna -b 0.0.0.0:8000 -w 4 --log-level warning

0 comments on commit 5270ecc

Please sign in to comment.