Skip to content

Docker buildkit + go module caching #702

@drewbailey

Description

@drewbailey

Is your task related to a problem? Please describe.

Currently incremental image builds are slow (make image takes a few minutes each run).

Describe the solution you'd like

Use buildkit & cache go modules

# syntax=docker/dockerfile:1
FROM docker.io/library/golang:1.24 AS builder
WORKDIR /src

# Copy go mod files first for better layer caching
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
    go mod download

# Copy source and build with caches
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    make argocd-agent
	
FROM docker.io/library/alpine:latest

COPY --from=builder /src/dist/argocd-agent /bin/argocd-agent
USER 999
ENTRYPOINT ["/bin/argocd-agent"]

or, at a minimum run go mod download as a separate layer

FROM docker.io/library/golang:1.24 AS builder
WORKDIR /src
+
+ # Copy go.mod and go.sum first for better layer caching
+ COPY go.mod go.sum ./
+ RUN go mod download
+
+ # Copy source code
  COPY . .
+
+ # Build the binary
  RUN make argocd-agent

Describe alternatives you've considered

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/taskTasks, chores, etc

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions