Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions content/manuals/build/cache/optimize.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ tool you're using. Here are a few examples:

```dockerfile
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -o /app/hello
```

Expand Down
10 changes: 4 additions & 6 deletions content/manuals/build/ci/github-actions/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,11 @@ Example Dockerfile in `build/package/Dockerfile`
FROM golang:1.21.1-alpine as base-build

WORKDIR /build
RUN go env -w GOMODCACHE=/root/.cache/go-build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this one would still be useful; the default would be ~/.cache/go-build, but as the mounts are hard-coded to a location, it could be useful to explicitly configure go to use a location (for the mounts).

Possibly it could be a custom path so that the cache-directory can be set up to a predictable location for situations where you want to run the build as a user (USER) that's not root 🤔


COPY go.mod go.sum ./
RUN --mount=type=cache,target=/root/.cache/go-build go mod download
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps @crazy-max or @tonistiigi have ideas on this part; yes, it looks like go mod download may download more; but it could depend on the situation; i.e., if all modules are downloaded, then possibly the cache could be re-used for multiple stages (instead of each stage downloading "just" the parts it needs).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For projects that don't vendor the dependencies this command is still useful so we should keep it imo.

the cache could be re-used for multiple stages (instead of each stage downloading "just" the parts it needs).

For multi-stage but also if doing a multiplatform build it will download it once similar to https://github.com/docker/compose/blob/6a2d16bd106bd84065e790b5c2b111509e8a6fd9/Dockerfile#L50-L54


COPY ./src ./
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also double-checking with @crazy-max and @tonistiigi on this one; i.e., if copying the source was intentional here (restoring cache or otherwise?).

These are always tricky as there's no "single" rule, some bits may depend on your situation.

RUN --mount=type=cache,target=/root/.cache/go-build go build -o /bin/app /build/src
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,target=. \
go build -o /bin/app /build/src
...
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,9 @@ FROM node:17.7-alpine3.14 AS client-builder
FROM golang:1.17-alpine AS builder
ENV CGO_ENABLED=0
WORKDIR /backend
COPY vm/go.* .
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod download
COPY vm/. .
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,source=vm/.,target=. \
go build -trimpath -ldflags="-s -w" -o bin/service

FROM alpine:3.15
Expand Down
3 changes: 0 additions & 3 deletions hack/releaser/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ FROM golang:${GO_VERSION}-alpine AS base
RUN apk add --no-cache openssl
ENV CGO_ENABLED=0
WORKDIR /src
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

FROM base AS releaser
RUN --mount=type=bind,target=. \
Expand Down