qdm12 / deunhealth

Binary program to restart unhealthy Docker containers
MIT License
117 stars 8 forks source link

Feature request: Upgrade Dockerfile to use Docker buildkit effectively #58

Open modem7 opened 2 years ago

modem7 commented 2 years ago

What's the feature? šŸ§

Given that Go has a lot of dependencies, leveraging the docker buildkit cache should be able to reduce time taken between builds effectively.

Optional extra information šŸš€

Here's a Dockerfile I edited, I can create a pull request if it's useful?:

Alpine and Go versions should probably also be updated in this.

# syntax = docker/dockerfile:latest
# Sets linux/amd64 in case it's not injected by older Docker versions
ARG BUILDPLATFORM=linux/amd64

ARG ALPINE_VERSION=3.15
ARG GO_VERSION=1.17
ARG XCPUTRANSLATE_VERSION=v0.6.0
ARG GOLANGCI_LINT_VERSION=v1.45.2

FROM --platform=${BUILDPLATFORM} qmcgaw/xcputranslate:${XCPUTRANSLATE_VERSION} AS xcputranslate
FROM --platform=${BUILDPLATFORM} qmcgaw/binpot:golangci-lint-${GOLANGCI_LINT_VERSION} AS golangci-lint

FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS base
ENV CGO_ENABLED=0
WORKDIR /tmp/gobuild
RUN <<EOF
    set -xe
    apk add --no-cache -U \
        git \
        g++
EOF
COPY --link --from=xcputranslate /xcputranslate /usr/local/bin/xcputranslate
COPY --link --from=golangci-lint /bin /go/bin/golangci-lint
COPY --link go.mod go.sum ./
RUN --mount=type=cache,id=gocache,target=/root/.cache/go-build \
    --mount=type=cache,id=gocache,target=/go/pkg/mod \
    <<EOF
    set -xe
    go mod download -x
EOF
COPY --link cmd/ ./cmd/
COPY --link internal/ ./internal/

FROM base AS test
# Note on the go race detector:
# - we set CGO_ENABLED=1 to have it enabled
# - we installed g++ in the base stage to support the race detector
ENV CGO_ENABLED=1
ENTRYPOINT go test -race -coverprofile=coverage.txt ./...

FROM base AS lint
COPY --link .golangci.yml ./
RUN <<EOF
    set -xe
    golangci-lint run --timeout=10m
EOF

FROM base AS build
ARG TARGETPLATFORM
ARG VERSION=unknown
ARG CREATED="an unknown date"
ARG COMMIT=unknown
RUN --mount=type=cache,id=gocache,target=/root/.cache/go-build \
    --mount=type=cache,id=gocache,target=/go/pkg/mod \
    <<EOF
    set -xe
    GOARCH="$(xcputranslate translate -targetplatform=${TARGETPLATFORM} -field arch)" \
    GOARM="$(xcputranslate translate -targetplatform=${TARGETPLATFORM} -field arm)" \
    go build -trimpath -ldflags="-s -w \
    -X 'main.version=$VERSION' \
    -X 'main.buildDate=$CREATED' \
    -X 'main.commit=$COMMIT' \
    " -o app cmd/app/main.go
EOF

FROM scratch
ENTRYPOINT ["/app"]
HEALTHCHECK --interval=10s --timeout=5s --start-period=5s --retries=2 CMD ["/app","healthcheck"]
ENV LOG_LEVEL=info \
    HEALTH_SERVER_ADDRESS=127.0.0.1:9999 \
    TZ=America/Montreal
ARG VERSION=unknown
ARG CREATED="an unknown date"
ARG COMMIT=unknown
LABEL \
    org.opencontainers.image.authors="quentin.mcgaw@gmail.com" \
    org.opencontainers.image.version=$VERSION \
    org.opencontainers.image.created=$CREATED \
    org.opencontainers.image.revision=$COMMIT \
    org.opencontainers.image.url="https://github.com/qdm12/deunhealth" \
    org.opencontainers.image.documentation="https://github.com/qdm12/deunhealth/blob/main/README.md" \
    org.opencontainers.image.source="https://github.com/qdm12/deunhealth" \
    org.opencontainers.image.title="deunhealth" \
    org.opencontainers.image.description="REstart your unhealthy containers in style"
COPY --link --from=build --chown=1000 /tmp/gobuild/app /app