FROM golang:alpine AS build
WORKDIR /app

RUN rm /etc/passwd \
 && touch /etc/passwd /etc/group \
 && adduser --disabled-password --shell /sbin/nologin --no-create-home --uid 1000 nonroot

# install ca-certificates to support https
RUN apk update && apk upgrade && apk add --no-cache ca-certificates
RUN update-ca-certificates

COPY go.* ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath -o marketminds main.go

FROM scratch
COPY --from=build /etc/passwd /etc/passwd
# copy ca-certificates for https
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build --chown=nonroot /app/marketminds /app/marketminds
# copy db migration files
COPY --from=build --chown=nonroot /app/db/ /app/db/

#ENV GIN_MODE=release
USER nonroot
ENTRYPOINT ["/app/marketminds"]

 