2024-11-01 13:34:12 +00:00
|
|
|
# syntax=docker/dockerfile:1.10
|
2024-09-19 21:00:20 +00:00
|
|
|
ARG RUST_VERSION=1.81
|
2023-07-05 11:25:19 +00:00
|
|
|
ARG CARGO_BUILD_FEATURES=default
|
2023-08-31 13:02:59 +00:00
|
|
|
ARG RUST_RELEASE_MODE=debug
|
2023-11-24 17:52:19 +00:00
|
|
|
|
2023-10-11 15:57:05 +00:00
|
|
|
ARG AMD_BUILDER_IMAGE=rust:${RUST_VERSION}
|
2024-01-09 16:48:29 +00:00
|
|
|
# Repo: https://github.com/raskyld/lemmy-cross-toolchains
|
2024-12-04 09:22:43 +00:00
|
|
|
ARG ARM_BUILDER_IMAGE="ghcr.io/raskyld/aarch64-lemmy-linux-gnu:v0.5.0"
|
2023-11-24 17:52:19 +00:00
|
|
|
|
2023-10-11 15:57:05 +00:00
|
|
|
ARG AMD_RUNNER_IMAGE=debian:bookworm-slim
|
2023-11-24 17:52:19 +00:00
|
|
|
ARG ARM_RUNNER_IMAGE=debian:bookworm-slim
|
|
|
|
|
|
|
|
ARG UNAME=lemmy
|
|
|
|
ARG UID=1000
|
|
|
|
ARG GID=1000
|
2023-08-31 13:02:59 +00:00
|
|
|
|
|
|
|
# AMD64 builder
|
2023-10-11 15:57:05 +00:00
|
|
|
FROM --platform=${BUILDPLATFORM} ${AMD_BUILDER_IMAGE} AS build-amd64
|
2023-08-31 13:02:59 +00:00
|
|
|
|
|
|
|
ARG CARGO_BUILD_FEATURES
|
|
|
|
ARG RUST_RELEASE_MODE
|
2024-03-21 15:42:30 +00:00
|
|
|
ARG RUSTFLAGS
|
2023-08-31 13:02:59 +00:00
|
|
|
|
|
|
|
WORKDIR /lemmy
|
|
|
|
|
|
|
|
COPY . ./
|
|
|
|
|
|
|
|
# Debug build
|
|
|
|
RUN --mount=type=cache,target=/lemmy/target set -ex; \
|
|
|
|
if [ "${RUST_RELEASE_MODE}" = "debug" ]; then \
|
2023-10-11 15:57:05 +00:00
|
|
|
cargo build --features "${CARGO_BUILD_FEATURES}"; \
|
2023-11-26 21:50:31 +00:00
|
|
|
mv target/"${RUST_RELEASE_MODE}"/lemmy_server ./lemmy_server; \
|
2023-02-17 06:29:45 +00:00
|
|
|
fi
|
|
|
|
|
2023-08-31 13:02:59 +00:00
|
|
|
# Release build
|
2023-11-26 21:50:31 +00:00
|
|
|
RUN --mount=type=cache,target=/lemmy/target set -ex; \
|
2023-08-31 13:02:59 +00:00
|
|
|
if [ "${RUST_RELEASE_MODE}" = "release" ]; then \
|
2024-04-04 14:14:59 +00:00
|
|
|
cargo clean --release; \
|
2023-10-11 15:57:05 +00:00
|
|
|
cargo build --features "${CARGO_BUILD_FEATURES}" --release; \
|
2023-11-26 21:50:31 +00:00
|
|
|
mv target/"${RUST_RELEASE_MODE}"/lemmy_server ./lemmy_server; \
|
2024-10-29 15:01:58 +00:00
|
|
|
#
|
|
|
|
# Compress the binary with upx
|
|
|
|
wget https://github.com/upx/upx/releases/download/v4.2.4/upx-4.2.4-amd64_linux.tar.xz; \
|
|
|
|
tar -xvf upx-4.2.4-amd64_linux.tar.xz; \
|
|
|
|
cp upx-4.2.4-amd64_linux/upx /usr/bin; \
|
|
|
|
upx --best --lzma lemmy_server; \
|
2023-02-17 06:29:45 +00:00
|
|
|
fi
|
|
|
|
|
2023-08-31 13:02:59 +00:00
|
|
|
# ARM64 builder
|
2023-11-24 17:52:19 +00:00
|
|
|
# NB(raskyld): this is a hack to be able to COPY --from= this image, because the variable doesn't
|
|
|
|
# seem to be expended in --form arg of COPY :(
|
|
|
|
FROM --platform=linux/amd64 ${ARM_BUILDER_IMAGE} AS build-arm64
|
2023-08-31 13:02:59 +00:00
|
|
|
|
2023-11-26 21:50:31 +00:00
|
|
|
ARG RUST_RELEASE_MODE
|
|
|
|
ARG CARGO_BUILD_FEATURES
|
2024-03-21 15:42:30 +00:00
|
|
|
ARG RUSTFLAGS
|
2023-11-26 21:50:31 +00:00
|
|
|
|
|
|
|
WORKDIR /home/lemmy/src
|
|
|
|
USER 10001:10001
|
|
|
|
|
|
|
|
COPY --chown=lemmy:lemmy . ./
|
|
|
|
|
|
|
|
ENV PATH="/home/lemmy/.cargo/bin:${PATH}"
|
|
|
|
ENV RUST_RELEASE_MODE=${RUST_RELEASE_MODE} \
|
|
|
|
CARGO_BUILD_FEATURES=${CARGO_BUILD_FEATURES}
|
|
|
|
|
|
|
|
# Debug build
|
|
|
|
RUN --mount=type=cache,target=./target,uid=10001,gid=10001 set -ex; \
|
|
|
|
if [ "${RUST_RELEASE_MODE}" = "debug" ]; then \
|
|
|
|
cargo build --features "${CARGO_BUILD_FEATURES}"; \
|
|
|
|
mv "./target/$CARGO_BUILD_TARGET/$RUST_RELEASE_MODE/lemmy_server" /home/lemmy/lemmy_server; \
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Release build
|
|
|
|
RUN --mount=type=cache,target=./target,uid=10001,gid=10001 set -ex; \
|
|
|
|
if [ "${RUST_RELEASE_MODE}" = "release" ]; then \
|
2024-04-04 14:14:59 +00:00
|
|
|
cargo clean --release; \
|
2023-11-26 21:50:31 +00:00
|
|
|
cargo build --features "${CARGO_BUILD_FEATURES}" --release; \
|
|
|
|
mv "./target/$CARGO_BUILD_TARGET/$RUST_RELEASE_MODE/lemmy_server" /home/lemmy/lemmy_server; \
|
2024-10-29 15:01:58 +00:00
|
|
|
#
|
|
|
|
# Compress the binary with upx
|
|
|
|
wget https://github.com/upx/upx/releases/download/v4.2.4/upx-4.2.4-arm64_linux.tar.xz; \
|
|
|
|
tar -xvf upx-4.2.4-arm64_linux.tar.xz; \
|
|
|
|
cp upx-4.2.4-arm64_linux/upx /usr/bin; \
|
|
|
|
upx --best --lzma /home/lemmy/lemmy_server; \
|
2023-11-26 21:50:31 +00:00
|
|
|
fi
|
|
|
|
|
2023-11-24 17:52:19 +00:00
|
|
|
# amd64 base runner
|
|
|
|
FROM ${AMD_RUNNER_IMAGE} AS runner-linux-amd64
|
2023-02-17 06:29:45 +00:00
|
|
|
|
2024-03-06 10:15:13 +00:00
|
|
|
# Add system packages that are needed: federation needs CA certificates, curl can be used for healthchecks
|
|
|
|
RUN apt update && apt install -y libssl-dev libpq-dev ca-certificates curl
|
2023-08-31 13:02:59 +00:00
|
|
|
|
2023-11-26 21:50:31 +00:00
|
|
|
COPY --from=build-amd64 --chmod=0755 /lemmy/lemmy_server /usr/local/bin
|
2023-08-31 13:02:59 +00:00
|
|
|
|
2023-11-24 17:52:19 +00:00
|
|
|
# arm base runner
|
|
|
|
FROM ${ARM_RUNNER_IMAGE} AS runner-linux-arm64
|
2023-10-11 15:57:05 +00:00
|
|
|
|
2024-03-06 10:15:13 +00:00
|
|
|
RUN apt update && apt install -y libssl-dev libpq-dev ca-certificates curl
|
2023-08-31 13:02:59 +00:00
|
|
|
|
2023-11-24 17:52:19 +00:00
|
|
|
COPY --from=build-arm64 --chmod=0755 /home/lemmy/lemmy_server /usr/local/bin
|
2023-10-11 15:57:05 +00:00
|
|
|
|
2023-11-24 17:52:19 +00:00
|
|
|
# Final image that use a base runner based on the target OS and ARCH
|
|
|
|
FROM runner-${TARGETOS}-${TARGETARCH}
|
2023-08-31 13:02:59 +00:00
|
|
|
|
2023-11-24 17:52:19 +00:00
|
|
|
LABEL org.opencontainers.image.authors="The Lemmy Authors"
|
|
|
|
LABEL org.opencontainers.image.source="https://github.com/LemmyNet/lemmy"
|
|
|
|
LABEL org.opencontainers.image.licenses="AGPL-3.0-or-later"
|
|
|
|
LABEL org.opencontainers.image.description="A link aggregator and forum for the fediverse"
|
2023-08-31 13:02:59 +00:00
|
|
|
|
2023-11-24 17:52:19 +00:00
|
|
|
ARG UNAME
|
|
|
|
ARG GID
|
|
|
|
ARG UID
|
2023-08-31 13:02:59 +00:00
|
|
|
|
2023-11-24 17:52:19 +00:00
|
|
|
RUN groupadd -g ${GID} -o ${UNAME} && \
|
|
|
|
useradd -m -u ${UID} -g ${GID} -o -s /bin/bash ${UNAME}
|
2023-10-11 15:57:05 +00:00
|
|
|
USER $UNAME
|
2023-08-31 13:02:59 +00:00
|
|
|
|
2023-11-24 17:52:19 +00:00
|
|
|
ENTRYPOINT ["lemmy_server"]
|
2023-10-11 15:57:05 +00:00
|
|
|
EXPOSE 8536
|
|
|
|
STOPSIGNAL SIGTERM
|