Skip to content

Commit e5b6734

Browse files
committed
Enable build caching to optimize Docker image layer reuse
This change introduces build cache mounts for both APT package management and UV commands in the Dockerfile. These updates reduce redundant downloads and improve build performance by leveraging cached data across builds. The new configuration enhances efficiency while maintaining current behavior.
1 parent 2bd852f commit e5b6734

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Dockerfile

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ RUN mkdir /venv && chown nonroot:nonroot /venv
1414
ENV PATH="/venv/bin:$PATH"
1515

1616
# Install necessary runtime libraries (e.g. libmysql)
17-
RUN apt-get update \
17+
RUN --mount=type=cache,id=apt-cache,target=/var/cache/apt,sharing=locked \
18+
--mount=type=cache,id=apt-lib,target=/var/lib/apt,sharing=locked \
19+
apt-get update \
1820
&& apt-get install -y --no-install-recommends \
19-
make \
20-
&& rm -rf /var/lib/apt/lists/*
21+
make
2122

2223
FROM base AS base_builder
2324
ENV UV_PROJECT_ENVIRONMENT=/venv
@@ -26,10 +27,11 @@ ENV UV_COMPILE_BYTECODE=1
2627

2728
# Install build system requirements (gcc, library headers, etc.)
2829
# for compiled Python requirements like psycopg2
29-
RUN apt-get update \
30+
RUN --mount=type=cache,id=apt-cache,target=/var/cache/apt,sharing=locked \
31+
--mount=type=cache,id=apt-lib,target=/var/lib/apt,sharing=locked \
32+
apt-get update \
3033
&& apt-get install -y --no-install-recommends \
31-
build-essential gcc git \
32-
&& rm -rf /var/lib/apt/lists/*
34+
build-essential gcc git
3335

3436
COPY --from=ghcr.io/astral-sh/uv:0.6.3 /uv /uvx /bin/
3537

@@ -44,7 +46,7 @@ COPY --chown=nonroot:nonroot Makefile .
4446
# Dev image, contains all files and dependencies
4547
FROM base_builder AS dev
4648
COPY --chown=nonroot:nonroot . .
47-
RUN --mount=type=cache,target=~/.cache/uv \
49+
RUN --mount=type=cache,id=uv,target=~/.cache/uv,sharing=locked \
4850
make dev-dependencies
4951

5052
# Note that opentelemetry doesn't play well together with uvicorn reloader
@@ -53,22 +55,22 @@ CMD ["uvicorn", "http_app:create_app", "--host", "0.0.0.0", "--port", "8000", "-
5355

5456
# Installs requirements to run production dramatiq application
5557
FROM base_builder AS dramatiq_builder
56-
RUN --mount=type=cache,target=~/.cache/uv \
58+
RUN --mount=type=cache,id=uv,target=~/.cache/uv,sharing=locked \
5759
uv sync --no-dev --no-install-project --frozen --no-editable
5860

5961
# Installs requirements to run production http application
6062
FROM base_builder AS http_builder
61-
RUN --mount=type=cache,target=~/.cache/uv \
63+
RUN --mount=type=cache,id=uv,target=~/.cache/uv,sharing=locked \
6264
uv sync --no-dev --group http --no-install-project --frozen --no-editable
6365

6466
# Installs requirements to run production socketio application
6567
FROM base_builder AS socketio_builder
66-
RUN --mount=type=cache,target=~/.cache/uv \
68+
RUN --mount=type=cache,id=uv,target=~/.cache/uv,sharing=locked \
6769
uv sync --no-dev --group socketio --no-install-project --frozen --no-editable
6870

6971
# Installs requirements to run production migrations application
7072
FROM base_builder AS migrations_builder
71-
RUN --mount=type=cache,target=~/.cache/uv \
73+
RUN --mount=type=cache,target=~/.cache/uv,sharing=locked \
7274
uv sync --no-dev --group migrations --no-install-project --frozen --no-editable
7375

7476
# Create the base app with the common python packages

0 commit comments

Comments
 (0)