Skip to content

Commit 6b94718

Browse files
committed
Revert "Dockerfile: Switch to Debian-based images and simplify build command"
This reverts commit ff9a378. Reasons for reverting: 1. Compiling via musl is necessary to statically link dependencies and create a truly standalone Rust binary. [1] 2. Alpine-based Rust images are required for the build stage because such systems support dynamic linking, which is also needed for statically-linked binaries. [2] 3. Determining the target architecture and templating the correct value for the --target flag is necessary for the statically-linked binary to be built correctly. [2] [1]: https://doc.rust-lang.org/1.13.0/book/advanced-linking.html#linux [2]: rust-lang/rust#40174 (comment)
1 parent ca4e3f8 commit 6b94718

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

Dockerfile

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
1-
# Use the official Rust image as a parent image
2-
FROM rust:1.80 AS builder
1+
# Use the official Alpine-based Rust image as a parent image
2+
FROM rust:1.80-alpine AS builder
33

44
# Set the working directory in the container
55
WORKDIR /usr/src/app
66

77
# Install build dependencies
8-
RUN apt update && apt install -y \
9-
libssl-dev \
10-
pkg-config \
11-
&& rm -rf /var/lib/apt/lists/*
8+
RUN apk add --no-cache \
9+
musl-dev \
10+
openssl-dev \
11+
openssl-libs-static \
12+
pkgconfig \
13+
patch
14+
15+
# Set environment variables for static linking
16+
ENV OPENSSL_STATIC=yes
17+
ENV OPENSSL_DIR=/usr
1218

1319
# Copy the current directory contents into the container
1420
COPY . .
1521

16-
# Build the application
17-
RUN cargo build --release
22+
# Determine the target architecture and build the application
23+
RUN RUST_TARGET=$(rustc -vV | sed -n 's/host: //p') && \
24+
rustup target add $RUST_TARGET && \
25+
RUSTFLAGS='-C target-feature=+crt-static' cargo build --release --target $RUST_TARGET
1826

1927
# Stage for creating the non-privileged user
20-
FROM debian:12.6-slim AS user-stage
28+
FROM alpine:3.20 AS user-stage
2129

22-
RUN adduser --uid 10001 --system appuser
30+
RUN adduser -u 10001 -S appuser
2331

2432
# Stage for a smaller final image
2533
FROM scratch
2634

27-
# Copy necessary files from the builder stage
28-
COPY --from=builder /usr/src/app/target/release/inv_sig_helper_rust /app/inv_sig_helper_rust
35+
# Copy necessary files from the builder stage, using the correct architecture path
36+
COPY --from=builder /usr/src/app/target/*/release/inv_sig_helper_rust /app/inv_sig_helper_rust
2937
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
3038

3139
# Copy passwd file for the non-privileged user from the user-stage

0 commit comments

Comments
 (0)