1
1
FROM --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
3
9
WORKDIR $HOME
4
10
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 \
6
19
&& apt install -y --no-install-recommends \
7
20
build-essential \
8
21
curl \
9
22
python3-venv \
10
- cmake \
11
- && apt clean \
12
- && rm -rf /var/lib/apt/lists/*
23
+ cmake
13
24
14
25
# Setup zig as cross compiling linker
15
26
RUN 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
17
30
ENV PATH="$HOME/.venv/bin:$PATH"
18
31
19
32
# Install rust
@@ -25,21 +38,29 @@ RUN case "$TARGETPLATFORM" in \
25
38
esac
26
39
27
40
# Update rustup whenever we bump the rust version
41
+ ENV PATH="$CARGO_HOME/bin:$PATH"
28
42
COPY 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)
33
48
34
49
# 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 \
39
60
"linux/arm64" ) export JEMALLOC_SYS_WITH_LG_PAGE=16;; \
40
61
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 \
43
64
&& cp target/$(cat rust_target.txt)/release/uvx /uvx
44
65
# TODO(konsti): Optimize binary size, with a version that also works when cross compiling
45
66
# RUN strip --strip-all /uv
0 commit comments