Skip to content

Commit 93ac9eb

Browse files
authored
Use cargo-chef for better Docker layer caching (#1923)
Easier to maintain than the previous hack :)
2 parents 08a163a + 2e7c74f commit 93ac9eb

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

server/Dockerfile

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
# Base build
2-
FROM rust:1.86-slim-bookworm AS build
1+
# Using https://github.com/LukeMathWalker/cargo-chef for better layer caching
2+
3+
# Base image for planner and build - keep in sync with .github/workflows/server-ci.yml
4+
FROM docker.io/rust:1.86-slim-bookworm AS chef
5+
RUN cargo install cargo-chef
6+
WORKDIR /app
7+
8+
# Build plan environment
9+
FROM chef AS planner
10+
COPY . .
11+
RUN cargo chef prepare --recipe-path recipe.json
12+
13+
# Build environment
14+
FROM chef AS build
315

416
RUN apt-get update && apt-get install -y \
517
build-essential=12.* \
@@ -16,24 +28,17 @@ RUN set -ex ; \
1628
mkdir -p /home/appuser ;\
1729
chown -R appuser: /home/appuser
1830

19-
WORKDIR /app
31+
COPY --from=planner /app/recipe.json recipe.json
2032

21-
# Hack to enable docker caching
22-
COPY Cargo.toml .
23-
COPY Cargo.lock .
24-
COPY svix-server_derive svix-server_derive
25-
COPY svix-server/Cargo.toml svix-server/
26-
RUN set -ex ;\
27-
mkdir svix-server/src ;\
28-
echo 'fn main() { println!("Dummy!"); }' > svix-server/src/main.rs ;\
29-
cargo build --release ;\
30-
rm -rf svix-server/src
33+
# Build dependencies - this is the caching Docker layer
34+
RUN cargo chef cook --release --package svix-server --bin svix-server --recipe-path recipe.json
3135

36+
# Build the server
3237
COPY . .
33-
RUN cargo build --release --frozen
38+
RUN cargo build --release --package svix-server --bin svix-server --frozen
3439

3540
# Production
36-
FROM debian:bookworm-slim AS prod
41+
FROM docker.io/debian:bookworm-slim AS prod
3742

3843
RUN set -ex ; \
3944
mkdir -p /app ;\

0 commit comments

Comments
 (0)