# syntax=docker/dockerfile:1.2
# -------------------------------------------------------------------------- #
# Stage 1 - Build project in fat builder stage                               #
# -------------------------------------------------------------------------- #
FROM rust:1.50 as builder

RUN mkdir /builddir
WORKDIR /builddir

# Download dependencies sources and cache them
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
RUN cargo fetch

# Copy the actual project source code and compile it
COPY ./src src
RUN cargo build --release


# -------------------------------------------------------------------------- #
# Stage 2 - Create slim container with only the final binary to run conduit  #
# -------------------------------------------------------------------------- #
FROM alpine:latest as runner

FROM alpine:3.12

ARG CREATED
ARG VERSION
ARG GIT_REF=origin/master

ENV CONDUIT_CONFIG="/srv/conduit/conduit.toml"

# Add www-data user and group with UID 82, as used by alpine
# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install
RUN set -x ; \
    addgroup -Sg 82 www-data 2>/dev/null ; \
    adduser -S -D -H -h /srv/conduit -G www-data -g www-data www-data 2>/dev/null ; \
    addgroup www-data www-data 2>/dev/null && exit 0 ; exit 1

# Create a volume for the database, to persist its contents
VOLUME ["/srv/conduit/.local/share/conduit"]


# Install packages needed to run Conduit
RUN apk add --no-cache \
        ca-certificates \
        curl \
        libgcc


# Test if Conduit is still alive, uses the same endpoint as Element
HEALTHCHECK --start-period=2s CMD curl --fail -s http://localhost:8000/_matrix/client/versions || curl -k --fail -s https://localhost:8000/_matrix/client/versions || exit 1


# Prepare directory and copy binary from builder stage into it
RUN mkdir -p /srv/conduit/.local/share/conduit
COPY --from=builder /builddir/target/release/conduit /srv/conduit/
RUN chown -cR www-data:www-data /srv/conduit


# Labels according to https://github.com/opencontainers/image-spec/blob/master/annotations.md
# including a custom label specifying the build command
LABEL org.opencontainers.image.created=${CREATED} \
      org.opencontainers.image.authors="Conduit Contributors" \
      org.opencontainers.image.title="Conduit" \
      org.opencontainers.image.version=${VERSION} \
      org.opencontainers.image.vendor="Conduit Contributors" \
      org.opencontainers.image.description="A Matrix homeserver written in Rust" \
      org.opencontainers.image.url="https://conduit.rs/" \
      org.opencontainers.image.revision=${GIT_REF} \
      org.opencontainers.image.source="https://gitlab.com/famedly/conduit.git" \
      org.opencontainers.image.licenses="Apache-2.0" \
      org.opencontainers.image.documentation="" \
      org.opencontainers.image.ref.name="" \
      org.label-schema.docker.build="docker build . -t matrixconduit/matrix-conduit:latest --build-arg CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ') --build-arg VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml)" \
      maintainer="Weasy666"


# Prepare execution environment
USER www-data
WORKDIR /srv/conduit
EXPOSE 8000
ENTRYPOINT [ "/srv/conduit/conduit" ]