Update dockerfile to optimize file size

This commit is contained in:
2025-11-04 20:32:52 +11:00
parent 4c41ae1f9a
commit 1c14458ab0
2 changed files with 58 additions and 67 deletions
+34 -51
View File
@@ -6,41 +6,31 @@
# Docker
Dockerfile*
docker-compose*
*.sh
# Python
# Python cache
__pycache__
*.pyc
*.pyo
*.pyd
.Python
env
pip-log.txt
pip-delete-this-directory.txt
.tox
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.log
.mypy_cache
.pytest_cache
.hypothesis
.ruff_cache
# Django
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Python packaging
*.egg-info/
.eggs/
build/
dist/
# Virtual environments
venv/
.venv/
env/
.env
# Removed uv.lock exclusion as it's needed for the build
venv.bak/
# IDE
.vscode/
@@ -58,27 +48,38 @@ env/
ehthumbs.db
Thumbs.db
# Node modules and build artifacts
# Node modules (will be installed in container)
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Static files (will be built in container)
# Build artifacts (will be built in container)
staticfiles/
new_theme/static_src/node_modules/
# Development files
README.md
*.md
!README.md
docs/
scripts/migrate_images.py
init.sh
docker.sh
run_server.sh
run_tailwind.sh
# Cache directories
.cache/
*.cache
.pytest_cache/
.coverage
htmlcov/
.tox/
.nox/
# Data and temporary files
data/
data/db.sqlite3
data/db.sqlite3-journal
data/celery/
tmp/
temp/
media/
@@ -87,38 +88,20 @@ media/
k8s/
.github/
# Logs
*.log
logs/
# Test files
tests/
test_*.py
*_test.py
# Build and compile artifacts
build/
dist/
*.egg-info/
.eggs/
# Additional cache and temporary files
.pytest_cache/
.coverage
htmlcov/
.tox/
.nox/
venv.bak/
# Development and documentation files
*.md
!pyproject.toml
docs/
# Scripts
scripts/migrate_images.py
init.sh
docker.sh
run_server.sh
run_tailwind.sh
# Package files (will be installed in container)
package.json
# Mobile app
mobile/
# Keep essential files for build
!pyproject.toml
!uv.lock
!package.json
!package-lock.json
!tailwind.config.js
+24 -16
View File
@@ -1,7 +1,7 @@
FROM ghcr.io/astral-sh/uv:bookworm-slim AS uv
# Build stage
FROM python:3.12 AS builder
FROM python:3.12-slim AS builder
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
@@ -9,11 +9,12 @@ 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
# Install build dependencies in one layer (minimal set)
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
gcc \
@@ -48,13 +49,17 @@ RUN uv run manage.py tailwind install \
&& uv run manage.py collectstatic --noinput \
&& uv run manage.py compilemessages --locale=zh_Hans
# Clean up build cache and unnecessary files
# 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 -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
FROM python:3.12-slim
# Production stage - use Debian Bookworm slim
FROM debian:bookworm-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
@@ -62,40 +67,43 @@ 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
# 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-arphic-ukai \
fonts-noto-cjk \
ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
&& 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
COPY --from=uv /usr/local/bin/uv /usr/local/bin/uv
# 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/static /app/static
COPY --from=builder --chown=appuser:appuser /app/locale /app/locale
# Copy application code
# 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/
# Clean up any Python cache files
# Final cleanup
RUN find /app -name "*.pyc" -delete \
&& find /app -name "__pycache__" -type d -exec rm -rf {} + || true
@@ -110,4 +118,4 @@ 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"]
CMD ["gunicorn", "--chdir", "/app", "core.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "2", "--threads", "4"]