Small refactor of scripts, Dockerfile, and docker-compose to support load balancing, and mutiple replicas. Various fixes related to playwright installation in container environment, static file handling, and etc.

This commit is contained in:
badblocks 2025-05-06 23:14:36 -07:00
parent 9b3b3d099f
commit 2dba19a77e
12 changed files with 109 additions and 127 deletions

View file

@ -1,25 +1,16 @@
# syntax=docker/dockerfile:1.9
FROM python3.13-slim-bookworm AS build
### Start build prep.
### This should be a separate build container for better reuse.
FROM python:3.13-slim-bookworm AS build
# The following does not work in Podman unless you build in Docker
# compatibility mode: <https://github.com/containers/podman/issues/8477>
# You can manually prepend every RUN script with `set -ex` too.
SHELL ["sh", "-exc"]
# Ensure apt-get doesn't open a menu on you.
# Ensure apt-get doesn't open a menu
ENV DEBIAN_FRONTEND=noninteractive
### Start build prep.
### This should be a separate build container for better reuse.
# RUN <<EOT
# apt-get update -qy
# apt-get install -qyy \
# -o APT::Install-Recommends=false \
# -o APT::Install-Suggests=false \
# nodejs npm
# EOT
COPY --from=ghcr.io/astral-sh/uv:0.7.2 /uv /usr/local/bin/uv
# - Silence uv complaining about not being able to use hard links,
@ -32,8 +23,6 @@ ENV UV_LINK_MODE=copy \
UV_PYTHON=python3.13 \
UV_PROJECT_ENVIRONMENT=/app
### End build prep -- this is where your app Dockerfile should start.
# Synchronize DEPENDENCIES without the application itself.
# This layer is cached until uv.lock or pyproject.toml change, which are
# only temporarily mounted into the build container since we don't need
@ -60,7 +49,7 @@ RUN --mount=type=cache,target=/root/.cache \
# --no-dev \
# --no-editable
### End build prep
##########################################################################
FROM python:3.13-slim-bookworm
@ -68,64 +57,58 @@ SHELL ["sh", "-exc"]
ARG ENV_FILE=.env.production
# Optional: add the application virtualenv to search path.
ENV PATH=/app/bin:$PATH
ENV PYTHONPATH=/code
ENV PYTHONUNBUFFERED 1
ENV HOME /code
ENV PYTHONPATH=/app
ENV PYTHONUNBUFFERED=1
ENV HOME=/code
ENV DJANGO_SETTINGS_MODULE=django_project.settings
# Don't run your app as root.
WORKDIR /app
# Don't run app as root
RUN <<EOT
groupadd -r app
useradd -r -d /app -g app -N app
groupadd -r app -g 10003
useradd -r -d /app -u 10003 -s /sbin/nologin -g app -N app
EOT
# Note how the runtime dependencies differ from build-time ones.
# Notably, there is no uv either!
# Runtime dependencies
RUN <<EOT
apt-get update -qy
apt-get install -qyy \
-o APT::Install-Recommends=false \
-o APT::Install-Suggests=false \
nodejs xvfb curl
RUN playwright install-deps && playwright install
xvfb curl
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EOT
# COPY docker-entrypoint.sh /
# ENTRYPOINT ["/docker-entrypoint.sh"]
# See <https://hynek.me/articles/docker-signals/>.
STOPSIGNAL SIGINT
# Copy the pre-built `/app` directory to the runtime container
# and change the ownership to user app and group app in one step.
COPY --from=build --chown=app:app /app /app
RUN playwright install --with-deps
# If your application is NOT a proper Python package that got
# pip-installed above, you need to copy your application into
# the container HERE:
COPY . /code
USER app
#WORKDIR /app # If python-packaged
WORKDIR /code # If not python-packaged
# Strictly optional, but I like it for introspection of what I've built
# and run a smoke test that the application can, in fact, be imported.
# RUN <<EOT
# python -V
# python -Im site
# python -Ic 'import the_app'
# EOT`
# the container HERE and set the WORKDIR:
COPY --chown=app:app ./ /code
WORKDIR /code
ENV PYTHONPATH=/code
# WORKDIR /app # if python-packaged
RUN rm -f .env
COPY ${ENV_FILE} .env
COPY --chown=app:app --chmod=700 /scripts/entrypoint.sh /entrypoint.sh
COPY --chown=app:app --chmod=700 /scripts/deploy.sh /deploy.sh
ENTRYPOINT ["/entrypoint.sh"]
USER app
EXPOSE 8000
HEALTHCHECK CMD curl --fail http://localhost:8000/health/ || exit 1
CMD ["granian", "--interface", "wsgi", "django_project.wsgi", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]
CMD ["granian", "--interface", "wsgi", "django_project.wsgi", "--host", "0.0.0.0", "--port", "8000", "--workers", "1", "--respawn-failed-workers", "--workers-kill-timeout", "60"]