From f908a0a0b16c6191f5e08ca6cb5570de913f05f9 Mon Sep 17 00:00:00 2001 From: Holger Bruch Date: Sat, 21 Sep 2024 08:48:20 +0200 Subject: [PATCH] incorporate review i.e. * switch to prebuilt gdal image * multistaged docker build * allow superuser creation via flag * use bash for script execution * set -euo pipefial for entrypoint.sh --- .dockerignore | 1 + Dockerfile | 35 ++++++++++++++++++++++++----------- entrypoint.sh | 14 +++++++++++--- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/.dockerignore b/.dockerignore index 7ca43d3..a9f05c6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,6 @@ .dockerignore .gitignore +.env docker-compose.yml Dockerfile env/ diff --git a/Dockerfile b/Dockerfile index 2ae091a..c33dcc1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,34 @@ -FROM python:3.11 +FROM ghcr.io/osgeo/gdal:alpine-small-3.9.2 AS builder LABEL maintainer="Holger Bruch " -ENV RUN_MIGRATION=false +RUN apk add --no-cache build-base python3-dev py3-pip postgresql-client libpq-dev geos-dev -RUN apt-get install -RUN apt-get update && apt-get install -y \ - libpq-dev libgdal-dev libproj-dev libgeos-dev \ - && rm -rf /var/lib/apt/lists/* + +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" WORKDIR /app -COPY requirements.txt /app/ -COPY web/scrapers/ParkAPI2_sources/requirements.txt /app/web/scrapers/ParkAPI2_sources/ +COPY requirements.txt . +COPY web/scrapers/ParkAPI2_sources/requirements.txt ./ParkAPI2_sources_requirements.txt + +RUN pip install -r requirements.txt -r ParkAPI2_sources_requirements.txt + +# Operational stage +FROM ghcr.io/osgeo/gdal:alpine-small-3.9.2 +LABEL maintainer="Holger Bruch " + +RUN apk add --no-cache bash git python3 py3-pip postgresql-client geos -RUN pip install -r requirements.txt -r web/scrapers/ParkAPI2_sources/requirements.txt +COPY --from=builder /opt/venv /opt/venv +ENV RUN_MIGRATION=0 \ + CREATE_SUPERUSER=0 \ + ASSIGN_LOCATIONS=0 \ + PYTHONBUFFERED=1 \ + PATH="/opt/venv/bin:$PATH" -COPY . /app +WORKDIR /app +COPY . /app/ EXPOSE 8000 -ENTRYPOINT ["sh", "/app/entrypoint.sh"] +ENTRYPOINT ["/bin/bash", "/app/entrypoint.sh"] CMD runserver 0.0.0.0:8000 diff --git a/entrypoint.sh b/entrypoint.sh index 7d6d1f0..b44794f 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,16 +1,24 @@ +#!/bin/bash +set -euo pipefail + cd web +if [ "$RUN_MIGRATION" -eq "1" ]; then + echo "RUN MIGRATION is TRUE" +fi # Run migration only if explicitly set via ENV -if [ "$RUN_MIGRATION" != "false" ]; then +if [ "$RUN_MIGRATION" -eq "1" ]; then ./manage.py migrate fi -if [ "$ASSING_LOCATIONS" != "false" ]; then +if [ "$ASSIGN_LOCATIONS" -eq "1" ]; then ./manage.py pa_find_locations fi # don't create an admin interface per default -#./manage.py createsuperuser +if [ "$CREATE_SUPERUSER" -eq "1" ]; then + ./manage.py createsuperuser +fi # start the server ./manage.py $@