Skip to content
Snippets Groups Projects
Dockerfile 1.29 KiB
Newer Older
  • Learn to ignore specific revisions
  • Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    # Building...
    
    FROM golang:alpine as server-builder
    
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    RUN apk update && apk add --no-cache git ca-certificates tzdata libcap mailcap && update-ca-certificates && rm -rf /var/cache/apk/*
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    # Create appuser
    ENV USER=appuser
    ENV UID=1000
    # See https://stackoverflow.com/a/55757473/12429735
    RUN adduser \
        --disabled-password \
        --gecos "" \
        --home "/nonexistent" \
        --shell "/sbin/nologin" \
        --no-create-home \
        --uid "${UID}" \
        "${USER}"
    
    WORKDIR /app
    
    
    Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    ADD . .
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    RUN chown -Rf "${UID}" ./*
    
    RUN go version
    
    Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    RUN go get -d -v && \
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
        CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go test ./...
    
    RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
        -ldflags='-w -s -extldflags "-static"' -a \
        -o /app/server .
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    RUN setcap cap_net_bind_service=+ep /app/server
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    # Running...
    FROM scratch
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    WORKDIR /app
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    COPY --from=server-builder /usr/share/zoneinfo /usr/share/zoneinfo
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    COPY --from=server-builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    COPY --from=server-builder /etc/passwd /etc/passwd
    COPY --from=server-builder /etc/group /etc/group
    COPY --from=server-builder /etc/mime.types /etc/mime.types
    
    # Copy static executable and application resources
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    COPY --from=server-builder /app/server /app/server
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    # Use an unprivileged user.
    USER appuser:appuser
    
    Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    
    ENTRYPOINT [ "./server"]