Merge pull request #22388 from Juneezee/go-mod-mounts

docs: use bind mounts in Go Dockerfile examples
This commit is contained in:
Eng Zer Jun
2026-01-22 15:28:53 +08:00
committed by GitHub
parent 814047fe25
commit 9103dff58a
4 changed files with 10 additions and 13 deletions

View File

@@ -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
```

View File

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

View File

@@ -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

View File

@@ -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=. \