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
This commit is contained in:
6543 2023-04-16 18:56:12 +02:00 committed by GitHub
parent 3fa713f414
commit 90c6dc2732
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 60 deletions

View file

@ -135,13 +135,14 @@ pipeline:
nightly_build: nightly_build:
image: woodpeckerci/plugin-docker-buildx image: woodpeckerci/plugin-docker-buildx
settings: settings:
dockerfile: docker/Dockerfile repo: dessalines/lemmy
dockerfile: docker/Dockerfile.multiarch
platforms: linux/amd64
build_args: RUST_RELEASE_MODE=release build_args: RUST_RELEASE_MODE=release
username: username:
from_secret: docker_username from_secret: docker_username
password: password:
from_secret: docker_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 # 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 tag: dev
when: when:
@ -152,13 +153,14 @@ pipeline:
publish_release_docker_image_amd: publish_release_docker_image_amd:
image: woodpeckerci/plugin-docker-buildx image: woodpeckerci/plugin-docker-buildx
settings: settings:
dockerfile: docker/Dockerfile repo: dessalines/lemmy
dockerfile: docker/Dockerfile.multiarch
platforms: linux/amd64
build_args: RUST_RELEASE_MODE=release build_args: RUST_RELEASE_MODE=release
username: username:
from_secret: docker_username from_secret: docker_username
password: password:
from_secret: docker_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 # 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: true
# auto_tag_suffix: linux-amd64 # auto_tag_suffix: linux-amd64
@ -169,13 +171,14 @@ pipeline:
publish_release_docker_image_arm: publish_release_docker_image_arm:
image: woodpeckerci/plugin-docker-buildx image: woodpeckerci/plugin-docker-buildx
settings: settings:
dockerfile: docker/Dockerfile repo: dessalines/lemmy
dockerfile: docker/Dockerfile.multiarch
platforms: linux/arm64
build_args: RUST_RELEASE_MODE=release build_args: RUST_RELEASE_MODE=release
username: username:
from_secret: docker_username from_secret: docker_username
password: password:
from_secret: docker_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 # 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: true
# auto_tag_suffix: linux-arm64 # auto_tag_suffix: linux-arm64
@ -227,7 +230,7 @@ pipeline:
secrets: [cargo_api_token] secrets: [cargo_api_token]
when: when:
event: tag event: tag
#platform: linux/amd64 #platform: linux/amd64
notify_on_failure: notify_on_failure:
image: alpine:3 image: alpine:3

View file

@ -1,34 +1,39 @@
FROM clux/muslrust:1.67.0 as builder 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 WORKDIR /app
ARG CARGO_BUILD_TARGET=x86_64-unknown-linux-musl
# This can be set to release using --build-arg
ARG RUST_RELEASE_MODE="debug"
COPY . . 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 # Debug mode build
RUN --mount=type=cache,target=/app/target \ 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" \ 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} \ && 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 fi
# Release mode build # Release mode build
RUN \ RUN \
if [ "$RUST_RELEASE_MODE" = "release" ] ; then \ if [ "$RUST_RELEASE_MODE" = "release" ]; then \
cargo build --target ${CARGO_BUILD_TARGET} --release \ rustup target add ${CARGO_BUILD_TARGET} \
&& cp ./target/$CARGO_BUILD_TARGET/$RUST_RELEASE_MODE/lemmy_server /app/lemmy_server; \ && cargo build --target ${CARGO_BUILD_TARGET} --release \
&& cp ./target/${CARGO_BUILD_TARGET}/${RUST_RELEASE_MODE}/lemmy_server /app/lemmy_server; \
fi fi
# The alpine runner # The Alpine runner
FROM alpine:3 as lemmy FROM alpine:3 as lemmy
# Install libpq for postgres # Install libpq for Postgres
RUN apk add libpq RUN apk add --no-cache ca-certificates libpq
# Copy resources # Copy resources
COPY --from=builder /app/lemmy_server /app/lemmy COPY --from=builder /app/lemmy_server /app/lemmy

View file

@ -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"]

View file

@ -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"]