Files

70 lines
2.1 KiB
Docker
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# syntax=docker/dockerfile:1
# 后端(Django + Wagtail)生产镜像,多阶段构建:
# builder — 安装编译依赖 + Python 包(含需要本地编译的 psycopg/Pillow 等)
# runtime — 仅包含运行时所需的系统库 + 已安装的 Python 包,体积更小,非 root 用户运行
ARG PYTHON_VERSION=3.14-slim
FROM python:${PYTHON_VERSION} AS builder
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
WORKDIR /app
# 编译期依赖:psycopg(PostgreSQL 客户端)、Pillow(Wagtail 图片处理)均需要这些头文件/编译工具
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libpq-dev \
libjpeg-dev \
zlib1g-dev \
libwebp-dev \
&& rm -rf /var/lib/apt/lists/*
COPY requirements/ requirements/
RUN pip install --user --no-warn-script-location -r requirements/production.txt
FROM python:${PYTHON_VERSION} AS runtime-base
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PATH=/home/django/.local/bin:$PATH \
DJANGO_SETTINGS_MODULE=wagtailcms.settings.production
WORKDIR /app
# 运行期依赖:只需要动态库,不需要编译工具链
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
libjpeg62-turbo \
libwebp7 \
zlib1g \
&& rm -rf /var/lib/apt/lists/* \
&& groupadd -r django && useradd -r -g django -m -d /home/django django
COPY --from=builder /root/.local /home/django/.local
COPY . .
COPY docker/entrypoint.sh /app/docker/entrypoint.sh
RUN sed -i 's/\r$//' /app/docker/entrypoint.sh \
&& chmod +x /app/docker/entrypoint.sh \
&& mkdir -p /app/staticfiles /app/media \
&& chown -R django:django /app /home/django/.local
USER django
EXPOSE 8000
ENTRYPOINT ["/app/docker/entrypoint.sh"]
CMD ["gunicorn", "wagtailcms.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3"]
FROM runtime-base AS local-test
RUN pip install --user --no-warn-script-location --no-cache-dir -r requirements/dev.txt
FROM runtime-base AS runtime