From 554c1e9cf49ad850157da9465d4dac78db7267ad Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Thu, 18 Jan 2024 19:53:52 -0600 Subject: [PATCH] container: Build static executable Building a static executable and distributing it in a "from scratch" container image dramatically reduces the image size: down to 8 MB from 102 MB. --- Containerfile | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Containerfile b/Containerfile index a561900..30487c3 100644 --- a/Containerfile +++ b/Containerfile @@ -1,23 +1,23 @@ -FROM registry.fedoraproject.org/fedora-minimal:39 AS build +FROM docker.io/library/rust:1.73-alpine AS build RUN --mount=type=cache,target=/var/cache \ - microdnf install -y \ - --setopt install_weak_deps=0 \ - cargo\ + apk add \ + musl-dev \ && : COPY . /src WORKDIR /src -RUN cargo build --release --locked +RUN cargo build --release --locked \ + && strip target/release/tmpl -FROM registry.fedoraproject.org/fedora-minimal:39 +FROM scratch -COPY --from=build /src/target/release/tmpl /usr/local/bin +COPY --from=build /src/target/release/tmpl /tmpl -ENTRYPOINT ["/usr/local/bin/tmpl"] +ENTRYPOINT ["/tmpl"] LABEL name='tmpl' \ vendor='Dustin C. Hatch' \