Skip to content

Instantly share code, notes, and snippets.

@wrboyce
Created February 6, 2020 13:56
Show Gist options
  • Save wrboyce/a23e084f4f65fefecb348b085d0a76d4 to your computer and use it in GitHub Desktop.
Save wrboyce/a23e084f4f65fefecb348b085d0a76d4 to your computer and use it in GitHub Desktop.
Dockerfile experimental syntax for properly cached cargo builds
# syntax=docker/dockerfile:experimental
FROM rust:slim as builder
RUN apt-get update && \
apt-get install musl-tools -y && \
apt-get clean && rm -rf /var/lib/apt/lists/* && \
rustup target add x86_64-unknown-linux-musl
COPY . .
RUN --mount=type=cache,target=/usr/local/cargo,from=rust:slim,source=/usr/local/cargo \
--mount=type=cache,target=target \
RUSTFLAGS=-Clinker=musl-gcc \
cargo build \
--target=x86_64-unknown-linux-musl \
--release && \
cp target/x86_64-unknown-linux-musl/release/foo /foo
FROM scratch as main
COPY --from=builder /foo /foo
ENTRYPOINT ["/foo"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment