1- # ##########################
2- # Stage 1 : Backend build #
3- # ##########################
1+ # syntax=docker/dockerfile:1
42
5- # Versions
6- ARG RUST_VERSION
7- ARG FLUTTER_VERSION
3+ # Default versions (can be overridden with --build-arg)
4+ ARG RUST_VERSION=1.93
5+ ARG FLUTTER_VERSION=3.41.2
86
9- # Set up an environnement to cross-compile the app for musl to create a statically-linked binary
10- FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-trixie AS backend-builder
7+ # --- Frontend Builder ---
8+ # Use the Flutter image to build the web assets
9+ FROM --platform=$BUILDPLATFORM ghcr.io/cirruslabs/flutter:${FLUTTER_VERSION} AS frontend-builder
10+ WORKDIR /src
11+ # Build the Flutter web app
12+ COPY --exclude=.dart_tool frontend .
13+ RUN flutter build web --release
14+ # Add the static web assets
15+ COPY --exclude=index.html backend/web/. build/web/
16+ # Gzip assets for the backend to serve gzipped files
17+ RUN find build/web -type f ! -name "*.gz" ! -name "*.tmpl" -exec gzip -9 {} +
18+
19+ # --- Backend Builder ---
20+ FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-bookworm AS backend-builder
1121ARG TARGETPLATFORM
12- RUN case "$TARGETPLATFORM" in \
13- "linux/amd64" ) echo x86_64-unknown-linux-gnu > /rust_target.txt ;; \
14- "linux/arm64" ) echo aarch64-unknown-linux-gnu > /rust_target.txt ;; \
15- "linux/arm/v7" ) echo armv7-unknown-linux-gnueabihf > /rust_target.txt ;; \
16- "linux/arm/v6" ) wget https://github.com/cross-tools/musl-cross/releases/download/20250929/arm-unknown-linux-musleabihf.tar.xz -O - | tar -xJf - -C /opt && echo arm-unknown-linux-musleabihf > /rust_target.txt ;; \
17- *) exit 1 ;; \
18- esac
19- ENV PATH="/opt/arm-unknown-linux-musleabihf/bin:$PATH"
20- RUN rustup target add $(cat /rust_target.txt)
21- RUN apt update && apt install -y clang cmake gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf libc6-dev-i386 libcap2-bin libclang-dev musl-dev musl-tools
22+
23+ # Install cross-compilation dependencies
24+ RUN apt-get update && apt-get install -y \
25+ clang \
26+ cmake \
27+ gcc-aarch64-linux-gnu \
28+ gcc-arm-linux-gnueabihf \
29+ libc6-dev-i386 \
30+ libcap2-bin \
31+ libclang-dev \
32+ musl-dev \
33+ musl-tools \
34+ wget \
35+ xz-utils \
36+ && rm -rf /var/lib/apt/lists/*
37+
2238RUN ln -s /usr/include/asm-generic /usr/include/asm
2339
40+ # Handle armv6 musl toolchain if needed
41+ RUN if [ "$TARGETPLATFORM" = "linux/arm/v6" ]; then \
42+ wget https://github.com/cross-tools/musl-cross/releases/download/20250929/arm-unknown-linux-musleabihf.tar.xz -O - | tar -xJf - -C /opt; \
43+ fi
44+ ENV PATH="/opt/arm-unknown-linux-musleabihf/bin:$PATH"
45+
46+ WORKDIR /app
47+
48+ # Determine the rust target
49+ RUN case "$TARGETPLATFORM" in \
50+ "linux/amd64" ) RUST_TARGET="x86_64-unknown-linux-gnu" ;; \
51+ "linux/arm64" ) RUST_TARGET="aarch64-unknown-linux-gnu" ;; \
52+ "linux/arm/v7" ) RUST_TARGET="armv7-unknown-linux-gnueabihf" ;; \
53+ "linux/arm/v6" ) RUST_TARGET="arm-unknown-linux-musleabihf" ;; \
54+ *) RUST_TARGET="x86_64-unknown-linux-gnu" ;; \
55+ esac; \
56+ echo "$RUST_TARGET" > /rust_target_name && \
57+ rustup target add "$RUST_TARGET"
58+
59+ # Copy frontend assets from frontend-builder
60+ # We copy them to backend/dist because backend/src/web.rs uses #[folder = "dist/"]
61+ COPY --from=frontend-builder /src/build/web ./backend/dist
62+
63+ # Copy backend source
64+ COPY backend/Cargo.toml ./backend/
65+ COPY backend/.cargo ./backend/.cargo
66+ COPY backend/src ./backend/src
67+
68+ WORKDIR /app/backend
69+ RUN RUST_TARGET=$(cat /rust_target_name) && \
70+ cargo build --profile release_optimized --target "$RUST_TARGET" && \
71+ cp target/"$RUST_TARGET" /release_optimized/atrium /atrium
72+
73+ # --- Binary Exporter ---
74+ # This stage is used to extract the binary locally
75+ FROM scratch AS binary-exporter
76+ ARG TARGETARCH
77+ ARG TARGETVARIANT
78+ COPY --from=backend-builder /atrium /atrium-${TARGETARCH}${TARGETVARIANT}
79+
80+ # --- Final Prep ---
81+ # Prepare the binary and environment in a temporary stage
82+ FROM --platform=$BUILDPLATFORM debian:trixie-slim AS prep
83+ ARG TARGETPLATFORM
84+ RUN apt-get update && apt-get install -y adduser libcap2-bin && rm -rf /var/lib/apt/lists/*
2485# Create appuser
2586ENV USER=appuser
2687ENV UID=1000
@@ -33,54 +94,25 @@ RUN adduser \
3394 --uid "${UID}" \
3495 "${USER}"
3596
36- WORKDIR /build
37-
38- COPY ./backend/.cargo ./.cargo
39- COPY ./backend/Cargo.toml ./
40- COPY ./backend/src ./src
41- COPY ./backend/tests ./tests
42-
43- # RUN cargo test --release --target $(cat /rust_target.txt)
44- RUN cargo build --profile release_optimized --target $(cat /rust_target.txt)
45- RUN cp target/$(cat /rust_target.txt)/release_optimized/atrium .
46- RUN chown -f "${UID}" :"${UID}" ./atrium
47- # Allow running on ports < 1000
48- RUN setcap cap_net_bind_service=+ep ./atrium
49-
5097RUN mkdir -p /myapp/app
51- COPY ./backend/atrium.yaml /myapp/app
52- COPY ./backend/web/onlyoffice/ /myapp/app/web/onlyoffice/
53- COPY ./backend/web/oauth2/ /myapp/app/web/oauth2/
54- RUN chown -Rf "${UID}" :"${UID}" /myapp
55-
56- # ###########################
57- # Stage 2 : Frontend build #
58- # ###########################
59-
60- FROM --platform=$BUILDPLATFORM ghcr.io/cirruslabs/flutter:${FLUTTER_VERSION} AS frontend-builder
61- WORKDIR /build
62- COPY ./frontend .
63- RUN flutter pub get
64- RUN flutter build web
65-
66- # ########################
67- # Stage 3 : Final image #
68- # ########################
98+ COPY --from=backend-builder /atrium /myapp/app/atrium
99+ RUN chown -Rf "${UID}" :"${UID}" /myapp/app/
100+ # Allow running on ports < 1000
101+ RUN setcap cap_net_bind_service=+ep /myapp/app/atrium
69102
103+ # --- Final Image ---
70104FROM --platform=linux/amd64 gcr.io/distroless/cc-debian13 AS base-amd64
71105FROM --platform=linux/arm64 gcr.io/distroless/cc-debian13 AS base-arm64
72106FROM --platform=linux/arm/v7 gcr.io/distroless/cc-debian13 AS base-armv7
73107FROM --platform=linux/arm/v6 scratch AS base-armv6
74108
109+ ARG TARGETARCH
110+ ARG TARGETVARIANT
75111FROM base-${TARGETARCH}${TARGETVARIANT}
112+ COPY --from=prep /etc/passwd /etc/passwd
113+ COPY --from=prep /etc/group /etc/group
114+ COPY --from=prep --chown=appuser:appuser /myapp /
76115
77- COPY --from=backend-builder /etc/passwd /etc/passwd
78- COPY --from=backend-builder /etc/group /etc/group
79-
80- COPY --from=backend-builder /myapp /
81116WORKDIR /app
82- COPY --from=backend-builder /build/atrium ./
83- COPY --chown=appuser:appuser --from=frontend-builder /build/build/web/ /app/web/
84-
85117USER appuser:appuser
86- ENTRYPOINT ["./atrium" ]
118+ ENTRYPOINT ["./atrium" ]
0 commit comments