Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Docker dev setup #13

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
# - SECRET_KEY=...
# - DATABASE_URL='postgres://<postgres-host>/<database-name>'
# - 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
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
Expand Down
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `[email protected]`.

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`.
Expand Down Expand Up @@ -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:

Expand All @@ -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:

Expand All @@ -109,12 +114,6 @@ Run dev server:

./manage.py runserver

## Running the tests

Install dependencies:

pip install -r requirements-dev.txt

Run tests:

pytest
Expand Down
30 changes: 15 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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"
28 changes: 28 additions & 0 deletions docker/entrypoint-dev.sh
Original file line number Diff line number Diff line change
@@ -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
Loading