Add ARM64 Docker image support (#3810)

* Add ARM64 Docker image support

* Fix

* Fix Docker build

* Fix typo

* Use OpenSSL 3 and PostgreSQL Lib v15 in Docker builder

* Use simple comments in Dockerfile

* Add comment explaining Docker image cross build

* Move docker step above tests for testing

* Move docker build step back to normal position

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
This commit is contained in:
Ellie 2023-08-31 23:02:59 +10:00 committed by GitHub
parent 56e26fc3d4
commit bd3f39973f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 113 additions and 33 deletions

View File

@ -239,7 +239,7 @@ steps:
settings: settings:
repo: dessalines/lemmy repo: dessalines/lemmy
dockerfile: docker/Dockerfile dockerfile: docker/Dockerfile
platforms: linux/amd64 platforms: linux/amd64,linux/arm64
build_args: build_args:
- RUST_RELEASE_MODE=release - RUST_RELEASE_MODE=release
auto_tag: true auto_tag: true
@ -252,7 +252,7 @@ steps:
settings: settings:
repo: dessalines/lemmy repo: dessalines/lemmy
dockerfile: docker/Dockerfile dockerfile: docker/Dockerfile
platforms: linux/amd64 platforms: linux/amd64,linux/arm64
build_args: build_args:
- RUST_RELEASE_MODE=release - RUST_RELEASE_MODE=release
tag: dev tag: dev

View File

@ -1,45 +1,125 @@
FROM clux/muslrust:1.70.0 as builder #
WORKDIR /app # Docker multiarch image:
ARG CARGO_BUILD_TARGET=x86_64-unknown-linux-musl # We build the Lemmy binary for amd64 and arm64 in individual stages using the blackdex/rust-musl image (github.com/blackdex/rust-musl).
# This image uses musl-cross-make (github.com/richfelker/musl-cross-make) to build a musl cross compilation toolchain for the target
# architecture. It also includes pre-built static libraries such as libpq. These libraries can improve the compile time and eliminate
# the requirement for extra dependencies in the final image.
#
# During each build stage, we use the blackdex/rust-musl openssl 3 images and configure PQ_LIB_DIR=/usr/local/musl/pq15/lib to use
# libpq v15. We also ensure the installation of the Rust toolchain corresponding to the target architecture using:
# `rustup target add $TARGET-unknown-linux-musl`.
#
# comma-seperated list of features to enable ARG RUST_VERSION=1.71.0
ARG ALPINE_VERSION=3.18
ARG CARGO_BUILD_FEATURES=default ARG CARGO_BUILD_FEATURES=default
ARG RUST_RELEASE_MODE=debug
ARG UID=911
ARG GID=911
# This can be set to release using --build-arg # AMD64 builder base
ARG RUST_RELEASE_MODE="debug" FROM --platform=${BUILDPLATFORM} blackdex/rust-musl:x86_64-musl-stable-${RUST_VERSION}-openssl3 AS base-amd64
COPY . . ENV DEBIAN_FRONTEND=noninteractive
ENV CARGO_HOME=/root/.cargo
ENV PQ_LIB_DIR=/usr/local/musl/pq15/lib
# Build the project RUN apt update && apt install -y \
--no-install-recommends \
git
# Debug mode build RUN mkdir -pv "${CARGO_HOME}" && \
RUN --mount=type=cache,target=/app/target \ rustup set profile minimal && \
if [ "$RUST_RELEASE_MODE" = "debug" ] ; then \ rustup target add x86_64-unknown-linux-musl
echo "pub const VERSION: &str = \"$(git describe --tag)\";" > "crates/utils/src/version.rs" \
&& cargo build --target ${CARGO_BUILD_TARGET} --features ${CARGO_BUILD_FEATURES} \ # ARM64 builder base
&& cp ./target/$CARGO_BUILD_TARGET/$RUST_RELEASE_MODE/lemmy_server /app/lemmy_server; \ FROM --platform=${BUILDPLATFORM} blackdex/rust-musl:aarch64-musl-stable-${RUST_VERSION}-openssl3 AS base-arm64
ENV DEBIAN_FRONTEND=noninteractive
ENV CARGO_HOME=/root/.cargo
ENV PQ_LIB_DIR=/usr/local/musl/pq15/lib
RUN apt update && apt install -y \
--no-install-recommends \
git
RUN mkdir -pv "${CARGO_HOME}" && \
rustup set profile minimal && \
rustup target add aarch64-unknown-linux-musl
# AMD64 builder
FROM base-amd64 AS build-amd64
ARG CARGO_BUILD_FEATURES
ARG RUST_RELEASE_MODE
WORKDIR /lemmy
COPY . ./
# Debug build
RUN --mount=type=cache,target=/lemmy/target set -ex; \
if [ "${RUST_RELEASE_MODE}" = "debug" ]; then \
echo "pub const VERSION: &str = \"$(git describe --tag)\";" > crates/utils/src/version.rs; \
cargo build --target=x86_64-unknown-linux-musl --features "${CARGO_BUILD_FEATURES}"; \
mv target/x86_64-unknown-linux-musl/debug/lemmy_server ./lemmy; \
fi fi
# Release mode build # Release build
RUN \ RUN set -ex; \
if [ "$RUST_RELEASE_MODE" = "release" ] ; then \ if [ "${RUST_RELEASE_MODE}" = "release" ]; then \
echo "pub const VERSION: &str = \"$(git describe --tag)\";" > "crates/utils/src/version.rs" \ echo "pub const VERSION: &str = \"$(git describe --tag)\";" > crates/utils/src/version.rs; \
&& cargo build --target ${CARGO_BUILD_TARGET} --features ${CARGO_BUILD_FEATURES} --release \ cargo build --target=x86_64-unknown-linux-musl --features "${CARGO_BUILD_FEATURES}" --release; \
&& cp ./target/$CARGO_BUILD_TARGET/$RUST_RELEASE_MODE/lemmy_server /app/lemmy_server; \ mv target/x86_64-unknown-linux-musl/release/lemmy_server ./lemmy; \
fi fi
# The alpine runner # ARM64 builder
FROM alpine:3 as lemmy FROM base-arm64 AS build-arm64
# Install libpq for postgres ARG CARGO_BUILD_FEATURES
RUN apk add --no-cache libpq ARG RUST_RELEASE_MODE
# Copy resources WORKDIR /lemmy
COPY --from=builder /app/lemmy_server /app/lemmy
COPY . ./
# Debug build
RUN --mount=type=cache,target=/lemmy/target set -ex; \
if [ "${RUST_RELEASE_MODE}" = "debug" ]; then \
echo "pub const VERSION: &str = \"$(git describe --tag)\";" > crates/utils/src/version.rs; \
cargo build --target=aarch64-unknown-linux-musl --features "${CARGO_BUILD_FEATURES}"; \
mv target/aarch64-unknown-linux-musl/debug/lemmy_server ./lemmy; \
fi
# Release build
RUN set -ex; \
if [ "${RUST_RELEASE_MODE}" = "release" ]; then \
echo "pub const VERSION: &str = \"$(git describe --tag)\";" > crates/utils/src/version.rs; \
cargo build --target=aarch64-unknown-linux-musl --features "${CARGO_BUILD_FEATURES}" --release; \
mv target/aarch64-unknown-linux-musl/release/lemmy_server ./lemmy; \
fi
# Get target binary
FROM build-${TARGETARCH} AS build
## Final image
FROM alpine:${ALPINE_VERSION}
ARG UID
ARG GID
RUN apk add --no-cache \
ca-certificates
COPY --from=build --chmod=0755 /lemmy/lemmy /usr/local/bin
RUN addgroup -S -g ${GID} lemmy && \
adduser -S -H -D -G lemmy -u ${UID} -g "" -s /sbin/nologin lemmy
# Create non-privileged user
RUN adduser -h /app -s sh -S -u 1000 lemmy
RUN chown -R lemmy /app
USER lemmy USER lemmy
CMD ["/app/lemmy"] CMD ["lemmy"]
EXPOSE 8536
STOPSIGNAL SIGTERM