1 # FIXME: use "--platform=$BUILDPLATFORM" and solve openssl cross-compile issue
2 FROM rust:1.67.0-alpine as builder
4 # Install build dependencies
5 RUN apk add --no-cache git openssl-dev libpq-dev musl-dev
7 # Set the working directory to /app and copy the source code
11 # Set the target architecture (can be set using --build-arg), buildx set it automatically
14 # This can be set to release using --build-arg
15 ARG RUST_RELEASE_MODE="debug"
18 # Docker and Rust use different architecture naming schemas, so we have to convert them
19 RUN case $TARGETARCH in \
20 arm64) RUSTARCH=aarch64 ;; \
21 amd64) RUSTARCH=x86_64 ;; \
22 *) echo "unsupported architecture: $TARGETARCH"; exit 3 ;; \
24 && echo "RUSTARCH=$RUSTARCH" >> .buildenv
27 RUN --mount=type=cache,target=/app/target \
28 if [ "$RUST_RELEASE_MODE" = "debug" ]; then \
30 && echo "pub const VERSION: &str = \"$(git describe --tag)\";" > "crates/utils/src/version.rs" \
31 && rustup target add ${RUSTARCH}-unknown-linux-musl \
32 && cargo build --target ${RUSTARCH}-unknown-linux-musl \
33 && cp ./target/${RUSTARCH}-unknown-linux-musl/${RUST_RELEASE_MODE}/lemmy_server /app/lemmy_server; \
38 if [ "$RUST_RELEASE_MODE" = "release" ]; then \
40 && rustup target add ${RUSTARCH}-unknown-linux-musl \
41 && cargo build --target ${RUSTARCH}-unknown-linux-musl --release \
42 && cp ./target/${RUSTARCH}-unknown-linux-musl/${RUST_RELEASE_MODE}/lemmy_server /app/lemmy_server; \
46 FROM alpine:3 as lemmy
48 # Install libpq for Postgres
49 RUN apk add --no-cache ca-certificates libpq
52 COPY --from=builder /app/lemmy_server /app/lemmy