我不完全理解docker
的RUN --mount=type=cache
机制,即使阅读了以下文章,
- https://docs.docker.com/build/cache/
- https://vsupalov.com/buildkit-cache-mount-dockerfile/的
- Difference between --cache-to/from and --mount type=cache in docker buildx build的
*mount=type=cache更深入的解释https://github.com/moby/buildkit/issues/1673
这是一个简单的dockerfile:
FROM debian
ENV GOPATH=/go
WORKDIR ${GOPATH}/src/github.com/my/prj
# Copy sources
COPY ./ ./
RUN ls -l
RUN --mount=type=cache,target=${GOPATH} ls -l
字符串
我试图做的是缓存go mod download
和所有去构建缓存,然而,这是我得到的:
#5 [internal] load build context
#5 transferring context: 2.04kB done
#5 DONE 0.0s
#6 [builder 2/5] WORKDIR /go/src/github.com/my/prj
#6 CACHED
#7 [builder 3/5] COPY ./ ./
#7 DONE 0.0s
#8 [builder 4/5] RUN ls -l
#8 1.555 total 312
#8 1.555 -rw-r--r-- 1 root root 23796 Dec 2 21:59 History.md
#8 1.555 -rw-r--r-- 1 root root 1097 Dec 2 21:59 LICENSE
#8 1.555 -rw-r--r-- 1 root root 600 Dec 2 21:59 Makefile
#8 1.555 -rw-r--r-- 1 root root 15 Dec 2 21:59 Procfile
#8 1.555 -rw-r--r-- 1 root root 57837 Dec 2 21:59 README.md
#8 1.555 -rwxr-xr-x 1 root root 714 Dec 2 21:59 benchmark.sh
#8 1.555 -rw-r--r-- 1 root root 6093 Dec 2 21:59 controllers.go
#8 1.555 -rw-r--r-- 1 root root 3823 Dec 2 21:59 error.go
#8 1.555 -rw-r--r-- 1 root root 459 Dec 2 21:59 error_test.go
#8 1.555 -rw-r--r-- 1 root root 276 Dec 2 21:59 go.mod
#8 1.555 -rw-r--r-- 1 root root 6439 Dec 2 21:59 go.sum
. . .
#8 1.555 -rw-r--r-- 1 root root 318 Dec 2 21:59 version.go
#8 DONE 1.6s
#9 [builder 5/5] RUN --mount=type=cache,target=/go ls -l
#9 1.881 total 4
#9 1.881 -rw-r--r-- 1 root root 45 Dec 2 22:58 go.mod
#9 DONE 1.9s
型
为什么使用--mount=type=cache
时我的文件突然消失了?如何清除这样的--mount=type=cache
,同时保留所有其他Docker缓存,如步骤缓存或apt缓存?使用COPY . .
时,是否仍然可以缓存go mod download
和所有go build缓存?
1条答案
按热度按时间zxlwwiss1#
Dockerfile中似乎缺少
# syntax = docker/dockerfile:1.2
。只需确保它被声明为Dockerfile中的第一行。此注解告诉Docker您正在使用BuildKit功能。