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 # Docker
Dockerfile* Dockerfile*
docker-compose* docker-compose*
*.sh
# Python # Python cache
__pycache__ __pycache__
*.pyc *.pyc
*.pyo *.pyo
*.pyd *.pyd
.Python .Python
env
pip-log.txt
pip-delete-this-directory.txt
.tox
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.log *.log
.mypy_cache .mypy_cache
.pytest_cache .pytest_cache
.hypothesis .hypothesis
.ruff_cache
# Django # Python packaging
*.log *.egg-info/
local_settings.py .eggs/
db.sqlite3 build/
db.sqlite3-journal dist/
# Virtual environments # Virtual environments
venv/ venv/
.venv/ .venv/
env/ env/
.env .env
# Removed uv.lock exclusion as it's needed for the build venv.bak/
# IDE # IDE
.vscode/ .vscode/
@@ -58,27 +48,38 @@ env/
ehthumbs.db ehthumbs.db
Thumbs.db Thumbs.db
# Node modules and build artifacts # Node modules (will be installed in container)
node_modules/ node_modules/
npm-debug.log* npm-debug.log*
yarn-debug.log* yarn-debug.log*
yarn-error.log* yarn-error.log*
# Static files (will be built in container) # Build artifacts (will be built in container)
staticfiles/ staticfiles/
new_theme/static_src/node_modules/
# Development files # Development files
README.md
*.md *.md
!README.md
docs/ docs/
scripts/migrate_images.py init.sh
docker.sh
run_server.sh
run_tailwind.sh
# Cache directories # Cache directories
.cache/ .cache/
*.cache *.cache
.pytest_cache/
.coverage
htmlcov/
.tox/
.nox/
# Data and temporary files # Data and temporary files
data/ data/db.sqlite3
data/db.sqlite3-journal
data/celery/
tmp/ tmp/
temp/ temp/
media/ media/
@@ -87,38 +88,20 @@ media/
k8s/ k8s/
.github/ .github/
# Logs
*.log
logs/
# Test files # Test files
tests/ tests/
test_*.py test_*.py
*_test.py *_test.py
# Build and compile artifacts # Scripts
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/migrate_images.py scripts/migrate_images.py
init.sh
docker.sh
run_server.sh
run_tailwind.sh
# Package files (will be installed in container) # Mobile app
package.json 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 FROM ghcr.io/astral-sh/uv:bookworm-slim AS uv
# Build stage # Build stage
FROM python:3.12 AS builder FROM python:3.12-slim AS builder
# Set environment variables # Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONDONTWRITEBYTECODE=1
@@ -9,11 +9,12 @@ ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app ENV PYTHONPATH=/app
ENV UV_CACHE_DIR=/tmp/.cache/uv ENV UV_CACHE_DIR=/tmp/.cache/uv
ENV UV_VENV_DIRECTORY=/app/.venv ENV UV_VENV_DIRECTORY=/app/.venv
ENV DEBIAN_FRONTEND=noninteractive
# Set work directory # Set work directory
WORKDIR /app 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 \ RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \ build-essential \
gcc \ gcc \
@@ -48,13 +49,17 @@ RUN uv run manage.py tailwind install \
&& uv run manage.py collectstatic --noinput \ && uv run manage.py collectstatic --noinput \
&& uv run manage.py compilemessages --locale=zh_Hans && 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 \ RUN rm -rf /tmp/.cache/uv ~/.npm ~/.cache \
&& find /app -name "*.pyc" -delete \ && 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 # Production stage - use Debian Bookworm slim
FROM python:3.12-slim FROM debian:bookworm-slim
# Set environment variables # Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONDONTWRITEBYTECODE=1
@@ -62,40 +67,43 @@ ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app ENV PYTHONPATH=/app
ENV VIRTUAL_ENV=/app/.venv ENV VIRTUAL_ENV=/app/.venv
ENV PATH="/app/.venv/bin:$PATH" ENV PATH="/app/.venv/bin:$PATH"
ENV DEBIAN_FRONTEND=noninteractive
# Set work directory # Set work directory
WORKDIR /app WORKDIR /app
# Install only runtime dependencies # Install only runtime dependencies in one layer
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
python3.12 \
python3.12-lib2to3 \
python3.12-distutils \
curl \ curl \
chromium \ chromium \
chromium-driver \ chromium-driver \
fonts-arphic-ukai \ fonts-noto-cjk \
ca-certificates \
&& rm -rf /var/lib/apt/lists/* \ && rm -rf /var/lib/apt/lists/* \
&& apt-get clean && apt-get clean \
&& rm -rf /tmp/* /var/tmp/*
# Create non-root user and directories # Create non-root user and directories
RUN useradd -m -u 1000 appuser \ RUN useradd -m -u 1000 appuser \
&& mkdir -p /app/data/media/screenshots \ && mkdir -p /app/data/media/screenshots \
&& chown -R appuser:appuser /app && chown -R appuser:appuser /app
# Copy only the virtual environment and built assets # Copy only the virtual environment and built assets from builder
COPY --from=uv /usr/local/bin/uv /usr/local/bin/uv
COPY --from=builder --chown=appuser:appuser /app/.venv /app/.venv 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/staticfiles /app/staticfiles
COPY --from=builder --chown=appuser:appuser /app/static /app/static
COPY --from=builder --chown=appuser:appuser /app/locale /app/locale COPY --from=builder --chown=appuser:appuser /app/locale /app/locale
# Copy application code (minimal)
# Copy application code
COPY --chown=appuser:appuser manage.py ./ COPY --chown=appuser:appuser manage.py ./
COPY --chown=appuser:appuser core/ ./core/ COPY --chown=appuser:appuser core/ ./core/
COPY --chown=appuser:appuser links/ ./links/ COPY --chown=appuser:appuser links/ ./links/
COPY --chown=appuser:appuser new_theme/ ./new_theme/ COPY --chown=appuser:appuser new_theme/ ./new_theme/
COPY --chown=appuser:appuser templates/ ./templates/ COPY --chown=appuser:appuser templates/ ./templates/
# Clean up any Python cache files # Final cleanup
RUN find /app -name "*.pyc" -delete \ RUN 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
@@ -110,4 +118,4 @@ HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/ || exit 1 CMD curl -f http://localhost:8000/ || exit 1
# Run the application # 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"]