Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
modem7 opened this issue Oct 5, 2022 · 0 comments
Open

Comments

@modem7
Copy link

modem7 commented Oct 5, 2022

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant