# Lint stage - fast feedback on lint issues, before the build starts.
# The linter is invoked directly rather than through `make lint`: that
# target shells out to `docker build -f Dockerfile.lint`, and there is
# no docker daemon inside a docker build.
# golangci/golangci-lint:v2.12.2 (Debian-based), 2026-08-10
FROM golangci/golangci-lint:v2.12.2@sha256:5cceeef04e53efe1470638d4b4b4f5ceefd574955ab3941b2d9a68a8c9ad5240 AS lint

WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN make fmt-check
RUN golangci-lint run --config .golangci.yml ./...

# Build stage
# golang 1.25-alpine, 2026-02-28
FROM golang@sha256:f6751d823c26342f9506c03797d2527668d095b0a15f1862cddb4d927a7a4ced AS builder

RUN apk add --no-cache git make gcc musl-dev binutils-gold

# Force BuildKit to run the lint stage before proceeding
COPY --from=lint /src/go.sum /dev/null

WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download

COPY . .

# Run the tests - build fails if any test fails
RUN make test

# Build the binary
RUN make build

# Runtime stage
# alpine 3.21, 2026-02-28
FROM alpine@sha256:c3f8e73fdb79deaebaa2037150150191b9dcbfba68b4a46d70103204c53f4709

RUN apk add --no-cache ca-certificates tzdata

WORKDIR /app

COPY --from=builder /src/bin/dnswatcher /app/dnswatcher

# Create data directory
RUN mkdir -p /var/lib/dnswatcher

ENV DNSWATCHER_DATA_DIR=/var/lib/dnswatcher

EXPOSE 8080

ENTRYPOINT ["/app/dnswatcher"]
