Dockerfile: Switch to Debian-based images and simplify build command

- rust:1.80-alpine replaced with rust:1.80 for the builder stage
- alpine:3.20 replaced with debian:12.6-slim for the user-stage
- Build command simplified to use default target architecture
This commit is contained in:
perennial 2024-08-14 10:04:35 +10:00
parent 1329745627
commit ff9a378564
No known key found for this signature in database
GPG Key ID: 826BC6E6B83E08E7

View File

@ -1,39 +1,31 @@
# Use the official Alpine-based Rust image as a parent image
FROM rust:1.80-alpine AS builder
# Use the official Rust image as a parent image
FROM rust:1.80 AS builder
# Set the working directory in the container
WORKDIR /usr/src/app
# Install build dependencies
RUN apk add --no-cache \
musl-dev \
openssl-dev \
openssl-libs-static \
pkgconfig \
patch
# Set environment variables for static linking
ENV OPENSSL_STATIC=yes
ENV OPENSSL_DIR=/usr
RUN apt update && apt install -y \
libssl-dev \
pkg-config \
&& rm -rf /var/lib/apt/lists/*
# Copy the current directory contents into the container
COPY . .
# Determine the target architecture and build the application
RUN RUST_TARGET=$(rustc -vV | sed -n 's/host: //p') && \
rustup target add $RUST_TARGET && \
RUSTFLAGS='-C target-feature=+crt-static' cargo build --release --target $RUST_TARGET
# Build the application
RUN cargo build --release
# Stage for creating the non-privileged user
FROM alpine:3.20 AS user-stage
FROM debian:12.6-slim AS user-stage
RUN adduser -u 10001 -S appuser
RUN adduser --uid 10001 --system appuser
# Stage for a smaller final image
FROM scratch
# Copy necessary files from the builder stage, using the correct architecture path
COPY --from=builder /usr/src/app/target/*/release/inv_sig_helper_rust /app/inv_sig_helper_rust
# Copy necessary files from the builder stage
COPY --from=builder /usr/src/app/target/release/inv_sig_helper_rust /app/inv_sig_helper_rust
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Copy passwd file for the non-privileged user from the user-stage