build: update dockerfile

support rootless and cache cargo build for faster builds
This commit is contained in:
Alexander Navarro 2025-03-14 16:54:38 -03:00
parent 3c2d3cf720
commit 29756b9578

View file

@ -1,19 +1,19 @@
# ── Javascript ────────────────────────────────────────────────────────── # ── Javascript ──────────────────────────────────────────────────────────
FROM oven/bun:1 AS base-js FROM oven/bun:1 AS bun
WORKDIR /usr/src/app WORKDIR /usr/src/app
# install dependencies into temp directory # install dependencies into temp directory
# this will cache them and speed up future builds # this will cache them and speed up future builds
FROM base-js AS install FROM bun AS js-deps
COPY package.json bun.lock /temp/dev/ COPY package.json bun.lock /temp/dev/
RUN cd /temp/dev && bun install --frozen-lockfile RUN cd /temp/dev && bun install --frozen-lockfile
# copy node_modules from temp directory # copy node_modules from temp directory
# then copy all (non-ignored) project files into the image # then copy all (non-ignored) project files into the image
FROM base-js AS prerelease FROM bun AS js-bundler
COPY --from=install /temp/dev/node_modules node_modules COPY --from=js-deps /temp/dev/node_modules node_modules
COPY frontend frontend COPY frontend frontend
COPY .devfiles/scripts/build-frontend.ts .devfiles/scripts/build-frontend.ts COPY .devfiles/scripts/build-frontend.ts .devfiles/scripts/build-frontend.ts
@ -22,22 +22,43 @@ RUN bun ./.devfiles/scripts/build-frontend.ts
# ── Rust ──────────────────────────────────────────────────────────────── # ── Rust ────────────────────────────────────────────────────────────────
FROM rust:slim AS base-rust FROM rust:slim AS builder
WORKDIR /app WORKDIR /app
# Create appuser in builder as the scratch image can't do it
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid 10001 \
appuser
COPY --from=js-bundler /usr/src/app/dist dist
COPY . . COPY . .
COPY --from=prerelease /usr/src/app/dist dist
RUN cargo build --locked --release RUN --mount=type=cache,target=/app/target/ \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/usr/local/cargo/registry/ \
cargo build --locked --release \
&& mv target/release/compendium /tmp/compendium
FROM scratch AS runner FROM gcr.io/distroless/cc AS runner
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
ENV RUST_LOG="compendium=debug,info" ENV RUST_LOG="compendium=debug,info"
COPY --from=base-rust /app/target/release/compendium / WORKDIR /app
COPY --from=builder --chown=appuser:appuser /tmp/compendium /app/compendium
EXPOSE 3000 EXPOSE 3000
ENTRYPOINT ["./compendium"] USER appuser:appuser
CMD ["./compendium"]
CMD ["/app/compendium"]