# syntax=docker/dockerfile:1.20.0

FROM golang:1.26.5-alpine3.23@sha256:622e56dbc11a8cfe87cafa2331e9a201877271cbff918af53d3be315f3da88cc AS builder

ENV CGO_ENABLED=0
ENV GOTOOLCHAIN=local

RUN GOBIN=/out go install \
  -trimpath \
  -ldflags="-s -w -X github.com/caddyserver/caddy/v2.CustomVersion=v2.11.4" \
  github.com/caddyserver/caddy/v2/cmd/caddy@v2.11.4

RUN mkdir -p /rootfs/data/caddy /rootfs/config/caddy /rootfs/tmp \
  && chown -R 1000:1000 /rootfs

FROM scratch

ENV XDG_CONFIG_HOME=/config
ENV XDG_DATA_HOME=/data
ENV HOME=/tmp

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /out/caddy /usr/bin/caddy
COPY --from=builder --chown=1000:1000 /rootfs/data /data
COPY --from=builder --chown=1000:1000 /rootfs/config /config
COPY --from=builder --chown=1000:1000 /rootfs/tmp /tmp

USER 1000:1000
WORKDIR /srv

EXPOSE 80 443 443/udp 2019

ENTRYPOINT ["/usr/bin/caddy"]
CMD ["run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
