]> Untitled Git - lemmy.git/commitdiff
Use official rust image and clean up Dockerfiles (#2804)
author6543 <6543@obermui.de>
Sun, 16 Apr 2023 16:56:12 +0000 (18:56 +0200)
committerGitHub <noreply@github.com>
Sun, 16 Apr 2023 16:56:12 +0000 (12:56 -0400)
* Add docker/Dockerfile.multiarch

* Update normal Dockerfile to be similar to multiarch one

* docker build run on x86 and arm64 now
manifest not needed, its handled by buildx plugin

* Rename Dockerfile.arm To Dockerfile.debian

* Emulate builder on arm target too

* fix misspell

* Improve Dockerfiles

* naming

* undo multiarchbuild settings as its blocked by a dependeny, see #2806

* Delete Dockerfile.debian

.woodpecker.yml
docker/Dockerfile
docker/Dockerfile.arm [deleted file]
docker/Dockerfile.multiarch [new file with mode: 0644]

index adeeffd87f2a507cc1f7e95ab11974d37c668413..9bed578c83b74a9334b2fcb615c957983e2273bc 100644 (file)
@@ -135,13 +135,14 @@ pipeline:
   nightly_build:
     image: woodpeckerci/plugin-docker-buildx
     settings:
-      dockerfile: docker/Dockerfile
+      repo: dessalines/lemmy
+      dockerfile: docker/Dockerfile.multiarch
+      platforms: linux/amd64
       build_args: RUST_RELEASE_MODE=release
       username:
         from_secret: docker_username
       password:
         from_secret: docker_password
-      repo: dessalines/lemmy
       # add_host: github.com:140.82.112.3,static.crates.io:18.154.227.73,crates.io:108.138.64.68,dl-cdn.alpinelinux.org:146.75.30.133
       tag: dev
     when:
@@ -152,13 +153,14 @@ pipeline:
   publish_release_docker_image_amd:
     image: woodpeckerci/plugin-docker-buildx
     settings:
-      dockerfile: docker/Dockerfile
+      repo: dessalines/lemmy
+      dockerfile: docker/Dockerfile.multiarch
+      platforms: linux/amd64
       build_args: RUST_RELEASE_MODE=release
       username:
         from_secret: docker_username
       password:
         from_secret: docker_password
-      repo: dessalines/lemmy
       # add_host: github.com:140.82.112.3,static.crates.io:18.154.227.73,crates.io:108.138.64.68,dl-cdn.alpinelinux.org:146.75.30.133
       auto_tag: true
       # auto_tag_suffix: linux-amd64
@@ -169,13 +171,14 @@ pipeline:
   publish_release_docker_image_arm:
     image: woodpeckerci/plugin-docker-buildx
     settings:
-      dockerfile: docker/Dockerfile
+      repo: dessalines/lemmy
+      dockerfile: docker/Dockerfile.multiarch
+      platforms: linux/arm64
       build_args: RUST_RELEASE_MODE=release
       username:
         from_secret: docker_username
       password:
         from_secret: docker_password
-      repo: dessalines/lemmy
       # add_host: github.com:140.82.112.3,static.crates.io:18.154.227.73,crates.io:108.138.64.68,dl-cdn.alpinelinux.org:146.75.30.133
       auto_tag: true
       # auto_tag_suffix: linux-arm64
@@ -227,7 +230,7 @@ pipeline:
     secrets: [cargo_api_token]
     when:
       event: tag
-        #platform: linux/amd64
+      #platform: linux/amd64
 
   notify_on_failure:
     image: alpine:3
index 4da87a1d8416aee32802c27657910bae0c08eb8d..c6df9f1c1892ee29f5ee8a8cda73ce6369de1215 100644 (file)
@@ -1,34 +1,39 @@
-FROM clux/muslrust:1.67.0 as builder
-WORKDIR /app
-ARG CARGO_BUILD_TARGET=x86_64-unknown-linux-musl
+FROM rust:1.67.0-alpine as builder
 
-# This can be set to release using --build-arg
-ARG RUST_RELEASE_MODE="debug"
+# Install build dependencies
+RUN apk add --no-cache git openssl-dev libpq-dev musl-dev
 
+# Set the working directory to /app and copy the source code
+WORKDIR /app
 COPY . .
 
-# Build the project
-    
+# Set cargo build target (can be changed using --build-arg)
+ARG CARGO_BUILD_TARGET="x86_64-unknown-linux-musl"
+# Set the release mode (can be changed using --build-arg)
+ARG RUST_RELEASE_MODE="debug"
+
 # Debug mode build
 RUN --mount=type=cache,target=/app/target \
-    if [ "$RUST_RELEASE_MODE" = "debug" ] ; then \
+    if [ "$RUST_RELEASE_MODE" = "debug" ]; then \
       echo "pub const VERSION: &str = \"$(git describe --tag)\";" > "crates/utils/src/version.rs" \
+      && rustup target add ${CARGO_BUILD_TARGET} \
       && cargo build --target ${CARGO_BUILD_TARGET} \
-      && cp ./target/$CARGO_BUILD_TARGET/$RUST_RELEASE_MODE/lemmy_server /app/lemmy_server; \
+      && cp ./target/${CARGO_BUILD_TARGET}/${RUST_RELEASE_MODE}/lemmy_server /app/lemmy_server; \
     fi
 
 # Release mode build
 RUN \
-    if [ "$RUST_RELEASE_MODE" = "release" ] ; then \
-      cargo build --target ${CARGO_BUILD_TARGET} --release \
-      && cp ./target/$CARGO_BUILD_TARGET/$RUST_RELEASE_MODE/lemmy_server /app/lemmy_server; \
+    if [ "$RUST_RELEASE_MODE" = "release" ]; then \
+      rustup target add ${CARGO_BUILD_TARGET} \
+      && cargo build --target ${CARGO_BUILD_TARGET} --release \
+      && cp ./target/${CARGO_BUILD_TARGET}/${RUST_RELEASE_MODE}/lemmy_server /app/lemmy_server; \
     fi
 
-# The alpine runner
+# The Alpine runner
 FROM alpine:3 as lemmy
 
-# Install libpq for postgres
-RUN apk add libpq
+# Install libpq for Postgres
+RUN apk add --no-cache ca-certificates libpq
 
 # Copy resources
 COPY --from=builder /app/lemmy_server /app/lemmy
diff --git a/docker/Dockerfile.arm b/docker/Dockerfile.arm
deleted file mode 100644 (file)
index 6243946..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-ARG RUST_BUILDER_IMAGE=rust:1.64-slim-buster
-
-# Build Lemmy
-FROM $RUST_BUILDER_IMAGE 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/*
-
-WORKDIR /app
-
-COPY ./ ./
-RUN echo "pub const VERSION: &str = \"$(git describe --tag)\";" > "crates/utils/src/version.rs"
-
-RUN cargo build --release
-
-RUN cp ./target/release/lemmy_server /app/lemmy_server
-
-# The Debian runner
-FROM debian:buster-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/*
-
-RUN addgroup --gid 1000 lemmy
-RUN useradd --no-create-home --shell /bin/sh --uid 1000 --gid 1000 lemmy
-
-# Copy resources
-COPY --chown=lemmy:lemmy --from=builder /app/lemmy_server /app/lemmy
-
-RUN chown lemmy:lemmy /app/lemmy
-USER lemmy
-EXPOSE 8536
-CMD ["/app/lemmy"]
diff --git a/docker/Dockerfile.multiarch b/docker/Dockerfile.multiarch
new file mode 100644 (file)
index 0000000..6f793d4
--- /dev/null
@@ -0,0 +1,55 @@
+# FIXME: use "--platform=$BUILDPLATFORM" and solve openssl cross-compile issue
+FROM rust:1.67.0-alpine as builder
+
+# Install build dependencies
+RUN apk add --no-cache git openssl-dev libpq-dev musl-dev
+
+# Set the working directory to /app and copy the source code
+WORKDIR /app
+COPY . .
+
+# Set the target architecture (can be set using --build-arg), buildx set it automatically
+ARG TARGETARCH
+
+# This can be set to release using --build-arg
+ARG RUST_RELEASE_MODE="debug"
+
+# Prepare toolchain
+# Docker and Rust use different architecture naming schemas, so we have to convert them
+RUN case $TARGETARCH in \
+      arm64) RUSTARCH=aarch64 ;; \
+      amd64) RUSTARCH=x86_64 ;; \
+      *) echo "unsupported architecture: $TARGETARCH"; exit 3 ;; \
+    esac \
+ && echo "RUSTARCH=$RUSTARCH" >> .buildenv
+
+# Debug mode build
+RUN --mount=type=cache,target=/app/target \
+    if [ "$RUST_RELEASE_MODE" = "debug" ]; then \
+      source .buildenv \
+      && echo "pub const VERSION: &str = \"$(git describe --tag)\";" > "crates/utils/src/version.rs" \
+      && rustup target add ${RUSTARCH}-unknown-linux-musl \
+      && cargo build --target ${RUSTARCH}-unknown-linux-musl \
+      && cp ./target/${RUSTARCH}-unknown-linux-musl/${RUST_RELEASE_MODE}/lemmy_server /app/lemmy_server; \
+    fi
+
+# Release mode build
+RUN \
+    if [ "$RUST_RELEASE_MODE" = "release" ]; then \
+      source .buildenv \
+      && rustup target add ${RUSTARCH}-unknown-linux-musl \
+      && cargo build --target ${RUSTARCH}-unknown-linux-musl --release \
+      && cp ./target/${RUSTARCH}-unknown-linux-musl/${RUST_RELEASE_MODE}/lemmy_server /app/lemmy_server; \
+    fi
+
+# The Alpine runner
+FROM alpine:3 as lemmy
+
+# Install libpq for Postgres
+RUN apk add --no-cache ca-certificates libpq
+
+# Copy resources
+COPY --from=builder /app/lemmy_server /app/lemmy
+
+EXPOSE 8536
+CMD ["/app/lemmy"]