FROM golang:1.14-alpine AS builder

RUN apk -U --no-cache add build-base git gcc

WORKDIR /go/src/github.com/ory/kratos

ADD go.mod go.mod
ADD go.sum go.sum

RUN go mod download
RUN GO111MODULE=on go install github.com/gobuffalo/packr/v2/packr2

ADD . .

RUN packr2
RUN CGO_ENABLED=1 go build -tags sqlite -a -o /usr/bin/kratos

FROM alpine:3.11

RUN addgroup -S ory; \
    adduser -S ory -G ory -D -H -s /bin/nologin

EXPOSE 4433
EXPOSE 4434

COPY --from=builder /usr/bin/kratos /usr/bin/kratos

# By creating the sqlite folder as the ory user, the mounted volume will be owned by ory:ory, which
# is required for read/write of SQLite.
RUN mkdir -p /var/lib/sqlite
RUN chown ory:ory /var/lib/sqlite
VOLUME /var/lib/sqlite

USER ory

ENTRYPOINT ["kratos"]
CMD ["serve"]
