-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.chef
More file actions
70 lines (55 loc) · 2.04 KB
/
Copy pathDockerfile.chef
File metadata and controls
70 lines (55 loc) · 2.04 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Optimized Dockerfile using cargo-chef for faster incremental builds
# This dramatically reduces build times by caching dependencies separately from source code
# Chef stage - prepare the recipe
FROM rust:slim-bookworm AS chef
RUN cargo install cargo-chef --version 0.1.70
WORKDIR /app
# Planner stage - generate recipe.json
FROM chef AS planner
COPY Cargo.toml Cargo.lock ./
COPY src ./src
RUN cargo chef prepare --recipe-path recipe.json
# Builder stage - build dependencies first, then app
FROM chef AS builder
ENV RUSTFLAGS="-C target-cpu=native"
# Install build dependencies
RUN apt-get update && apt-get install -y \
pkg-config \
libssl-dev \
curl \
&& rm -rf /var/lib/apt/lists/*
# Copy recipe and build dependencies
# This layer is cached unless Cargo.toml or Cargo.lock change
COPY --from=planner /app/recipe.json recipe.json
# Build dependencies (this is the slow part, but gets cached)
RUN cargo chef cook --release --recipe-path recipe.json
# Copy source code (changes frequently, but dependencies are already built)
COPY Cargo.toml Cargo.lock ./
COPY src ./src
COPY migrations ./migrations
COPY templates ./templates
COPY dashboard ./dashboard
# Build the application (fast because dependencies are cached)
ARG CARGO_BUILD_JOBS=4
ARG ENABLE_LTO=true
RUN if [ "$ENABLE_LTO" = "false" ]; then \
sed -i 's/lto = true/lto = false/' Cargo.toml && \
sed -i 's/codegen-units = 1/codegen-units = 16/' Cargo.toml; \
fi && \
cargo build --release --bin rust-true -j $CARGO_BUILD_JOBS
# Runtime stage - using Google's distroless image for minimal attack surface
FROM gcr.io/distroless/cc-debian12
# Create app directory
WORKDIR /app1
# Copy binary from builder
COPY --from=builder /app/target/release/rust-true /app/rust-true
# Copy migrations and templates
COPY --from=builder /app/migrations /app/migrations
COPY --from=builder /app/templates /app/templates
COPY --from=builder /app/dashboard /app/dashboard
# Expose port
EXPOSE 8080
# Run the application as non-root
USER nonroot:nonroot
# Run the application
CMD ["/app/rust-true"]