Skip to content

Commit 79257c1

Browse files
committed
Add cargo-chef to docker files
1 parent 41cbec2 commit 79257c1

4 files changed

Lines changed: 55 additions & 13 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ once_cell = "1.19"
1616
env_logger = "0.11.6"
1717
http_v02 = { package = "http", version = "0.2.12" }
1818
sqlx = { version = "0.8.3", features = [ "runtime-tokio", "postgres", "uuid", "bigdecimal", "rust_decimal" ] }
19-
# openworkers-runtime ={ git = "https://github.com/openworkers/openworkers-runtime", tag = "v0.1.10"}
20-
openworkers-runtime = { path = "../openworkers-runtime" }
19+
openworkers-runtime ={ git = "https://github.com/openworkers/openworkers-runtime", tag = "v0.1.11"}
20+
# openworkers-runtime = { path = "../openworkers-runtime" }
2121
async-nats = "0.37"
2222
futures = "0.3"
2323
serde_json = "1.0.137"

Dockerfile

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
FROM rust:1.84 AS builder
1+
FROM rust:1.91 AS chef
2+
RUN cargo install cargo-chef
3+
WORKDIR /build
24

3-
RUN mkdir -p /build
5+
FROM chef AS planner
6+
COPY . .
7+
RUN cargo chef prepare --recipe-path recipe.json
48

9+
FROM chef AS builder
510
ENV RUST_BACKTRACE=1
611
ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin
712

8-
WORKDIR /build
13+
RUN touch $RUNTIME_SNAPSHOT_PATH
14+
15+
COPY --from=planner /build/recipe.json recipe.json
16+
# Build dependencies - this is the caching Docker layer!
17+
RUN --mount=type=cache,target=$CARGO_HOME/git \
18+
--mount=type=cache,target=$CARGO_HOME/registry \
19+
--mount=type=cache,target=/build/target \
20+
cargo chef cook --release --recipe-path recipe.json
921

10-
COPY . /build
22+
# Build application
23+
COPY . .
1124

1225
RUN touch $RUNTIME_SNAPSHOT_PATH
1326

Dockerfile.multi

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RUN apt-get update \
55
&& apt-get install -y ca-certificates wget \
66
&& rm -rf /var/lib/apt/lists/*
77

8-
FROM --platform=$BUILDPLATFORM rust:1.84 AS prepare
8+
FROM --platform=$BUILDPLATFORM rust:1.91 AS prepare
99

1010
RUN apt-get update && apt-get install -y \
1111
gcc-aarch64-linux-gnu \
@@ -22,6 +22,14 @@ ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
2222
RUN rustup target add x86_64-unknown-linux-gnu \
2323
aarch64-unknown-linux-gnu
2424

25+
RUN cargo install cargo-chef
26+
27+
FROM prepare AS planner
28+
29+
WORKDIR /build
30+
COPY . .
31+
RUN cargo chef prepare --recipe-path recipe.json
32+
2533
FROM prepare AS builder
2634

2735
RUN mkdir -p /build
@@ -31,13 +39,22 @@ ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin
3139

3240
WORKDIR /build
3341

34-
COPY . /build
35-
3642
RUN touch $RUNTIME_SNAPSHOT_PATH
3743

44+
COPY --from=planner /build/recipe.json recipe.json
45+
46+
# Build dependencies - this is the caching Docker layer!
47+
RUN --mount=type=cache,sharing=locked,target=$CARGO_HOME/git \
48+
--mount=type=cache,sharing=locked,target=$CARGO_HOME/registry \
49+
--mount=type=cache,sharing=locked,target=/build/target \
50+
cargo chef cook --release --recipe-path recipe.json
51+
52+
# Build application
53+
COPY . /build
54+
3855
RUN --mount=type=cache,sharing=locked,target=$CARGO_HOME/git \
3956
--mount=type=cache,sharing=locked,target=$CARGO_HOME/registry \
40-
--mount=type=cache,sharing=locked,target=/build/openworkers-runner/target \
57+
--mount=type=cache,sharing=locked,target=/build/target \
4158
cargo run --release --bin snapshot
4259

4360
FROM builder AS platform
@@ -46,9 +63,20 @@ ARG TARGETPLATFORM
4663

4764
RUN echo "Building for $TARGETPLATFORM"
4865

49-
RUN --mount=type=cache,id=apt-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
50-
--mount=type=cache,id=apt-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
51-
--mount=type=cache,id=apt-$TARGETPLATFORM,sharing=locked,target=/build/openworkers-runner/target \
66+
# Build dependencies for target platform
67+
RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
68+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
69+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
70+
case "$TARGETPLATFORM" in \
71+
"linux/arm64") cargo chef cook --release --target aarch64-unknown-linux-gnu --recipe-path recipe.json ;; \
72+
"linux/amd64") cargo chef cook --release --target x86_64-unknown-linux-gnu --recipe-path recipe.json ;; \
73+
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
74+
esac
75+
76+
# Build application for target platform
77+
RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
78+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
79+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
5280
case "$TARGETPLATFORM" in \
5381
"linux/arm64") cargo build --release --target aarch64-unknown-linux-gnu && \
5482
mv /build/target/aarch64-unknown-linux-gnu/release/openworkers-runner /build/output ;; \

0 commit comments

Comments
 (0)