Files
links/Dockerfile
T

122 lines
3.5 KiB
Docker

FROM ghcr.io/astral-sh/uv:bookworm-slim AS uv
# Build stage
FROM python:3.12-slim AS builder
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app
ENV UV_CACHE_DIR=/tmp/.cache/uv
ENV UV_VENV_DIRECTORY=/app/.venv
ENV DEBIAN_FRONTEND=noninteractive
# Set work directory
WORKDIR /app
# Install build dependencies in one layer (minimal set)
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
gcc \
nodejs \
npm \
gettext \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# Copy uv binary
COPY --from=uv /usr/local/bin/uv /usr/local/bin/uv
# Copy dependency files first for better caching
COPY pyproject.toml uv.lock ./
# Install Python dependencies
RUN uv sync --no-dev --frozen --no-install-project
# Copy only necessary project files
COPY manage.py ./
COPY core/ ./core/
COPY links/ ./links/
COPY new_theme/ ./new_theme/
COPY templates/ ./templates/
COPY locale/ ./locale/
COPY tailwind.config.js ./
COPY static/ ./static/
# Build static assets and translations
RUN uv run manage.py tailwind install \
&& uv run manage.py tailwind build \
&& uv run manage.py collectstatic --noinput \
&& uv run manage.py compilemessages --locale=zh_Hans
# Clean up build artifacts aggressively
RUN rm -rf /tmp/.cache/uv ~/.npm ~/.cache \
&& find /app -name "*.pyc" -delete \
&& find /app -name "__pycache__" -type d -exec rm -rf {} + || true \
&& find /app -type f -name "*.md" -delete \
&& find /app -type f -name "*.txt" ! -name "requirements.txt" -delete \
&& rm -rf /app/.venv/lib/python3.12/site-packages/pip \
&& rm -rf /app/.venv/lib/python3.12/site-packages/setuptools
# Production stage - use Debian Bookworm slim
FROM debian:bookworm-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app
ENV VIRTUAL_ENV=/app/.venv
ENV PATH="/app/.venv/bin:$PATH"
ENV DEBIAN_FRONTEND=noninteractive
# Set work directory
WORKDIR /app
# Install only runtime dependencies in one layer
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.12 \
python3.12-lib2to3 \
python3.12-distutils \
curl \
chromium \
chromium-driver \
fonts-noto-cjk \
ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean \
&& rm -rf /tmp/* /var/tmp/*
# Create non-root user and directories
RUN useradd -m -u 1000 appuser \
&& mkdir -p /app/data/media/screenshots \
&& chown -R appuser:appuser /app
# Copy only the virtual environment and built assets from builder
COPY --from=builder --chown=appuser:appuser /app/.venv /app/.venv
COPY --from=builder --chown=appuser:appuser /app/staticfiles /app/staticfiles
COPY --from=builder --chown=appuser:appuser /app/locale /app/locale
# Copy application code (minimal)
COPY --chown=appuser:appuser manage.py ./
COPY --chown=appuser:appuser core/ ./core/
COPY --chown=appuser:appuser links/ ./links/
COPY --chown=appuser:appuser new_theme/ ./new_theme/
COPY --chown=appuser:appuser templates/ ./templates/
# Final cleanup
RUN find /app -name "*.pyc" -delete \
&& find /app -name "__pycache__" -type d -exec rm -rf {} + || true
# Switch to non-root user
USER appuser
# Expose port 8000
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/ || exit 1
# Run the application
CMD ["gunicorn", "--chdir", "/app", "core.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "2", "--threads", "4"]