From cca9aa6318c88d61627c95cb04e4e79ec63a8a88 Mon Sep 17 00:00:00 2001 From: Maulana Sodiqin Date: Mon, 26 May 2025 23:41:00 +0700 Subject: [PATCH] chore: optimize dockerfile --- Dockerfile | 45 +++++++++++++++++++++++++++------------------ README.md | 2 +- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7f5696c..528e944 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,29 @@ -FROM rust:1.85-slim-bullseye AS builder +FROM rust:1.85-alpine AS builder -RUN apt-get update && apt-get install -y \ +RUN apk add --no-cache \ curl \ - build-essential \ - libssl-dev \ - libssl1.1 \ - pkg-config \ - && rm -rf /var/lib/apt/lists/* + musl-dev \ + openssl-dev \ + openssl-libs-static \ + pkgconfig WORKDIR /app COPY Cargo.toml Cargo.lock ./ +RUN mkdir -p imphnen-backend/src imphnen-cms/src imphnen-dimentorin/src \ + imphnen-entities/src imphnen-gacha/src imphnen-gateway/src \ + imphnen-iam/src imphnen-libs/src imphnen-middleware/src \ + imphnen-utils/src tests/src && \ + echo "fn main() {}" > imphnen-backend/src/main.rs && \ + find . -name "src" -type d -exec sh -c 'echo "// dummy" > "$1/lib.rs"' _ {} \; + +RUN echo '[package]\nname = "tests"\nversion = "0.1.0"\nedition = "2021"' > tests/Cargo.toml + + +RUN echo -e '[package]\nname = "tests"\nversion = "0.1.0"\nedition = "2021"' > tests/Cargo.toml + + COPY imphnen-backend ./imphnen-backend COPY imphnen-cms ./imphnen-cms COPY imphnen-dimentorin ./imphnen-dimentorin @@ -24,15 +36,12 @@ COPY imphnen-middleware ./imphnen-middleware COPY imphnen-utils ./imphnen-utils COPY tests ./tests -RUN cargo fetch +RUN RUSTFLAGS="-C target-cpu=generic -C opt-level=s -C panic=abort -C codegen-units=1 -C strip=symbols" \ + cargo build -p imphnen-backend --release && \ + strip target/release/api && \ + upx --best --lzma target/release/api 2>/dev/null || true -RUN cargo build -p imphnen-backend --release \ - && strip target/release/api - -FROM gcr.io/distroless/cc AS runner - -WORKDIR /app - -COPY --from=builder /app/target/release/api . - -CMD ["/app/api"] +FROM scratch AS runner +COPY --from=builder /app/target/release/api /api +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +ENTRYPOINT ["/api"] \ No newline at end of file diff --git a/README.md b/README.md index a0262e9..2b7e510 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ For production deployment: 2. **Run the Docker container**: ```sh - docker run -d --env-file .env -p 3000:3000 imphnen-backend + docker run --name imphnen-backend -d --env-file .env -p 3000:3000 imphnen-backend ``` Adjust the port and environment variables as needed.