# syntax=docker/dockerfile:1
# A static musl binary on an empty image; the default theme is compiled into it.
#
#   docker build -t blog .
#   docker run -p 8080:8080 -v blog-data:/data -e BLOG_EDITOR_ORIGIN=https://write.example.com blog
#   docker run --rm -it -v blog-data:/data blog invite --id you

FROM rust:1-alpine AS build
# musl-dev for mimalloc's C build, nasm for rav1e's (AVIF) assembly.
RUN apk add --no-cache musl-dev nasm
WORKDIR /src
COPY Cargo.toml Cargo.lock ./
COPY src src
# The default theme is embedded at compile time (src/theme.rs).
COPY theme theme
# Alpine's Rust targets musl and links statically by default.
RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/src/target \
    cargo build --release --locked && cp target/release/blog /blog
# scratch has no mkdir; an empty, writable /data for the volume to start from.
RUN mkdir /data

FROM scratch
COPY --from=build /blog /blog
COPY --from=build --chown=65532:65532 /data /data
USER 65532:65532
ENV BLOG_STORE=/data \
    BLOG_PUBLISH=/data/public \
    BLOG_ADDR=0.0.0.0:8080 \
    MIMALLOC_ALLOW_THP=0
EXPOSE 8080
VOLUME /data
ENTRYPOINT ["/blog"]
CMD ["serve"]
