-
Notifications
You must be signed in to change notification settings - Fork 8.1k
docs: use bind mounts in Go Dockerfile examples #22388
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 go.mod go.sum ./ | ||
| RUN --mount=type=cache,target=/root/.cache/go-build go mod download | ||
Juneezee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 ./ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 | ||
| ... | ||
| ``` | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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 notroot🤔