mirror of
https://github.com/docker/docs.git
synced 2026-03-27 14:28:47 +07:00
Merge pull request #22388 from Juneezee/go-mod-mounts
docs: use bind mounts in Go Dockerfile examples
This commit is contained in:
1
content/manuals/build/cache/optimize.md
vendored
1
content/manuals/build/cache/optimize.md
vendored
@@ -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
|
||||
```
|
||||
|
||||
|
||||
@@ -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
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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=. \
|
||||
|
||||
Reference in New Issue
Block a user