Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use official rust image and clean up Dockerfiles #2804

Merged
17 changes: 10 additions & 7 deletions .woodpecker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -227,7 +230,7 @@ pipeline:
secrets: [cargo_api_token]
when:
event: tag
#platform: linux/amd64
#platform: linux/amd64

notify_on_failure:
image: alpine:3
Expand Down
35 changes: 20 additions & 15 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
37 changes: 0 additions & 37 deletions docker/Dockerfile.arm

This file was deleted.

55 changes: 55 additions & 0 deletions docker/Dockerfile.multiarch
Original file line number Diff line number Diff line change
@@ -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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably remove this file as cross-compile isnt working. And based on the age of the issue it wont get fixed anytime soon :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull does not address cross compilation!

Its moved in a extra pull

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this was added to the main branch anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the multiarch docker image is just ment to be build with buildx ... and the cross-compiline is just an optional feature ... for now I would recomend to just use it as single source of truth and have different agents that target this dockerfile

the old Dockerfile is just for normal devs who wana build the image but dont have buildx installed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I would prefer to remove the old dockerfile, and list buildx as a requirements. cc @dessalines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok fine with that ... I have a look in altering the docs/readme

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'm def fine with removing the old Dockerfile

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the "old" is the same as the new one ... just without some fancy if statements and args on FROM 😆. so you can make the multiarch to a normal one again at any time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.