Skip to content
Snippets Groups Projects
Dockerfile 1.36 KiB
Newer Older
  • Learn to ignore specific revisions
  • Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    ##################################
    # STEP 1 build executable binary #
    ##################################
    FROM golang:alpine AS builder
    
    # Install git + SSL ca certificates.
    # Git is required for fetching the dependencies.
    # Ca-certificates is required to call HTTPS endpoints.
    RUN apk update && apk add --no-cache git ca-certificates tzdata && update-ca-certificates
    
    # Create appuser
    ENV USER=appuser
    ENV UID=10001
    
    # See https://stackoverflow.com/a/55757473/12429735
    RUN adduser \
        --disabled-password \
        --gecos "" \
        --home "/nonexistent" \
        --shell "/sbin/nologin" \
        --no-create-home \
        --uid "${UID}" \
        "${USER}"
    WORKDIR $GOPATH/src/mypackage/myapp/
    COPY . .
    
    # Fetch dependencies.
    RUN go get -d -v
    
    # Build the binary
    RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
        -ldflags='-w -s -extldflags "-static"' -a \
        -o /go/bin/app .
    
    ##############################
    # STEP 2 build a small image #
    ##############################
    FROM scratch
    
    # Import from builder.
    COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
    COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
    COPY --from=builder /etc/passwd /etc/passwd
    COPY --from=builder /etc/group /etc/group
    
    # Copy our static executable
    COPY --from=builder /go/bin/app /go/bin/app
    
    # Use an unprivileged user.
    USER appuser:appuser
    
    # Run the app binary.
    ENTRYPOINT ["/go/bin/app"]