Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 8 additions & 5 deletions content/manuals/build/ci/github-actions/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,16 @@ 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
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=bind,source=go.mod,target=go.mod \
--mount=type=bind,source=go.sum,target=go.sum \
go mod download

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't recall for sure but I believe that I might've opted for COPY just to simplify the syntax on this page - it's specifically about cache mounts, so I probably wanted to make everything else as simple as possible. But the obvious downside of that is that we don't show the "optimal" pattern of bind mounting, which lead to this PR being raised.

I appreciate that it's easier to have all the best practices shown in the same example so I'm happy to get this in!

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 ./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