From c743af20a87b6df670e14396922ad032646a7d1d Mon Sep 17 00:00:00 2001 From: AFCMS Date: Thu, 26 Sep 2024 12:02:20 +0200 Subject: [PATCH] Improve Dockerfile - Cross-compilation support (amd64 + arm64) - Use cargo build cache - Add opencontainers labels --- .dockerignore | 4 ++++ Dockerfile | 45 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..136b3eca --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +scripts +.github +.editorconfig +.pre-commit-hooks.yaml diff --git a/Dockerfile b/Dockerfile index 9eeeaa1a..636c5835 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,45 @@ -FROM rust:alpine as base +# syntax=docker/dockerfile:1 +FROM --platform=$BUILDPLATFORM tonistiigi/xx AS xx -COPY . /src +FROM --platform=$BUILDPLATFORM rust:1.74-alpine AS base -RUN rustup update 1.74 && rustup default 1.74 - -RUN apk update \ - && apk add \ +RUN apk update && \ + apk add \ gcc \ - g++ + g++ \ + clang -RUN cd /src && cargo build --release +COPY --from=xx / / -FROM alpine as tool +ARG TARGETPLATFORM +RUN xx-info env -COPY --from=base /src/target/release/oxipng /usr/local/bin +RUN xx-apk add \ + gcc \ + musl-dev \ + libdeflate WORKDIR /src + +COPY . . + +RUN --mount=type=cache,target=/root/.cargo/git/db \ + --mount=type=cache,target=/root/.cargo/registry/cache \ + --mount=type=cache,target=/root/.cargo/registry/index \ + xx-cargo build --release && \ + xx-verify /src/target/$(xx-cargo --print-target-triple)/release/oxipng && \ + cp /src/target/$(xx-cargo --print-target-triple)/release/oxipng /src/target/oxipng + +FROM alpine AS tool + +LABEL org.opencontainers.image.title="Oxipng" +LABEL org.opencontainers.image.description="Multithreaded PNG optimizer written in Rust" +LABEL org.opencontainers.image.authors="Joshua Holmer " +LABEL org.opencontainers.image.licenses="MIT" +LABEL org.opencontainers.image.source="https://github.com/shssoichiro/oxipng" + +COPY --from=base /src/target/oxipng /usr/local/bin + +WORKDIR /work ENTRYPOINT [ "oxipng" ] CMD [ "--help" ]