From 5d7f0cb6972e78fbc162cac0289afdb33192d962 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Sun, 3 Nov 2024 00:04:20 +0100 Subject: [PATCH] Add Docker dev setup --- Dockerfile | 8 ++++---- README.md | 21 ++++++++++----------- docker-compose.yml | 30 +++++++++++++++--------------- docker/entrypoint-dev.sh | 28 ++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 docker/entrypoint-dev.sh diff --git a/Dockerfile b/Dockerfile index 587a770..b1add69 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,13 +5,13 @@ # - SECRET_KEY=... # - DATABASE_URL='postgres:///' # - ALLOWED_HOST='reservations.coredump.ch' -# -# See docker-compose.yml as an example on how to run this image. FROM docker.io/python:3.9-slim-bullseye +ARG REQUIREMENTS_FILE=requirements.txt + # Add requirements file -ADD requirements.txt /code/requirements.txt +ADD requirements*.txt /code/ WORKDIR /code # Install dependencies @@ -19,7 +19,7 @@ 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 +RUN pip install -r $REQUIREMENTS_FILE # Add code ADD . /code diff --git a/README.md b/README.md index 4696ef7..15357b9 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,17 @@ A small Python 3 / Django 3.2 LTS project to manage reservations for our 3D printer. - ## API Tokens You need an API token to be able to read or write from this API. Request one from `danilo@coredump.ch`. -The token should be included in the `Authorization` HTTP header. e key should +The token should be included in the `Authorization` HTTP header. The key should be prefixed by the string literal "Token", with whitespace separating the two strings. For example: Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b - ## API Endpoints All endpoints are below `/api/v1`. @@ -76,8 +74,15 @@ Output: If there are multiple pages, you will find the corresponding URLs in the `next` or `previous` response fields. +## Dev Setup: Docker Compose + +Run `docker-compose up` and enjoy the dev server at `http://localhost:8000/`. + +To run tests: + + docker compose exec web pytest -## Dev Setup +## Dev Setup: Manual Prerequisites: @@ -93,7 +98,7 @@ Dependencies: python3 -m venv venv source venv/bin/activate - pip install -r requirements.txt + pip install -r requirements-dev.txt Env vars: @@ -109,12 +114,6 @@ Run dev server: ./manage.py runserver -## Running the tests - -Install dependencies: - - pip install -r requirements-dev.txt - Run tests: pytest diff --git a/docker-compose.yml b/docker-compose.yml index e8468a2..2e71700 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,18 +1,11 @@ -# 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/static directory to a directory on the host -# and configure your web server to serve these files on /static/. - -version: "3.9" +# Note: This config is only for development and contains insecure defaults! volumes: reservation-api-data: services: db: - image: docker.io/postgres:14-alpine + image: docker.io/postgres:17-alpine volumes: - reservation-api-data:/var/lib/postgresql/data healthcheck: @@ -23,14 +16,21 @@ services: environment: - POSTGRES_DB=reservation-api - POSTGRES_USER=reservation-api - - POSTGRES_PASSWORD=TODO-CHANGEME + - POSTGRES_PASSWORD=insecure-dev-pg web: - build: . + depends_on: + - db + build: + context: . + args: + REQUIREMENTS_FILE: "requirements-dev.txt" ports: - "127.0.0.1:8000:8000" + volumes: + - ./:/code environment: - SECRET_KEY: TODO-CHANGEME - DATABASE_URL: postgres://reservation-api:TODO-CHANGEME@db/reservation-api + SECRET_KEY: "insecure-dev-secret" + DJANGO_DEBUG: "true" + DATABASE_URL: "postgres://reservation-api:insecure-dev-pg@db/reservation-api" ALLOWED_HOST: localhost - depends_on: - - db + command: "/bin/bash docker/entrypoint-dev.sh" diff --git a/docker/entrypoint-dev.sh b/docker/entrypoint-dev.sh new file mode 100644 index 0000000..71ca734 --- /dev/null +++ b/docker/entrypoint-dev.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# +# Entry point script for Docker during development +set -euo pipefail + +# 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 "Collect static files…" +./manage.py collectstatic --noinput + +echo "Start dev server…" +./manage.py runserver 0.0.0.0:8000 --force-color