Skip to content

Commit aab7aeb

Browse files
authored
dan/per-13354-pdp-speed-up-docker-build (#294)
* Add Dockerfile and .dockerignore for improved caching and build efficiency - Updated Dockerfile to utilize BuildKit cache mounts for cargo, Go modules, and pip installations, optimizing incremental builds. - Expanded .dockerignore to include additional files and directories for cleaner builds, including Python artifacts, logs, and local development files. * Update .gitignore to exclude OPA build bundles from version control
1 parent d8bd7ed commit aab7aeb

File tree

3 files changed

+83
-22
lines changed

3 files changed

+83
-22
lines changed

.dockerignore

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,55 @@
1+
# Version control
12
.git/
2-
helm/
3-
.venv/
4-
.github/
3+
.gitignore
4+
5+
# Build artifacts
56
target/
7+
.cargo/
68
**/*.rs.bk
79
**/*.pdb
810
Cargo.lock
9-
.cargo/
10-
.dockerignore
11-
.gitignore
11+
12+
# Documentation
1213
README.md
1314
docs/
14-
**/tests/
15+
*.md
16+
!requirements.md
17+
18+
# CI/CD and deployment
19+
.github/
20+
helm/
21+
.dockerignore
22+
23+
# Python artifacts
24+
.venv/
1525
**/__pycache__/
1626
**/*.pyc
17-
**/.DS_Store
27+
**/*.pyo
28+
**/.pytest_cache/
29+
.coverage
30+
htmlcov/
31+
32+
# IDE and editor files
33+
.vscode/
34+
.idea/
35+
*.swp
36+
*.swo
37+
*~
38+
.DS_Store
39+
40+
# Test files
41+
**/tests/
42+
**/*_test.rs
43+
**/*_test.go
44+
**/test_*.py
45+
46+
# Logs and temp files
47+
*.log
48+
tmp/
49+
temp/
50+
.tmp/
51+
52+
# Local development
53+
.env
54+
.env.local
55+
*.local

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,6 @@ Cargo.lock
161161
# and can be added to the global gitignore or merged into this file. For a more nuclear
162162
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
163163
#.idea/
164+
165+
# Remove OPA build bundles (downloaded by build scripts)
166+
/custom/*

Dockerfile

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ WORKDIR /app
1313
ENV PKGCONFIG_SYSROOTDIR=/
1414
RUN 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
1720
RUN 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
2528
FROM rust_chef AS rust_builder
2629
COPY --from=rust_planner /app/recipe.json recipe.json
2730
ENV 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
3341
COPY . .
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
4658
COPY 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 && \
7590
RUN 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
106124
USER root
107125

108126
# Install python dependencies in one command to optimize layer size
127+
# Use cache mount for pip to speed up incremental builds
109128
COPY ./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

Comments
 (0)