| 1 |
# Thanks to https://chemidy.medium.com/create-the-smallest-and-secured-golang-docker-image-based-on-scratch-4752223b7324 |
| 2 |
############################ |
| 3 |
# STEP 1 build executable binary |
| 4 |
############################ |
| 5 |
FROM golang:alpine AS builder |
| 6 |
|
| 7 |
ENV USER=certstreamserver |
| 8 |
ENV UID=10001 |
| 9 |
|
| 10 |
# Create user |
| 11 |
RUN adduser \ |
| 12 |
--disabled-password \ |
| 13 |
--gecos "" \ |
| 14 |
--home "/nonexistent" \ |
| 15 |
--shell "/sbin/nologin" \ |
| 16 |
--no-create-home \ |
| 17 |
--uid "${UID}" \ |
| 18 |
"${USER}" |
| 19 |
|
| 20 |
# Install git. Git is required for fetching the dependencies. |
| 21 |
RUN apk update && apk add --no-cache git |
| 22 |
WORKDIR $GOPATH/src/certstream-server-go/ |
| 23 |
COPY . . |
| 24 |
|
| 25 |
# Fetch dependencies. |
| 26 |
RUN go mod download |
| 27 |
|
| 28 |
# Build the binary. |
| 29 |
RUN go build -ldflags="-w -s" -o /go/bin/certstream-server-go $GOPATH/src/certstream-server-go/cmd/certstream-server-go/ |
| 30 |
RUN chown -R "${USER}:${USER}" /go/bin/certstream-server-go |
| 31 |
|
| 32 |
############################ |
| 33 |
# STEP 2 build a small image |
| 34 |
############################ |
| 35 |
FROM alpine |
| 36 |
|
| 37 |
WORKDIR /app |
| 38 |
|
| 39 |
# Import the user and group files from the builder. |
| 40 |
COPY --from=builder /etc/passwd /etc/passwd |
| 41 |
COPY --from=builder /etc/group /etc/group |
| 42 |
|
| 43 |
# Copy our static executable. |
| 44 |
COPY --from=builder /go/bin/certstream-server-go /app/certstream-server-go |
| 45 |
COPY --chown=certstreamserver:certstreamserver ./config.docker.yaml /app/config/config.yaml |
| 46 |
|
| 47 |
# Use an unprivileged user. |
| 48 |
USER certstreamserver:certstreamserver |
| 49 |
|
| 50 |
EXPOSE 8080 |
| 51 |
|
| 52 |
ENTRYPOINT ["/app/certstream-server-go", "-config", "/app/config/config.yaml"] |