Update dockerfile to collect static files at build time

This commit is contained in:
Gabriel Augendre 2022-01-26 12:20:38 +01:00
parent e122c62ef1
commit c0ae4a4d61
2 changed files with 28 additions and 3 deletions

View file

@ -1,6 +1,10 @@
## Build venv ##############################################
# Build virtualenv
##############################################
FROM python:3.10.1-buster AS venv FROM python:3.10.1-buster AS venv
# Prepare system
##############################################
# https://python-poetry.org/docs/#installation # https://python-poetry.org/docs/#installation
ENV POETRY_VERSION=1.1.11 ENV POETRY_VERSION=1.1.11
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python - 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 WORKDIR /app
# Install python dependencies
##############################################
COPY pyproject.toml poetry.lock ./ COPY pyproject.toml poetry.lock ./
RUN python -m venv --copies /app/venv \ RUN python -m venv --copies /app/venv \
&& . /app/venv/bin/activate \ && . /app/venv/bin/activate \
&& poetry install --no-dev && 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 ## Get git versions
FROM alpine/git:v2.30.2 AS git FROM alpine/git:v2.30.2 AS git
@ -23,7 +43,10 @@ WORKDIR /app
RUN git rev-parse HEAD | tee /version RUN git rev-parse HEAD | tee /version
## Beginning of runtime image
##############################################
# Main image
##############################################
FROM python:3.10.1-slim-buster as prod FROM python:3.10.1-slim-buster as prod
RUN echo "Europe/Paris" > /etc/timezone RUN echo "Europe/Paris" > /etc/timezone
@ -37,6 +60,8 @@ COPY pyproject.toml ./
COPY contrib/run ./run COPY contrib/run ./run
COPY src ./src/ COPY src ./src/
COPY --from=git /version /app/.version 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)" HEALTHCHECK --start-period=30s CMD python -c "import requests; requests.get('http://localhost:8000', timeout=2)"

View file

@ -131,7 +131,7 @@ USE_TZ = True
STATIC_URL = "/static/" STATIC_URL = "/static/"
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") STATIC_ROOT = BASE_DIR / "staticfiles"
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field