Skip to content

Commit bcd1ba5

Browse files
committed
Refactor Dockerfile to improve build process by introducing multi-stage builds with cargo-chef for dependency caching, updating Rust toolchain installation, and organizing stages for better clarity.
1 parent fb83540 commit bcd1ba5

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

Dockerfile

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ RUN echo '<?xml version="1.0"?>' > /opt/chromium/fonts.conf && \
4545
RUN mkdir -p /tmp/chrome-data /tmp/chrome-cache /tmp/chrome-user-data /tmp/.config /tmp/.cache /tmp/.local/share /tmp/.fontconfig \
4646
&& chmod -R 755 /tmp/chrome-data /tmp/chrome-cache /tmp/chrome-user-data /tmp/.config /tmp/.cache /tmp/.local /tmp/.fontconfig
4747

48-
# 2) Compile your Rust binary in a builder stage
49-
FROM base AS builder
48+
# 2) Set up Rust toolchain and cargo-chef
49+
FROM base AS chef
5050

5151
# Install Zig, which is required by cargo-lambda
5252
ENV ZIG_VERSION=0.13.0
@@ -56,19 +56,29 @@ RUN curl -L "https://ziglang.org/download/${ZIG_VERSION}/zig-linux-x86_64-${ZIG_
5656
rm /tmp/zig.tar.xz
5757
ENV PATH="/usr/local/zig:${PATH}"
5858

59-
# Install Rust & cargo-lambda
59+
# Install Rust & cargo tools
6060
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
6161
ENV PATH="/root/.cargo/bin:${PATH}"
62-
RUN cargo install cargo-lambda
62+
RUN cargo install cargo-chef cargo-lambda
6363

64-
# Copy your source code
6564
WORKDIR /src
65+
66+
# 3) Prepare the recipe file
67+
FROM chef AS planner
6668
COPY . .
69+
RUN cargo chef prepare --recipe-path recipe.json
70+
71+
# 4) Build dependencies (this layer will be cached)
72+
FROM chef AS builder
73+
COPY --from=planner /src/recipe.json recipe.json
74+
# Build dependencies - this is the caching Docker layer!
75+
RUN cargo chef cook --release --recipe-path recipe.json --target x86_64-unknown-linux-gnu
6776

68-
# Build for x86_64
77+
# Build application
78+
COPY . .
6979
RUN cargo lambda build --release --bin screenshotapi
7080

71-
# 3) Final image: copy everything together
81+
# 5) Final image: copy everything together
7282
FROM base
7383

7484
# Copy the built binary from the builder stage

0 commit comments

Comments
 (0)