11FROM --platform=$BUILDPLATFORM ubuntu AS build
2- ENV HOME="/root"
2+ ENV HOME="/root" \
3+ # Place tool-specific caches in the buildkit tool cache.
4+ CARGO_HOME=/buildkit-cache/cargo \
5+ CARGO_ZIGBUILD_CACHE_DIR=/buildkit-cache/cargo-zigbuild \
6+ PIP_CACHE_DIR=/buildkit-cache/pip \
7+ RUSTUP_HOME=/buildkit-cache/rustup \
8+ ZIG_GLOBAL_CACHE_DIR=/buildkit-cache/zig
39WORKDIR $HOME
410
5- RUN apt update \
11+ RUN \
12+ --mount=type=cache,target=/var/cache/apt,sharing=locked \
13+ --mount=type=cache,target=/var/lib/apt,sharing=locked \
14+ # remove the default docker-specific apt config that auto-deletes /var/apt/cache archives
15+ rm -f /etc/apt/apt.conf.d/docker-clean && \
16+ # and configure apt-get to keep downloaded archives in the cache
17+ echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache && \
18+ apt update \
619 && apt install -y --no-install-recommends \
720 build-essential \
821 curl \
922 python3-venv \
10- cmake \
11- && apt clean \
12- && rm -rf /var/lib/apt/lists/*
23+ cmake
1324
1425# Setup zig as cross compiling linker
1526RUN python3 -m venv $HOME/.venv
16- RUN .venv/bin/pip install cargo-zigbuild
27+ RUN \
28+ --mount=type=cache,target=/buildkit-cache,id="tool-caches" \
29+ .venv/bin/pip install cargo-zigbuild
1730ENV PATH="$HOME/.venv/bin:$PATH"
1831
1932# Install rust
@@ -25,21 +38,29 @@ RUN case "$TARGETPLATFORM" in \
2538 esac
2639
2740# Update rustup whenever we bump the rust version
41+ ENV PATH="$CARGO_HOME/bin:$PATH"
2842COPY rust-toolchain.toml rust-toolchain.toml
29- RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --target $(cat rust_target.txt) --profile minimal --default-toolchain none
30- ENV PATH="$HOME/.cargo/bin:$PATH"
31- # Installs the correct toolchain version from rust-toolchain.toml and then the musl target
32- RUN rustup target add $(cat rust_target.txt)
43+ RUN \
44+ --mount=type=cache,target=/buildkit-cache,id="tool-caches" \
45+ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --target $(cat rust_target.txt) --profile minimal --default-toolchain none \
46+ # Installs the correct toolchain version from rust-toolchain.toml and then the musl target
47+ && rustup target add $(cat rust_target.txt)
3348
3449# Build
35- COPY crates crates
36- COPY ./Cargo.toml Cargo.toml
37- COPY ./Cargo.lock Cargo.lock
38- RUN case "${TARGETPLATFORM}" in \
50+ RUN \
51+ # bind mounts to access Cargo config, lock, and sources, without having to
52+ # copy them into the build layer and so bloat the docker build cache
53+ --mount=type=bind,source=crates,target=crates \
54+ --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
55+ --mount=type=bind,source=Cargo.lock,target=Cargo.lock \
56+ # Cache mounts to speed up builds
57+ --mount=type=cache,target=$HOME/target/ \
58+ --mount=type=cache,target=/buildkit-cache,id="tool-caches" \
59+ case "${TARGETPLATFORM}" in \
3960 "linux/arm64" ) export JEMALLOC_SYS_WITH_LG_PAGE=16;; \
4061 esac && \
41- cargo zigbuild --bin uv --bin uvx --target $(cat rust_target.txt) --release
42- RUN cp target/$(cat rust_target.txt)/release/uv /uv \
62+ cargo zigbuild --bin uv --bin uvx --target $(cat rust_target.txt) --release \
63+ && cp target/$(cat rust_target.txt)/release/uv /uv \
4364 && cp target/$(cat rust_target.txt)/release/uvx /uvx
4465# TODO(konsti): Optimize binary size, with a version that also works when cross compiling
4566# RUN strip --strip-all /uv
0 commit comments