-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile.alpine
45 lines (37 loc) · 1.42 KB
/
Dockerfile.alpine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
FROM lukemathwalker/cargo-chef:latest-rust-alpine AS base
FROM base AS chef
WORKDIR /app
RUN apk add pkgconfig libressl-dev --no-cache
# Build only dependencies to speed up subsequent builds
ADD Cargo.toml Cargo.lock ./
RUN mkdir -p src
RUN echo "fn main() {}" > src/main.rs
RUN cargo fetch
FROM chef AS planner
COPY src src
RUN cargo chef prepare --recipe-path recipe.json
FROM chef AS cooker
COPY --from=planner /app/recipe.json recipe.json
# Cache the whole release target here
RUN cargo chef cook --release --recipe-path recipe.json
FROM cooker AS builder
# Build application
COPY src src
RUN cat src/main.rs > temp && mv temp src/main.r
RUN echo "Size of binary = $(echo "$(stat -c%s 'target/release/main')/1024000" | bc -l) MB."
RUN strip target/release/mains
RUN cargo build --release --bin main
RUN echo "Size of stripped binary = $(echo "$(stat -c%s 'target/release/main')/1024000" | bc -l) MB."
RUN readelf -p .comment target/release/main
# Add non-root user
FROM base AS user
RUN addgroup -S rootless && adduser -S rootless -G rootless
RUN echo "Size of /etc/passwd = $(echo "$(stat -c%s '/etc/passwd')/1024000" | bc -l) MB."
RUN echo "Size of /etc/ssl = $(echo "$(stat -c%s '/etc/ssl')/1024000" | bc -l) MB."
# We do not need the Rust toolchain to run the binary!
FROM scratch
COPY --from=base /etc/ssl /etc/ssl
COPY --from=user /etc/passwd /etc/passwd
COPY --from=builder /app/target/release/main /
USER rootless
ENTRYPOINT ["/main"]