@@ -13,7 +13,10 @@ WORKDIR /app
1313ENV PKGCONFIG_SYSROOTDIR=/
1414RUN apk add --no-cache musl-dev openssl-dev zig pkgconf perl make
1515
16- RUN cargo install --locked cargo-zigbuild cargo-chef
16+ # Cache cargo installations
17+ RUN --mount=type=cache,target=/usr/local/cargo/registry \
18+ --mount=type=cache,target=/usr/local/cargo/git \
19+ cargo install --locked cargo-zigbuild cargo-chef
1720RUN rustup target add x86_64-unknown-linux-musl aarch64-unknown-linux-musl
1821
1922# (2) nothing changed
@@ -25,17 +28,26 @@ RUN cargo chef prepare --recipe-path recipe.json
2528FROM rust_chef AS rust_builder
2629COPY --from=rust_planner /app/recipe.json recipe.json
2730ENV OPENSSL_DIR=/usr
28- RUN cargo chef cook --recipe-path recipe.json --release --zigbuild \
29- --target x86_64-unknown-linux-musl --target aarch64-unknown-linux-musl
30-
31- # (4) actuall project build for all targets
31+ # Enable incremental compilation and use cache mounts
32+ ENV CARGO_INCREMENTAL=1
33+ RUN --mount=type=cache,target=/usr/local/cargo/registry \
34+ --mount=type=cache,target=/usr/local/cargo/git \
35+ --mount=type=cache,target=/app/target \
36+ cargo chef cook --recipe-path recipe.json --release --zigbuild \
37+ --target x86_64-unknown-linux-musl --target aarch64-unknown-linux-musl
38+
39+ # (4) actual project build for all targets
3240# binary renamed to easier copy in runtime stage
3341COPY . .
34- RUN cargo zigbuild -r --target x86_64-unknown-linux-musl --target aarch64-unknown-linux-musl && \
35- mkdir -p /app/linux/arm64/ && \
36- mkdir -p /app/linux/amd64/ && \
37- cp target/aarch64-unknown-linux-musl/release/pdp-server /app/linux/arm64/pdp && \
38- cp target/x86_64-unknown-linux-musl/release/pdp-server /app/linux/amd64/pdp
42+ # Use cache mounts for incremental builds - this is the key optimization!
43+ RUN --mount=type=cache,target=/usr/local/cargo/registry \
44+ --mount=type=cache,target=/usr/local/cargo/git \
45+ --mount=type=cache,target=/app/target \
46+ cargo zigbuild -r --target x86_64-unknown-linux-musl --target aarch64-unknown-linux-musl && \
47+ mkdir -p /app/linux/arm64/ && \
48+ mkdir -p /app/linux/amd64/ && \
49+ cp target/aarch64-unknown-linux-musl/release/pdp-server /app/linux/arm64/pdp && \
50+ cp target/x86_64-unknown-linux-musl/release/pdp-server /app/linux/amd64/pdp
3951
4052
4153# OPA BUILD STAGE -----------------------------------
@@ -46,7 +58,10 @@ FROM golang:bullseye AS opa_build
4658COPY custom* /custom
4759
4860# Build OPA binary if custom_opa.tar.gz is provided
49- RUN if [ -f /custom/custom_opa.tar.gz ]; \
61+ # Use BuildKit cache mounts for Go modules and build cache for MUCH faster incremental builds
62+ RUN --mount=type=cache,target=/go/pkg/mod \
63+ --mount=type=cache,target=/root/.cache/go-build \
64+ if [ -f /custom/custom_opa.tar.gz ]; \
5065 then \
5166 cd /custom && \
5267 tar xzf custom_opa.tar.gz && \
@@ -75,9 +90,12 @@ RUN addgroup -S permit -g 1001 && \
7590RUN mkdir -p /app/backup && chmod -R 777 /app/backup
7691
7792# Install necessary libraries and delete SQLite in a single RUN command
78- RUN apk update && \
93+ # Use cache mount for apk to speed up package downloads
94+ RUN --mount=type=cache,target=/var/cache/apk \
95+ ln -s /var/cache/apk /etc/apk/cache && \
96+ apk update && \
7997 apk upgrade && \
80- apk add --no-cache bash build-base libffi-dev libressl-dev musl-dev zlib-dev gcompat wget && \
98+ apk add bash build-base libffi-dev libressl-dev musl-dev zlib-dev gcompat wget && \
8199 apk del sqlite
82100
83101
@@ -106,8 +124,10 @@ COPY kong_routes.json /config/kong_routes.json
106124USER root
107125
108126# Install python dependencies in one command to optimize layer size
127+ # Use cache mount for pip to speed up incremental builds
109128COPY ./requirements.txt ./requirements.txt
110- RUN pip install --upgrade pip setuptools && \
129+ RUN --mount=type=cache,target=/root/.cache/pip \
130+ pip install --upgrade pip setuptools && \
111131 pip install -r requirements.txt && \
112132 python -m pip uninstall -y pip setuptools && \
113133 rm -r /usr/local/lib/python3.10/ensurepip
0 commit comments