GoogleContainerTools / GoogleContainerTools/skaffold

Support for docker bind mounts ` --mount=type=...`

Open
#9,151 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
15.9k
Forks
1.7k
Avg merge
3d 9h
Merged PRs (30d)
10

Description

### Expected behavior

### Information

When building using `docker build`

```
docker build -t docker.io/repo/image:v1 \
--target distroless \
--build-arg GOPATH=/Users/USER/.gvm/pkgsets/go1.21.3/global \
--build-arg GOCACHE=/Users/USER/Library/Caches/go-build \
.
```

I can provide e.g. `GOPATH` and `GOCACHE` which will be used for caching the go modules and build artifacts across docker builds.

This is used in the `Dockerfile` in the following way:

```
FROM golang:1.21.3 as builder

WORKDIR /workspace

ARG GOPATH
ARG GOCACHE
# Use cache mounts to cache Go dependencies and bind mounts to avoid unnecessary
# layers when using COPY instructions for go.mod and go.sum.
# https://docs.docker.com/build/guide/mounts/
RUN --mount=type=cache,target=$GOPATH/pkg/mod \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
go mod download -x

COPY main.go main.go
COPY apis/ apis/
COPY controllers/ controllers/
COPY pkg/ pkg/
COPY internal/ internal/
COPY Makefile Makefile
COPY .git/ .git/

# Use cache mounts to cache Go dependencies and bind mounts to avoid unnecessary
# layers when using COPY instructions for go.mod and go.sum.
# https://docs.docker.com/build/guide/mounts/
RUN --mount=type=cache,target=$GOPATH/pkg/mod \
--mount=type=cache,target=$GOCACHE \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
go build .....
```

But apparently this cannot be used in skaffold because when using `--mount ...` in `Dockerfile` every other mount/filesystem (not sure) is read only and files/directories copied with `COPY` are not available. So in the above example the last `RUN` doesn't have access to `Makefile`. If I add

```
--mount=type=bind,source=.,target=. \
```

I'll then get:

```
0.207 go build -o bin/manager -ldflags "-s -w \
0.207 main.go
20.42 command-line-arguments: go build command-line-arguments: mkdir bin/: read-only file system
```

- Skaffold version: 2.8

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.