2022-10-28 22:16:23 +02:00
|
|
|
##############################################
|
|
|
|
# Build virtualenv
|
|
|
|
##############################################
|
2022-10-31 09:31:22 +01:00
|
|
|
FROM python:3.11.0-bullseye AS venv
|
2022-10-28 22:16:23 +02:00
|
|
|
|
|
|
|
# Prepare poetry
|
|
|
|
##############################################
|
|
|
|
# https://python-poetry.org/docs/#installation
|
|
|
|
ENV POETRY_VERSION=1.1.15
|
|
|
|
RUN curl -sSL https://install.python-poetry.org | python3 -
|
|
|
|
|
|
|
|
ENV PATH /root/.local/bin:$PATH
|
|
|
|
|
|
|
|
RUN python -m pip install --user poetry-lock-check==0.1.0 \
|
|
|
|
cleo==0.8.1 # poetry-lock-check depends on cleo
|
|
|
|
|
|
|
|
WORKDIR /app
|
|
|
|
COPY pyproject.toml poetry.lock ./
|
|
|
|
|
|
|
|
RUN python -m poetry_lock_check check-lock
|
|
|
|
|
|
|
|
# Install python dependencies
|
|
|
|
##############################################
|
|
|
|
RUN python -m venv --copies /app/venv
|
2022-10-31 09:30:26 +01:00
|
|
|
ARG POETRY_OPTIONS
|
2022-10-28 22:16:23 +02:00
|
|
|
# Will install dev deps as well, so that we can run tests in this image
|
|
|
|
RUN . /app/venv/bin/activate \
|
2022-10-31 09:30:26 +01:00
|
|
|
&& poetry install --no-interaction $POETRY_OPTIONS
|
2022-10-28 22:16:23 +02:00
|
|
|
|
|
|
|
ENV PATH /app/venv/bin:$PATH
|
|
|
|
|
|
|
|
# Collect static files & build assets
|
|
|
|
##############################################
|
|
|
|
|
|
|
|
COPY ./src /app/src
|
|
|
|
COPY ./envs/local-envs.env /app/.env
|
|
|
|
WORKDIR /app/src
|
|
|
|
|
|
|
|
# Required for manage.py to startup
|
|
|
|
ARG ENV_FILE=/app/.env
|
2022-10-31 09:45:00 +01:00
|
|
|
ARG DEBUG=false
|
2022-10-28 22:16:23 +02:00
|
|
|
ENV STATIC_ROOT=/app/static
|
2022-10-31 09:01:44 +01:00
|
|
|
ENV DATABASE_URL=sqlite:////app/db/db.sqlite3
|
2022-10-28 22:16:23 +02:00
|
|
|
|
|
|
|
RUN mkdir -p $STATIC_ROOT
|
|
|
|
|
|
|
|
# Build assets so that we don't need the build tools later
|
|
|
|
RUN python manage.py collectstatic --noinput --clear
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##############################################
|
|
|
|
# write git info
|
|
|
|
##############################################
|
2022-10-31 09:06:48 +01:00
|
|
|
FROM alpine/git:v2.36.3 AS git
|
2022-10-28 22:16:23 +02:00
|
|
|
|
|
|
|
WORKDIR /app
|
|
|
|
COPY .git /app/.git/
|
|
|
|
RUN git describe --tags --always > /git-describe
|
|
|
|
RUN git rev-parse HEAD > /git-commit
|
|
|
|
RUN date +'%Y-%m-%d %H:%M %Z' > /build-date
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##############################################
|
|
|
|
# Main image
|
|
|
|
##############################################
|
2022-10-31 09:31:22 +01:00
|
|
|
FROM python:3.11.0-slim-bullseye AS final
|
2022-10-28 22:16:23 +02:00
|
|
|
|
|
|
|
ARG DEBIAN_FRONTEND=noninteractive
|
|
|
|
|
|
|
|
# Setup user & group
|
|
|
|
##############################################
|
|
|
|
RUN groupadd -g 1000 django
|
|
|
|
RUN useradd -M -d /app -u 1000 -g 1000 -s /bin/bash django
|
|
|
|
|
|
|
|
# Setup system
|
|
|
|
##############################################
|
|
|
|
RUN apt-get update -y \
|
|
|
|
&& apt-get install -y --no-install-recommends \
|
|
|
|
libxml2 \
|
|
|
|
media-types \
|
|
|
|
postgresql-client
|
|
|
|
|
|
|
|
# Fetch project requirements
|
|
|
|
##############################################
|
|
|
|
COPY --chown=django:django --from=venv /app/venv /app/venv/
|
|
|
|
COPY --chown=django:django --from=git /git-describe /git-commit /build-date /app/git/
|
|
|
|
ENV PATH /app/venv/bin:$PATH
|
|
|
|
|
|
|
|
# Fetch built assets & static files
|
|
|
|
##############################################
|
|
|
|
ENV STATIC_ROOT=/app/static
|
|
|
|
COPY --chown=django:django --from=venv $STATIC_ROOT $STATIC_ROOT
|
|
|
|
|
|
|
|
# Create directory structure
|
|
|
|
##############################################
|
|
|
|
WORKDIR /app
|
|
|
|
COPY pyproject.toml poetry.lock ./
|
|
|
|
ADD --chown=django:django ./src ./src
|
|
|
|
COPY --chown=django:django tasks.py ./tasks.py
|
|
|
|
|
|
|
|
|
2022-10-31 09:10:14 +01:00
|
|
|
RUN mkdir -p /app/data /app/db
|
|
|
|
RUN chown django:django /app /app/data /app/db
|
|
|
|
|
|
|
|
ENV SECRET_KEY "changeme"
|
|
|
|
ENV DEBUG "false"
|
|
|
|
ENV DATABASE_URL "sqlite:////app/db/db.sqlite3"
|
2022-10-28 22:16:23 +02:00
|
|
|
|
|
|
|
EXPOSE 8000
|
|
|
|
|
|
|
|
WORKDIR /app/src
|
|
|
|
|
2022-11-10 17:42:05 +01:00
|
|
|
HEALTHCHECK --start-period=30s CMD python -c "import requests; requests.get('http://localhost:8000', timeout=2)"
|
|
|
|
|
2022-10-28 22:16:23 +02:00
|
|
|
USER django
|
2022-11-10 18:27:45 +01:00
|
|
|
CMD ["gunicorn", "charasheet.wsgi", "--graceful-timeout=5", "--worker-tmp-dir=/dev/shm", "--workers=2", "--threads=4", "--worker-class=gthread", "--bind=0.0.0.0:8000", "--log-file=-"]
|