Skip to content

Instantly share code, notes, and snippets.

@sassman
Created February 26, 2020 12:04
Show Gist options
  • Save sassman/ceda8885d50fb8da3c5c885c591247d3 to your computer and use it in GitHub Desktop.
Save sassman/ceda8885d50fb8da3c5c885c591247d3 to your computer and use it in GitHub Desktop.
Rust webapp dockerization template for static binary with musl libc
FROM rust:latest as builder
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
musl-tools
RUN rustup target add x86_64-unknown-linux-musl
RUN rustup component add clippy
WORKDIR /x
## doing some overhead here to keep docker build time low
# create a new empty shell project
RUN USER=root cargo new --bin server
WORKDIR /x/server
# copy over your manifests
COPY ./server/Cargo.lock ./
COPY ./server/Cargo.toml ./
# this build step will cache dependencies
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build --target x86_64-unknown-linux-musl --release
RUN rm src/*.rs
## done with the overhead
# skip all other waste
COPY ./server/src ./src
RUN rm -f ./target/release/deps/server*
RUN RUSTFLAGS=-Clinker=musl-gcc \
cargo clippy --target x86_64-unknown-linux-musl --all-features -- -D warnings && \
cargo test --target x86_64-unknown-linux-musl --verbose && \
cargo build --target x86_64-unknown-linux-musl --release --verbose
# take the build artifact
FROM scratch
WORKDIR /x
COPY --from=builder /x/server/target/x86_64-unknown-linux-musl/release/server /x/server
USER 1000
EXPOSE 9090
CMD ["/x/server"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment