From afef29aa5410b1250aa1c3656ffd482792f06fbd Mon Sep 17 00:00:00 2001 From: caipira113 Date: Wed, 20 Mar 2024 00:20:30 +0900 Subject: [PATCH] static docker image --- Dockerfile | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 68ba6ec..ed4f648 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,14 +10,27 @@ RUN cargo chef prepare --recipe-path recipe.json FROM base AS builder +ARG TARGETPLATFORM +ARG RUSTFLAGS='-C target-feature=+crt-static' + COPY --from=planner /app/recipe.json ./ -RUN cargo chef cook --release --recipe-path recipe.json +RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then TARGET=x86_64-unknown-linux-gnu; elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then TARGET=aarch64-unknown-linux-gnu; fi \ + && cargo chef cook --release --target $TARGET --recipe-path recipe.json COPY . . -RUN cargo build --release +RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then TARGET=x86_64-unknown-linux-gnu; elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then TARGET=aarch64-unknown-linux-gnu; fi \ + && cargo build --release --target $TARGET \ + && cp -r target/$TARGET/release/conex target/release/conex + +FROM alpine:latest AS packer + +RUN apk add --no-cache upx + +COPY --from=builder /app/target/release/conex /app/conex +RUN upx --brute /app/conex -FROM gcr.io/distroless/cc-debian12:nonroot AS runtime +FROM gcr.io/distroless/static-debian12:nonroot AS runtime WORKDIR /app -COPY --from=builder /app/target/release/conex ./ +COPY --from=packer /app/conex ./ CMD ["./conex"] \ No newline at end of file