-
-
Notifications
You must be signed in to change notification settings - Fork 883
/
Dockerfile
121 lines (94 loc) · 3.94 KB
/
Dockerfile
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# syntax=docker/dockerfile:1.10
ARG RUST_VERSION=1.81
ARG CARGO_BUILD_FEATURES=default
ARG RUST_RELEASE_MODE=debug
ARG AMD_BUILDER_IMAGE=rust:${RUST_VERSION}
# Repo: https://github.com/raskyld/lemmy-cross-toolchains
ARG ARM_BUILDER_IMAGE="ghcr.io/raskyld/aarch64-lemmy-linux-gnu:v0.5.0"
ARG AMD_RUNNER_IMAGE=debian:bookworm-slim
ARG ARM_RUNNER_IMAGE=debian:bookworm-slim
ARG UNAME=lemmy
ARG UID=1000
ARG GID=1000
# AMD64 builder
FROM --platform=${BUILDPLATFORM} ${AMD_BUILDER_IMAGE} AS build-amd64
ARG CARGO_BUILD_FEATURES
ARG RUST_RELEASE_MODE
ARG RUSTFLAGS
WORKDIR /lemmy
COPY . ./
# Debug build
RUN --mount=type=cache,target=/lemmy/target set -ex; \
if [ "${RUST_RELEASE_MODE}" = "debug" ]; then \
cargo build --features "${CARGO_BUILD_FEATURES}"; \
mv target/"${RUST_RELEASE_MODE}"/lemmy_server ./lemmy_server; \
fi
# Release build
RUN --mount=type=cache,target=/lemmy/target set -ex; \
if [ "${RUST_RELEASE_MODE}" = "release" ]; then \
cargo clean --release; \
cargo build --features "${CARGO_BUILD_FEATURES}" --release; \
mv target/"${RUST_RELEASE_MODE}"/lemmy_server ./lemmy_server; \
#
# 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; \
fi
# ARM64 builder
# 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
ARG RUST_RELEASE_MODE
ARG CARGO_BUILD_FEATURES
ARG RUSTFLAGS
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 \
cargo clean --release; \
cargo build --features "${CARGO_BUILD_FEATURES}" --release; \
mv "./target/$CARGO_BUILD_TARGET/$RUST_RELEASE_MODE/lemmy_server" /home/lemmy/lemmy_server; \
#
# 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; \
fi
# amd64 base runner
FROM ${AMD_RUNNER_IMAGE} AS runner-linux-amd64
# 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
COPY --from=build-amd64 --chmod=0755 /lemmy/lemmy_server /usr/local/bin
# arm base runner
FROM ${ARM_RUNNER_IMAGE} AS runner-linux-arm64
RUN apt update && apt install -y libssl-dev libpq-dev ca-certificates curl
COPY --from=build-arm64 --chmod=0755 /home/lemmy/lemmy_server /usr/local/bin
# Final image that use a base runner based on the target OS and ARCH
FROM runner-${TARGETOS}-${TARGETARCH}
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"
ARG UNAME
ARG GID
ARG UID
RUN groupadd -g ${GID} -o ${UNAME} && \
useradd -m -u ${UID} -g ${GID} -o -s /bin/bash ${UNAME}
USER $UNAME
ENTRYPOINT ["lemmy_server"]
EXPOSE 8536
STOPSIGNAL SIGTERM