From c0ae4a4d61a0b3921b32d87f177a81ab42835956 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Wed, 26 Jan 2022 12:20:38 +0100 Subject: [PATCH] Update dockerfile to collect static files at build time --- Dockerfile | 29 +++++++++++++++++++++++++++-- src/shortener/settings.py | 2 +- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9560bc4..785b618 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,10 @@ -## Build venv +############################################## +# Build virtualenv +############################################## FROM python:3.10.1-buster AS venv +# Prepare system +############################################## # https://python-poetry.org/docs/#installation ENV POETRY_VERSION=1.1.11 RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python - @@ -9,12 +13,28 @@ ENV PATH /root/.local/bin:$PATH WORKDIR /app +# Install python dependencies +############################################## COPY pyproject.toml poetry.lock ./ RUN python -m venv --copies /app/venv \ && . /app/venv/bin/activate \ && poetry install --no-dev +# Collect static files & build assets +############################################## +ENV PATH /app/venv/bin:$PATH +WORKDIR /app/src +COPY ./src ./ + +# Build assets so that we don't need the build tools later +RUN python manage.py collectstatic --noinput --clear + + + +############################################## +# write git info +############################################## ## Get git versions FROM alpine/git:v2.30.2 AS git @@ -23,7 +43,10 @@ WORKDIR /app RUN git rev-parse HEAD | tee /version -## Beginning of runtime image + +############################################## +# Main image +############################################## FROM python:3.10.1-slim-buster as prod RUN echo "Europe/Paris" > /etc/timezone @@ -37,6 +60,8 @@ COPY pyproject.toml ./ COPY contrib/run ./run COPY src ./src/ COPY --from=git /version /app/.version +ARG STATIC_ROOT=/app/src/staticfiles +COPY --chown=django:django --from=venv $STATIC_ROOT $STATIC_ROOT HEALTHCHECK --start-period=30s CMD python -c "import requests; requests.get('http://localhost:8000', timeout=2)" diff --git a/src/shortener/settings.py b/src/shortener/settings.py index 5ebad84..1d50fe4 100644 --- a/src/shortener/settings.py +++ b/src/shortener/settings.py @@ -131,7 +131,7 @@ USE_TZ = True STATIC_URL = "/static/" STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" -STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") +STATIC_ROOT = BASE_DIR / "staticfiles" # Default primary key field type # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field