Build Rust container images faster using layer caching

In the previous blog post I described how to define GitHub Actions pipeline to benefit from caching Rust dependencies and container images’ layers. But the final result may also depend on your Dockerfile. Namely, Docker Buildx Action supports BuildKit and buildx and in order to benefit from this, your Dockerfile has to explicitly cache layers. Actually, this is quite easy to achieve, let’s see the example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # syntax=docker/dockerfile:1.3 FROM rust:1.61.0 AS builder ARG TARGETPLATFORM WORKDIR /root RUN --mount=type=cache,target=/usr/local/cargo/registry,id=${TARGETPLATFORM} \ cargo install cargo-strip COPY . . RUN --mount=type=cache,target=/usr/local/cargo/registry,id=${TARGETPLATFORM} --mount=type=cache,target=/root/target,id=${TARGETPLATFORM} \ cargo build --release && \ cargo strip && \ mv /root/target/release/<your-crate-name> /root FROM gcr.io/distroless/cc-debian11 COPY --from=builder /root/<your-crate-name> / ENTRYPOINT ["./<your-crate-name>"] EXPOSE 3000 The crucial part here is the annotation # syntax=docker/dockerfile:1.4, this will turn on the advanced features. ARG TARGETPLATFORM is optional and will be used just if you have matrix build with different platforms. If you build for single platform, you can remove this line and also ,id=${TARGETPLATFORM} from the remaining lines of the Dockerfile. The next important point is --mount=type=cache,target=/usr/local/cargo/registry. This defines which path should be actually cached as container image layer. Replace <your-crate-name> placeholder with your real crate name and you are ready to go. If you expose your service on a different port than 3000, please modify the final line EXPOSE 3000 to the correct port, or if there are no exposed ports, just delete the line. ...

2022-06-03 · 2 min · Boban Acimovic

How I speeded up my Rust builds on GitHub ~30 times

Rust is a great programming language, but it’s compiler is known to be slow. The main reason is that it checks so many things during the compilation in order to provides memory and concurrency safety. Like in many other cases, pipelines may be speeded up by caching, but it is very important to set the caching properly, otherwise it won’t help. Besides compiling your code may be slow, you also may want to use some cargo tools to lint and check your code and this will require to be compiled as well, if you don’t want to download binaries. Some popular tools are: ...

2022-05-18 · 5 min · Boban Acimovic