Skip to content

Commit 9b44b81

Browse files
committed
build: refactor Dockerfile for improved caching and version management
- Introduced BuildKit cache mount for faster dependency installation. - Parameterized `uv` version with build arguments for flexibility and consistency. - Simplified UV binary handling using multi-stage builds and improved layer caching. - Enhanced file permission setup for better security and usability.
1 parent d474b07 commit 9b44b81

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

backend/Dockerfile

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# ============================================
2+
# Build Arguments
3+
# ============================================
4+
ARG UV_VERSION=0.9.18
5+
6+
# ============================================
7+
# Stage 0: UV Binary
8+
# ============================================
9+
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv
10+
111
# ============================================
212
# Stage 1: Base - Common Configuration
313
# ============================================
@@ -29,27 +39,27 @@ RUN apt-get update && \
2939
libpq-dev && \
3040
rm -rf /var/lib/apt/lists/*
3141

32-
# Copy uv from official image (pinned version for reproducibility)
33-
COPY --from=ghcr.io/astral-sh/uv:0.5.16 /uv /uvx /usr/local/bin/
42+
# Copy uv from uv stage (pinned version for reproducibility)
43+
COPY --from=uv /uv /uvx /usr/local/bin/
3444

3545
# Copy dependency files first for optimal layer caching
3646
# Changes to source code won't invalidate this layer
3747
COPY pyproject.toml uv.lock ./
3848

39-
# Install dependencies
49+
# Install dependencies with BuildKit cache mount for faster rebuilds
4050
# --frozen: Use exact versions from uv.lock (reproducible builds)
4151
# --no-dev: Exclude development dependencies
4252
# --extra production: Include any production related dependencies (redis, aiocache etc...)
43-
# --no-cache: Prevent uv cache bloat in image
44-
RUN uv sync --frozen --no-default-groups --extra production --no-cache
53+
RUN --mount=type=cache,target=/root/.cache/uv \
54+
uv sync --frozen --no-default-groups --extra production
4555

4656
# ============================================
4757
# Stage 3: Runtime - Production Image
4858
# ============================================
4959
FROM base AS runtime
5060

51-
# Copy uv for runtime execution
52-
COPY --from=ghcr.io/astral-sh/uv:0.9.18 /uv /uvx /usr/local/bin/
61+
# Copy uv from builder stage (ensures version consistency)
62+
COPY --from=builder /usr/local/bin/uv /usr/local/bin/uvx /usr/local/bin/
5363

5464
# Copy installed dependencies from builder stage
5565
# This excludes build tools (gcc, g++, libpq-dev)
@@ -61,10 +71,8 @@ COPY --chown=appuser:appuser . .
6171
# Copy Docker-specific alembic config
6272
COPY --chown=appuser:appuser alembic.docker.ini alembic.ini
6373

64-
# Create files directory and set ownership for entire /app directory
65-
# This ensures appuser can write to .venv and create build artifacts
66-
RUN mkdir -p /app/files && \
67-
chown -R appuser:appuser /app
74+
# Create files directory with proper ownership
75+
RUN mkdir -p /app/files && chown appuser:appuser /app/files
6876

6977
# Switch to non-root user for security
7078
USER appuser

0 commit comments

Comments
 (0)