add minimal docker support for debugging

This commit is contained in:
pedro 2024-08-02 13:05:02 +02:00
parent 435e18c8b6
commit 260b7db765
4 changed files with 109 additions and 0 deletions

6
db/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
# src https://stackoverflow.com/questions/115983/how-do-i-add-an-empty-directory-to-a-git-repository
# Ignore everything in this directory
*
# Except this file
!.gitignore

12
docker-compose.yml Normal file
View File

@ -0,0 +1,12 @@
services:
devicehub-django:
init: true
build:
dockerfile: docker/devicehub-django.Dockerfile
environment:
DEBUG: true
volumes:
- .:/opt/devicehub-django
ports:
- 8000:8000

View File

@ -0,0 +1,35 @@
FROM python:3.11.7-slim-bookworm
# last line is dependencies for weasyprint (for generating pdfs in lafede pilot) https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#debian-11
RUN apt update && \
apt-get install -y \
python3-xapian \
git \
sqlite3 \
jq \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /opt/devicehub-django
# reduce size (python specifics) -> src https://stackoverflow.com/questions/74616667/removing-pip-cache-after-installing-dependencies-in-docker-image
ENV PYTHONDONTWRITEBYTECODE=1
# here document in dockerfile src https://stackoverflow.com/questions/40359282/launch-a-cat-command-unix-into-dockerfile
RUN cat > /etc/pip.conf <<END
[install]
compile = no
[global]
no-cache-dir = True
END
RUN pip install --upgrade pip
COPY ./requirements.txt /opt/devicehub-django
RUN pip install -r requirements.txt
# TODO Is there a better way?
# Set PYTHONPATH to include the directory with the xapian module
ENV PYTHONPATH="${PYTHONPATH}:/usr/lib/python3/dist-packages"
COPY docker/devicehub-django.entrypoint.sh /
ENTRYPOINT sh /devicehub-django.entrypoint.sh

View File

@ -0,0 +1,56 @@
#!/bin/sh
set -e
set -u
# DEBUG
set -x
check_app_is_there() {
if [ ! -f "./manage.py" ]; then
usage
fi
}
deploy() {
# detect if existing deployment (TODO only works with sqlite)
if [ -f "${program_dir}/db/db.sqlite3" ]; then
echo "INFO: detected EXISTING deployment"
./manage.py migrate
else
# move the migrate thing in docker entrypoint
# inspired by https://medium.com/analytics-vidhya/django-with-docker-and-docker-compose-python-part-2-8415976470cc
echo "INFO detected NEW deployment"
./manage.py migrate
./manage.py add_user user@example.org 1234
fi
}
runserver() {
PORT="${PORT:-8000}"
if [ "${DEBUG:-}" = "true" ]; then
./manage.py runserver 0.0.0.0:${PORT}
else
# TODO
#./manage.py collectstatic
true
if [ "${EXPERIMENTAL:-}" = "true" ]; then
# TODO
# reloading on source code changing is a debugging future, maybe better then use debug
# src https://stackoverflow.com/questions/12773763/gunicorn-autoreload-on-source-change/24893069#24893069
# gunicorn with 1 worker, with more than 1 worker this is not expected to work
#gunicorn --access-logfile - --error-logfile - -b :${PORT} trustchain_idhub.wsgi:application
true
else
./manage.py runserver 0.0.0.0:${PORT}
fi
fi
}
main() {
program_dir='/opt/devicehub-django'
cd "${program_dir}"
deploy
runserver
}
main "${@}"