switch to musl for smaller artifacts

This commit is contained in:
2025-03-20 21:17:41 +01:00
parent 046c8d1784
commit b71743ff3f

View File

@ -1,5 +1,8 @@
FROM rust:1.85-slim-bookworm AS build FROM rust:1.85-slim-bookworm AS build
# get x86_64-unknown-linux-musl as a build target
RUN rustup target add x86_64-unknown-linux-musl
# create a new empty shell project # create a new empty shell project
RUN USER=root cargo new --bin http_server RUN USER=root cargo new --bin http_server
WORKDIR /http_server WORKDIR /http_server
@ -18,12 +21,13 @@ COPY ./src ./src
# build for release as a static-pie linked binary # build for release as a static-pie linked binary
RUN rm ./target/release/deps/http_server* RUN rm ./target/release/deps/http_server*
ENV RUSTFLAGS='-C target-feature=+crt-static' ENV RUSTFLAGS='-C target-feature=+crt-static'
RUN cargo build --release --target x86_64-unknown-linux-gnu RUN cargo build --release --target x86_64-unknown-linux-musl
RUN strip /http_server/target/x86_64-unknown-linux-musl/release/http_server
FROM scratch FROM scratch
# copy the build artifact from the build stage # copy the build artifact from the build stage
COPY --from=build /http_server/target/x86_64-unknown-linux-gnu/release/http_server / COPY --from=build /http_server/target/x86_64-unknown-linux-musl/release/http_server /
COPY ./www /www COPY ./www /www
EXPOSE 8080 EXPOSE 8080