FROM golang:1.22.5-alpine3.20 AS builder # Create appuser. ENV USER=appuser ENV UID=10001 RUN adduser \ --disabled-password \ --gecos "" \ --home "/nonexistent" \ --shell "/sbin/nologin" \ --no-create-home \ --uid "${UID}" \ "${USER}" WORKDIR $GOPATH/src/avatars/ COPY . . # Fetch dependencies. RUN go mod download RUN go mod verify # Build the binary. RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o /go/bin/avatars ############################ # STEP 2 build a small image ############################ FROM scratch # 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/avatars /go/bin/avatars # Set Work directory for relative paths of assets # WORKDIR /go/bin # Expose API Port EXPOSE 3382 # Use an unprivileged user. USER appuser:appuser # Run the hello binary. ENTRYPOINT ["/go/bin/avatars"]