Skip to content

Instantly share code, notes, and snippets.

@edr3x
Created July 10, 2023 20:06
Show Gist options
  • Save edr3x/89d4ebdd548bc005a57916e8dcf0b268 to your computer and use it in GitHub Desktop.
Save edr3x/89d4ebdd548bc005a57916e8dcf0b268 to your computer and use it in GitHub Desktop.
dockerfile for containerizing rust
FROM rust as planner

WORKDIR /app

RUN cargo install cargo-chef

COPY . .

RUN cargo chef prepare --recipe-path recipe.json

FROM rust as cacher

WORKDIR /app

RUN cargo install cargo-chef

COPY --from=planner /app/recipe.json recipe.json

RUN cargo chef cook --release --recipe-path recipe.json

FROM rust AS builder

ENV USER=webuser
ENV UID=6969

RUN adduser \
  --disabled-password \
  --gecos "" \
  --home "/nonexistent" \
  --shell "/sbin/nologin" \
  --no-create-home \
  --uid "${UID}" \
  "${USER}"

COPY . /app

WORKDIR /app

COPY --from=cacher /app/target target

RUN cargo build --release

FROM gcr.io/distroless/cc-debian11

COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group

COPY --from=builder /app/target/release/docker-rust /app/docker-rust

WORKDIR /app

CMD ["./docker-rust"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment