From: 6543 <6543@obermui.de> Date: Sun, 16 Apr 2023 16:56:12 +0000 (+0200) Subject: Use official rust image and clean up Dockerfiles (#2804) X-Git-Url: http://these/git/%22%7B%7D/static/README.zh.hant.md?a=commitdiff_plain;h=90c6dc27326e51e3b8db70e2702870c32b0dcfc7;p=lemmy.git Use official rust image and clean up Dockerfiles (#2804) * 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 --- diff --git a/.woodpecker.yml b/.woodpecker.yml index adeeffd8..9bed578c 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -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 diff --git a/docker/Dockerfile b/docker/Dockerfile index 4da87a1d..c6df9f1c 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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 index 62439468..00000000 --- a/docker/Dockerfile.arm +++ /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 index 00000000..6f793d42 --- /dev/null +++ b/docker/Dockerfile.multiarch @@ -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"]