Dockerfile templates for rust projects

This commit is contained in:
Fabian Schmidt
2024-09-04 09:19:15 +02:00
commit 01e72652b8
8 changed files with 181 additions and 0 deletions

47
docker/scratch.Dockerfile Executable file
View File

@@ -0,0 +1,47 @@
####################################################################################################
## Builder
####################################################################################################
FROM rust:latest AS builder
RUN rustup target add x86_64-unknown-linux-musl
RUN apt update && apt install -y musl-tools musl-dev
RUN update-ca-certificates
# Create appuser
ENV USER=small-rust
ENV UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"
WORKDIR /app
COPY ./ .
RUN cargo build --target x86_64-unknown-linux-musl --release
####################################################################################################
## Final image
####################################################################################################
FROM scratch
# Import from builder.
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
WORKDIR /app
# Copy our build
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/small-rust ./
# Use an unprivileged user.
USER small-rust:small-rust
CMD ["/app/small-rust"]