From 1e7d750641b37ad428ba75ccdf757fa50d196d98 Mon Sep 17 00:00:00 2001 From: ubergeek77 Date: Fri, 15 Dec 2023 14:28:27 -0600 Subject: [PATCH] Update Backend Dockerfile; remove Frontend Dockerfile as upstream is fixed --- Dockerfile-backend | 79 +++++++++++++++++++++--------------------- Dockerfile-frontend | 84 --------------------------------------------- 2 files changed, 40 insertions(+), 123 deletions(-) delete mode 100644 Dockerfile-frontend diff --git a/Dockerfile-backend b/Dockerfile-backend index bb4ee61..581d9af 100644 --- a/Dockerfile-backend +++ b/Dockerfile-backend @@ -1,55 +1,56 @@ -FROM rust:1.70-slim-buster as builder - -# Install compilation dependencies -RUN apt-get update \ - && apt-get -y install --no-install-recommends libssl-dev pkg-config libpq-dev git \ - && rm -rf /var/lib/apt/lists/* +ARG RUST_VERSION=1.72.1 +ARG CARGO_BUILD_FEATURES=default +ARG RUST_RELEASE_MODE=release -WORKDIR /app +ARG UNAME=lemmy +ARG UID=1000 +ARG GID=1000 -# comma-seperated list of features to enable -ARG CARGO_BUILD_FEATURES=default +FROM rust:${RUST_VERSION}-slim-buster as builder -# This can be set to release using --build-arg -ARG RUST_RELEASE_MODE="release" +ARG CARGO_BUILD_FEATURES +ARG RUST_RELEASE_MODE -COPY . . +WORKDIR /lemmy -# Build the project +COPY . ./ -# Debug mode build -RUN --mount=type=cache,target=/app/target \ - if [ "$RUST_RELEASE_MODE" = "debug" ] ; then \ - echo "pub const VERSION: &str = \"$(git describe --tag)\";" > "crates/utils/src/version.rs" \ - && echo "Building Lemmy $(git describe --tag), Cargo Target: $(rustc -vV | sed -n 's|host: ||p'), Mode: $RUST_RELEASE_MODE" \ - && cargo build --features ${CARGO_BUILD_FEATURES} \ - && cp ./target/$RUST_RELEASE_MODE/lemmy_server /app/lemmy_server; \ +# 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; \ + echo "Building Lemmy $(git describe --tag), Cargo Target: $(rustc -vV | sed -n 's|host: ||p'), Mode: $RUST_RELEASE_MODE" \ + cargo build --features "${CARGO_BUILD_FEATURES}"; \ + mv target/"${RUST_RELEASE_MODE}"/lemmy_server ./lemmy_server; \ fi -# Release mode build -RUN \ - if [ "$RUST_RELEASE_MODE" = "release" ] ; then \ - echo "pub const VERSION: &str = \"$(git describe --tag)\";" > "crates/utils/src/version.rs" \ - && echo "Building Lemmy $(git describe --tag), Cargo Target: $(rustc -vV | sed -n 's|host: ||p'), Mode: $RUST_RELEASE_MODE" \ - && cargo build --features ${CARGO_BUILD_FEATURES} --release \ - && cp ./target/$RUST_RELEASE_MODE/lemmy_server /app/lemmy_server; \ +# Release build +RUN --mount=type=cache,target=/lemmy/target set -ex; \ + if [ "${RUST_RELEASE_MODE}" = "release" ]; then \ + echo "pub const VERSION: &str = \"$(git describe --tag)\";" > crates/utils/src/version.rs; \ + echo "Building Lemmy $(git describe --tag), Cargo Target: $(rustc -vV | sed -n 's|host: ||p'), Mode: $RUST_RELEASE_MODE" \ + [ -z "$USE_RELEASE_CACHE" ] && cargo clean --release; \ + cargo build --features "${CARGO_BUILD_FEATURES}" --release; \ + mv target/"${RUST_RELEASE_MODE}"/lemmy_server ./lemmy_server; \ fi + # The Debian runner -FROM debian:buster-slim as lemmy +FROM debian:bookworm-slim as lemmy -# Install libpq for postgres -RUN apt-get update \ - && apt-get -y install --no-install-recommends postgresql-client libc6 libssl1.1 ca-certificates \ - && rm -rf /var/lib/apt/lists/* +# Federation needs CA certificates +RUN apt update && apt install -y libssl-dev libpq-dev ca-certificates -RUN addgroup --gid 1000 lemmy -RUN useradd --no-create-home --shell /bin/sh --uid 1000 --gid 1000 lemmy +COPY --from=builder --chmod=0755 /lemmy/lemmy_server /usr/local/bin -# Copy resources -COPY --chown=lemmy:lemmy --from=builder /app/lemmy_server /app/lemmy +ARG UNAME +ARG GID +ARG UID -RUN chown lemmy:lemmy /app/lemmy -USER lemmy +RUN groupadd -g ${GID} -o ${UNAME} && \ + useradd -m -u ${UID} -g ${GID} -o -s /bin/bash ${UNAME} +USER $UNAME -CMD ["/app/lemmy"] +ENTRYPOINT ["lemmy_server"] +EXPOSE 8536 +STOPSIGNAL SIGTERM diff --git a/Dockerfile-frontend b/Dockerfile-frontend deleted file mode 100644 index 1aef9ad..0000000 --- a/Dockerfile-frontend +++ /dev/null @@ -1,84 +0,0 @@ -# gobinaries.com is not reliable for container builds (external dependency) -# It also does not support arm/arm64 (at least not right now) -# We have to build node-prune from source -FROM golang as go-builder -WORKDIR /app -RUN cd /app && \ - git clone https://github.com/tj/node-prune && \ - cd /app/node-prune && \ - CGO_ENABLED=0 go build - -# Original Dockerfile below (mostly) -FROM node:alpine as builder - -# Upgrade to edge to fix sharp/libvips issues -RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/main" > /etc/apk/repositories && \ - echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories - -# Added vips-dev and pkgconfig so that local vips is used instead of prebuilt -# Done for two reasons: -# - libvips binaries are not available for ARM32 -# - It can break depending on the CPU (https://github.com/LemmyNet/lemmy-ui/issues/1566) -RUN apk update && apk upgrade && apk add --no-cache curl yarn python3 build-base gcc wget git vips-dev pkgconfig - -# Install node-gyp -RUN npm install -g node-gyp - -WORKDIR /usr/src/app - -ENV npm_config_target_platform=linux -ENV npm_config_target_libc=musl - -# Cache deps -COPY package.json yarn.lock ./ - -RUN sed -i 's|"sharp": "^0.32.4"|"sharp": "^0.32.6"|g' package.json - -RUN yarn --production --prefer-offline --pure-lockfile - -# Build -COPY generate_translations.js \ - tsconfig.json \ - webpack.config.js \ - .babelrc \ - ./ - -COPY lemmy-translations lemmy-translations -COPY src src -COPY .git .git - -# Set UI version -RUN echo "export const VERSION = '$(git describe --tag)';" > "src/shared/version.ts" - -RUN yarn --production --prefer-offline -RUN yarn build:prod - -# Prune the image -# Copy the manually built node-prune here -RUN mkdir -p /usr/local/bin/ -COPY --from=go-builder /app/node-prune/node-prune /usr/local/bin/node-prune -RUN chmod +x /usr/local/bin/node-prune -RUN node-prune /usr/src/app/node_modules - -RUN rm -rf ./node_modules/import-sort-parser-typescript -RUN rm -rf ./node_modules/typescript -RUN rm -rf ./node_modules/npm - -RUN du -sh ./node_modules/* | sort -nr | grep '\dM.*' - -FROM node:alpine as runner - -# Upgrade to edge to fix sharp/libvips issues -RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/main" > /etc/apk/repositories && \ - echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories - -RUN apk update && apk upgrade && apk add vips-cpp -COPY --from=builder /usr/src/app/dist /app/dist -COPY --from=builder /usr/src/app/node_modules /app/node_modules - -RUN chown -R node:node /app -USER node - -EXPOSE 1234 -WORKDIR /app -CMD node dist/js/server.js