-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.multi
More file actions
96 lines (74 loc) · 3.95 KB
/
Dockerfile.multi
File metadata and controls
96 lines (74 loc) · 3.95 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
FROM debian:bookworm-slim AS common
RUN apt-get update \
&& apt-get install -y ca-certificates \
&& rm -rf /var/lib/apt/lists/*
FROM --platform=$BUILDPLATFORM rust:1.91-bookworm AS prepare
RUN apt-get update && apt-get install -y \
gcc-aarch64-linux-gnu \
gcc-x86-64-linux-gnu \
libc6-dev-amd64-cross \
libc6-dev-arm64-cross \
qemu-user-static
ENV CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc
ENV CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
RUN rustup target add x86_64-unknown-linux-gnu \
aarch64-unknown-linux-gnu
RUN cargo install cargo-chef
FROM prepare AS planner
WORKDIR /build
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM prepare AS platform
ARG TARGETPLATFORM
ENV FEATURES=nats,database
ENV RUST_BACKTRACE=1
ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin
WORKDIR /build
RUN touch $RUNTIME_SNAPSHOT_PATH
COPY --from=planner /build/recipe.json recipe.json
RUN echo "Building for $TARGETPLATFORM with features $FEATURES"
RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
case "$TARGETPLATFORM" in \
"linux/arm64") cargo chef cook --release --features=$FEATURES --target aarch64-unknown-linux-gnu --recipe-path recipe.json ;; \
"linux/amd64") cargo chef cook --release --features=$FEATURES --target x86_64-unknown-linux-gnu --recipe-path recipe.json ;; \
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
esac
COPY . /build
# Build application for target platform
# 1. Build the snapshot generator for the TARGET platform
# 2. Run it (via QEMU emulation explicitly) to generate the snapshot.bin
# 3. Build the final executor which embeds the snapshot.bin
RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
case "$TARGETPLATFORM" in \
"linux/arm64") \
# Build snapshot generator for target \
cargo build --release --features=$FEATURES --bin snapshot --target aarch64-unknown-linux-gnu && \
# Generate snapshot (explicitly using qemu-aarch64-static with cross libs) \
QEMU_LD_PREFIX=/usr/aarch64-linux-gnu qemu-aarch64-static /build/target/aarch64-unknown-linux-gnu/release/snapshot && \
# Build executor \
cargo build --release --features=$FEATURES --target aarch64-unknown-linux-gnu && \
mv /build/target/aarch64-unknown-linux-gnu/release/task-executor /build/output ;; \
"linux/amd64") \
# Build snapshot generator for target \
cargo build --release --features=$FEATURES --bin snapshot --target x86_64-unknown-linux-gnu && \
# Generate snapshot (direct execution on x86_64, qemu on other archs) \
if [ "$(uname -m)" = "x86_64" ]; then \
/build/target/x86_64-unknown-linux-gnu/release/snapshot; \
else \
QEMU_LD_PREFIX=/usr/x86_64-linux-gnu qemu-x86_64-static /build/target/x86_64-unknown-linux-gnu/release/snapshot; \
fi && \
# Build executor \
cargo build --release --features=$FEATURES --target x86_64-unknown-linux-gnu && \
mv /build/target/x86_64-unknown-linux-gnu/release/task-executor /build/output ;; \
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
esac
FROM common
COPY --from=platform /build/output /usr/local/bin/task-executor
COPY --from=platform /build/snapshot.bin /build/snapshot.bin
ENTRYPOINT ["/usr/local/bin/task-executor"]