Skip to content

Instantly share code, notes, and snippets.

@prenaissance
Created December 14, 2025 21:07
Show Gist options
  • Select an option

  • Save prenaissance/1cbc03e58b9ace42d807d2feb8c46810 to your computer and use it in GitHub Desktop.

Select an option

Save prenaissance/1cbc03e58b9ace42d807d2feb8c46810 to your computer and use it in GitHub Desktop.
Rust performant cross-architecture Dockerfile
# Replace all <____> placeholders with values matching your project
FROM --platform=$BUILDPLATFORM rust:<version>-alpine AS builder
# This will be set automatically by docker buildx
ARG TARGETPLATFORM
WORKDIR /app
# Add any system dependencies here
RUN apk update && apk add --no-cache protobuf-dev postgresql-dev
# Map $TARGETPLATFORM to $RUST_TARGET used by cargo. Add any other desired architectures
RUN case "${TARGETPLATFORM}" in \
"linux/amd64") export RUST_TARGET="x86_64-unknown-linux-musl";; \
"linux/arm64") export RUST_TARGET="aarch64-unknown-linux-musl";; \
*) echo "Unsupported TARGETPLATFORM ${TARGETPLATFORM}"; exit 1;; \
esac && \
rustup target add $RUST_TARGET
ENV CARGO_TARGET_DIR=/app/target
COPY Cargo.toml Cargo.lock build.rs ./
COPY ./protos/ ./protos/
# Simulate src folder to install dependencies & run build script. This optimizes built times on cache misses
RUN mkdir src && echo "fn main() {}" > src/main.rs && cargo build --release && rm -rf src
COPY src ./src
RUN cargo build --release
RUN strip ./target/release/<binary_name>
FROM alpine:latest
WORKDIR /app
EXPOSE <app_ports>
COPY --from=builder /app/target/release/<binary_name> ./<binary_name>
ENTRYPOINT ["./<binary_name>"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment