FROM golang:1.18.0-alpine as builder

RUN apk update && apk add --no-cache git

# Create appuser.
ENV USER=appuser
ENV UID=10001

# See https://stackoverflow.com/a/55757473/12429735RUN
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    "${USER}"


WORKDIR $GOPATH/src/conka.pro/webauthn-server/
COPY . .

RUN go mod download

# RUN go mod verify
# Build the binary.
#RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/hello
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o /go/bin/webauthn-server

FROM scratch as runtime

# Import the user and group files from the builder.
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group

# Copy our static executable.
COPY --from=builder /go/bin/webauthn-server /go/bin/webauthn-server
COPY static /go/static

# Use an unprivileged user.
USER appuser:appuser

ENTRYPOINT ["/go/bin/webauthn-server"]
