Skip to content

Commit

Permalink
ci: use buildkit cache for dockerfile (#860)
Browse files Browse the repository at this point in the history
## Summary
Using new buildx cache instead of cargo chef for image updates, makes
subsequent updates much faster locally.

## Background
Cargo chef has not created huge speed up in build times due to nature of
our dependencies. In local tests it improves second build by ~1 minute
while slowing first build by about 3 minutes. This makes the first local
build take ~4 min and then subsequent minor updates can be built in less
than 30s.

## Changes
- Remove cargo chef
- Utilize buildkit caches

## Testing
Running locally, testing image built + CI tests on here to see how it
impacts CI build time.
  • Loading branch information
joroshiba authored Apr 3, 2024
1 parent 5da64fd commit f472a22
Showing 1 changed file with 10 additions and 36 deletions.
46 changes: 10 additions & 36 deletions containerfiles/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# build stage
FROM --platform=$BUILDPLATFORM lukemathwalker/cargo-chef:0.1.66-rust-1.76.0-bookworm AS chef
FROM --platform=$BUILDPLATFORM rust:1.76-bookworm AS rust

WORKDIR /build/

Expand Down Expand Up @@ -40,40 +39,17 @@ RUN \
install ./protoc/include/google/protobuf/* -Dt /usr/local/include/google/protobuf; \
install ./protoc/include/google/protobuf/compiler/* -Dt /usr/local/include/google/protobuf/compiler;

# install targets
FROM chef AS planner
ARG TARGETBINARY
COPY . .
RUN cargo chef prepare --bin $TARGETBINARY --recipe-path recipe.json

FROM chef as builder
COPY --from=planner /build/recipe.json recipe.json
FROM rust as builder

ARG BUILDPLATFORM
ARG TARGETPLATFORM
ARG TARGETBINARY
RUN \
if [ "$TARGETPLATFORM" = "linux/arm64" ] && [ "$BUILDPLATFORM" != "linux/arm64" ]; then \
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \
CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc \
CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++ \
PKG_CONFIG_SYSROOT_DIR=/usr/aarch64-linux-gnu; \
TARGET_TRIPLE=aarch64-unknown-linux-gnu; \
elif [ "$TARGETPLATFORM" = "linux/amd64" ] && [ "$BUILDPLATFORM" != "linux/amd64" ]; then \
export CARGO_TARGET_x86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc \
CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc \
CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++ \
PKG_CONFIG_SYSROOT_DIR=/usr/x86_64-linux-gnu; \
TARGET_TRIPLE=x86_64-unknown-linux-gnu; \
else \
TARGET_TRIPLE=$(uname -m)-unknown-linux-gnu; \
fi; \
export PROTOC=/usr/local/bin/protoc; \
cargo chef cook --release --bin $TARGETBINARY --target $TARGET_TRIPLE --recipe-path recipe.json;

COPY . .

RUN mkdir -p release
RUN \
--mount=type=cache,target=/usr/local/cargo/registry,id=${TARGETPLATFORM}-${TARGETBINARY} \
--mount=type=cache,target=/build/target,id=${TARGETPLATFORM}-${TARGETBINARY} \
if [ "$TARGETPLATFORM" = "linux/arm64" ] && [ "$BUILDPLATFORM" != "linux/arm64" ]; then \
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \
CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc \
Expand All @@ -90,13 +66,11 @@ RUN \
TARGET_TRIPLE=$(uname -m)-unknown-linux-gnu; \
fi; \
export PROTOC=/usr/local/bin/protoc; \
cargo build --release --target $TARGET_TRIPLE --bin $TARGETBINARY;

# replace this with `--out` or `--out-dir` once stable
RUN mkdir -p target/release
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then ARCH=aarch64; \
cargo build --release --target $TARGET_TRIPLE --bin $TARGETBINARY;\
# Copies the binary from out of the cache directory
if [ "$TARGETPLATFORM" = "linux/arm64" ]; then ARCH=aarch64; \
elif [ "$TARGETPLATFORM" = "linux/amd64" ]; then ARCH=x86_64; fi; \
cp target/$ARCH-unknown-linux-gnu/release/$TARGETBINARY target/release/
cp target/$ARCH-unknown-linux-gnu/release/$TARGETBINARY release/;

FROM debian:bookworm-slim
ARG TARGETBINARY
Expand All @@ -107,6 +81,6 @@ RUN \
apt install -y wget ca-certificates; \
apt-get clean; \
rm -rf /var/lib/apt/lists/*;
COPY --from=builder /build/target/release/$TARGETBINARY /usr/local/bin/$TARGETBINARY
COPY --from=builder /build/release/$TARGETBINARY /usr/local/bin/$TARGETBINARY
RUN ln -s /usr/local/bin/$TARGETBINARY /usr/local/bin/entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint"]

0 comments on commit f472a22

Please sign in to comment.