Docker BuildKit --mount=type=cache 不工作,为什么?
Docker BuildKit --mount=type=cache not working, why?
我正在尝试实施 Buildkit's cache mount feature.
我的 Docker 文件是:
# syntax = docker/dockerfile:experimental
FROM python:3.6-alpine
RUN --mount=type=cache,target=/root/.cache/pip pip install pyyaml
命令:docker build --progress=plain -t abc:1 . --no-cache
第一个运行输出:
#2 [internal] load .dockerignore
#2 transferring context: 2B done
#2 DONE 0.0s
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 175B done
#1 DONE 0.0s
#3 resolve image config for docker.io/docker/dockerfile:experimental
#3 DONE 2.8s
#4 docker-image://docker.io/docker/dockerfile:experimental@sha256:787107d7f...
#4 CACHED
#5 [internal] load metadata for docker.io/library/python:3.6-alpine
#5 DONE 0.0s
#6 [stage-0 1/2] FROM docker.io/library/python:3.6-alpine
#6 CACHED
#7 [stage-0 2/2] RUN --mount=type=cache,target=/root/.cache/pip pip install...
#7 1.410 Collecting pyyaml
#7 1.731 Downloading PyYAML-5.3.1.tar.gz (269 kB)
#7 2.470 Building wheels for collected packages: pyyaml
#7 2.471 Building wheel for pyyaml (setup.py): started
#7 2.801 Building wheel for pyyaml (setup.py): finished with status 'done'
#7 2.802 Created wheel for pyyaml: filename=PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl size=44621 sha256=0bfa8470e677b8c2e1d50f8749aa5890b5fb7d7d15352596fbd928c846f9922d
#7 2.802 Stored in directory: /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc
#7 2.803 Successfully built pyyaml
#7 2.835 Installing collected packages: pyyaml
#7 2.882 Successfully installed pyyaml-5.3.1
#7 DONE 3.3s
#8 exporting to image
#8 exporting layers
#8 exporting layers 0.1s done
#8 writing image sha256:d7ecd86c64611e14f55fa2d31b8f440246e356026eef7175526ccc921373c205 done
#8 naming to docker.io/library/abc:1 done
#8 DONE 0.1s
第二个运行输出:
#1 [internal] load .dockerignore
#1 transferring context: 2B done
#1 DONE 0.0s
#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 37B done
#2 DONE 0.0s
#3 resolve image config for docker.io/docker/dockerfile:experimental
#3 DONE 2.7s
#4 docker-image://docker.io/docker/dockerfile:experimental@sha256:787107d7f...
#4 CACHED
#5 [internal] load metadata for docker.io/library/python:3.6-alpine
#5 DONE 0.0s
#6 [stage-0 1/2] FROM docker.io/library/python:3.6-alpine
#6 CACHED
#7 [stage-0 2/2] RUN --mount=type=cache,target=/root/.cache/pip pip install...
#7 1.385 Collecting pyyaml
#7 1.773 Downloading PyYAML-5.3.1.tar.gz (269 kB)
#7 2.517 Building wheels for collected packages: pyyaml
#7 2.518 Building wheel for pyyaml (setup.py): started
#7 2.844 Building wheel for pyyaml (setup.py): finished with status 'done'
#7 2.846 Created wheel for pyyaml: filename=PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl size=44621 sha256=064a160d2f195ad7753fba5abf9481b40b515fadffc94f30aef20146209a0faa
#7 2.846 Stored in directory: /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc
#7 2.846 Successfully built pyyaml
#7 2.877 Installing collected packages: pyyaml
#7 2.920 Successfully installed pyyaml-5.3.1
#7 DONE 3.3s
#8 exporting to image
#8 exporting layers
#8 exporting layers 0.1s done
#8 writing image sha256:42525b426b00a8728b735c15efb96bd237fc020f46d6d99f5bfebb7758016e71 done
#8 naming to docker.io/library/abc:1 done
#8 DONE 0.1s
预期:
在第二个 运行 中,我期待从缓存中选择 pyyaml
。
OS:
Mac OS 卡特琳娜 (10.15.3)
Docker版本:
参考:
例如,您可以将 mode=0777
添加到 Dockerfile
以使其工作。
Dockerfile:
# syntax = docker/dockerfile:experimental
FROM python:3.6-alpine
RUN --mount=type=cache,mode=0777,target=/root/.cache/pip pip install pyyaml
这里,
mode: File mode for new cache directory in octal. Default 0755.
第一个运行:
#8 [stage-0 2/2] RUN --mount=type=cache,mode=0777,target=/root/.cache/pip p...
#8 2.085 Collecting pyyaml
#8 2.455 Downloading PyYAML-5.3.1.tar.gz (269 kB)
#8 3.484 Building wheels for collected packages: pyyaml
#8 3.485 Building wheel for pyyaml (setup.py): started
#8 3.864 Building wheel for pyyaml (setup.py): finished with status 'done'
#8 3.865 Created wheel for pyyaml: filename=PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl size=44621 sha256=372ab164f3e88d66fa2571fa048317b22a7c48bf723dfecbac4e058e9037c4fd
#8 3.865 Stored in directory: /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc
#8 3.866 Successfully built pyyaml
#8 3.901 Installing collected packages: pyyaml
#8 3.961 Successfully installed pyyaml-5.3.1
#8 DONE 4.7s
第二个运行:
#8 [stage-0 2/2] RUN --mount=type=cache,mode=0777,target=/root/.cache/pip p...
#8 2.069 Processing /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc/PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl
#8 2.112 Installing collected packages: pyyaml
#8 2.166 Successfully installed pyyaml-5.3.1
#8 DONE 2.5s
更新:
如果我用next做实验:
RUN --mount=type=cache,target=/root/.cache/pip ls -alh /root/.cache/pip;pip install pyyaml;ls -alh /root/.cache/pip
我可以看到下一个:
#7 [stage-0 2/2] RUN --mount=type=cache,target=/root/.cache/pip ls -alh /ro...
#7 0.405 total 8K
#7 0.405 drwxr-xr-x 2 root root 4.0K Apr 29 01:04 .
#7 0.405 drwxr-xr-x 3 root root 4.0K Apr 29 01:04 ..
#7 1.957 Collecting pyyaml
#7 2.572 Downloading PyYAML-5.3.1.tar.gz (269 kB)
#7 3.552 Building wheels for collected packages: pyyaml
#7 3.553 Building wheel for pyyaml (setup.py): started
#7 3.885 Building wheel for pyyaml (setup.py): finished with status 'done'
#7 3.887 Created wheel for pyyaml: filename=PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl size=44621 sha256=4c971f0f901ff8ed3a20c5bb7d94069f15249c665ebe611430db17cb87969cc7
#7 3.887 Stored in directory: /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc
#7 3.888 Successfully built pyyaml
#7 3.918 Installing collected packages: pyyaml
#7 3.972 Successfully installed pyyaml-5.3.1
#7 4.451 WARNING: You are using pip version 20.0.2; however, version 20.1 is available.
#7 4.451 You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
#7 4.512 total 20K
#7 4.512 drwxr-xr-x 5 root root 4.0K Apr 29 01:04 .
#7 4.512 drwxr-xr-x 3 root root 4.0K Apr 29 01:04 ..
#7 4.512 drwxr-xr-x 5 root root 4.0K Apr 29 01:04 http
#7 4.512 drwxr-xr-x 2 root root 4.0K Apr 29 01:04 selfcheck
#7 4.512 drwxr-xr-x 3 root root 4.0K Apr 29 01:04 wheels
#7 DONE 4.7s
Re运行 又是上面的,一样的。所以看起来如果没有设置 mode
,buildkit 将不会从 docker 主机装载缓存。但是如果设置模式mode
,即使是0755
也可以。
所以,现在我强烈猜测是MACOS上buildkit的一个bug,即:如果不设置mode
,它不会为你挂载缓存。但是 linux.
上的行为正常
我在 ubuntu 测试 docker 构建,并且有相同的错误。如果不设置模式,缓存不工作。
构建命令
DOCKER_BUILDKIT=1 docker build --progress=plain --no-cache --secret id=git,src=$PWD/gitconfig -f Dockerfile -t app .
Docker 文件
# syntax = docker/dockerfile:1.2
FROM golang:1.16.5 as builder
WORKDIR /code
COPY . .
RUN --mount=type=secret,id=git,target=/root/.gitconfig,required=true,mode=0666 \
--mount=type=cache,mode=0777,id=gocache,target=/root/.cache/go-build \
--mount=type=cache,mode=0777,id=gomodcache,target=/go/pkg/mod \
ls -lha /root/.cache/go-build && \
ls -lha /go/pkg/mod && \
GO_ENABLE="0" go build -o app -v main.go && \
ls -lha /root/.cache/go-build && \
ls -lha /go/pkg/mod
FROM ubuntu:20.04
COPY --from=builder /code/app /usr/local/bin/
ENTRYPOINT app
docker版本
Client:
Version: 20.10.2
API version: 1.41
Go version: go1.13.8
Git commit: 20.10.2-0ubuntu1~20.04.2
Built: Tue Mar 30 21:24:57 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server:
Engine:
Version: 20.10.2
API version: 1.41 (minimum version 1.12)
Go version: go1.13.8
Git commit: 20.10.2-0ubuntu1~20.04.2
Built: Mon Mar 29 19:10:09 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4-0ubuntu1~20.04.2
GitCommit:
runc:
Version: 1.0.0~rc95-0ubuntu1~20.04.1
GitCommit:
docker-init:
Version: 0.19.0
GitCommit:
我正在尝试实施 Buildkit's cache mount feature.
我的 Docker 文件是:
# syntax = docker/dockerfile:experimental
FROM python:3.6-alpine
RUN --mount=type=cache,target=/root/.cache/pip pip install pyyaml
命令:docker build --progress=plain -t abc:1 . --no-cache
第一个运行输出:
#2 [internal] load .dockerignore
#2 transferring context: 2B done
#2 DONE 0.0s
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 175B done
#1 DONE 0.0s
#3 resolve image config for docker.io/docker/dockerfile:experimental
#3 DONE 2.8s
#4 docker-image://docker.io/docker/dockerfile:experimental@sha256:787107d7f...
#4 CACHED
#5 [internal] load metadata for docker.io/library/python:3.6-alpine
#5 DONE 0.0s
#6 [stage-0 1/2] FROM docker.io/library/python:3.6-alpine
#6 CACHED
#7 [stage-0 2/2] RUN --mount=type=cache,target=/root/.cache/pip pip install...
#7 1.410 Collecting pyyaml
#7 1.731 Downloading PyYAML-5.3.1.tar.gz (269 kB)
#7 2.470 Building wheels for collected packages: pyyaml
#7 2.471 Building wheel for pyyaml (setup.py): started
#7 2.801 Building wheel for pyyaml (setup.py): finished with status 'done'
#7 2.802 Created wheel for pyyaml: filename=PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl size=44621 sha256=0bfa8470e677b8c2e1d50f8749aa5890b5fb7d7d15352596fbd928c846f9922d
#7 2.802 Stored in directory: /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc
#7 2.803 Successfully built pyyaml
#7 2.835 Installing collected packages: pyyaml
#7 2.882 Successfully installed pyyaml-5.3.1
#7 DONE 3.3s
#8 exporting to image
#8 exporting layers
#8 exporting layers 0.1s done
#8 writing image sha256:d7ecd86c64611e14f55fa2d31b8f440246e356026eef7175526ccc921373c205 done
#8 naming to docker.io/library/abc:1 done
#8 DONE 0.1s
第二个运行输出:
#1 [internal] load .dockerignore
#1 transferring context: 2B done
#1 DONE 0.0s
#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 37B done
#2 DONE 0.0s
#3 resolve image config for docker.io/docker/dockerfile:experimental
#3 DONE 2.7s
#4 docker-image://docker.io/docker/dockerfile:experimental@sha256:787107d7f...
#4 CACHED
#5 [internal] load metadata for docker.io/library/python:3.6-alpine
#5 DONE 0.0s
#6 [stage-0 1/2] FROM docker.io/library/python:3.6-alpine
#6 CACHED
#7 [stage-0 2/2] RUN --mount=type=cache,target=/root/.cache/pip pip install...
#7 1.385 Collecting pyyaml
#7 1.773 Downloading PyYAML-5.3.1.tar.gz (269 kB)
#7 2.517 Building wheels for collected packages: pyyaml
#7 2.518 Building wheel for pyyaml (setup.py): started
#7 2.844 Building wheel for pyyaml (setup.py): finished with status 'done'
#7 2.846 Created wheel for pyyaml: filename=PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl size=44621 sha256=064a160d2f195ad7753fba5abf9481b40b515fadffc94f30aef20146209a0faa
#7 2.846 Stored in directory: /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc
#7 2.846 Successfully built pyyaml
#7 2.877 Installing collected packages: pyyaml
#7 2.920 Successfully installed pyyaml-5.3.1
#7 DONE 3.3s
#8 exporting to image
#8 exporting layers
#8 exporting layers 0.1s done
#8 writing image sha256:42525b426b00a8728b735c15efb96bd237fc020f46d6d99f5bfebb7758016e71 done
#8 naming to docker.io/library/abc:1 done
#8 DONE 0.1s
预期:
在第二个 运行 中,我期待从缓存中选择 pyyaml
。
OS: Mac OS 卡特琳娜 (10.15.3)
Docker版本:
参考:
例如,您可以将 mode=0777
添加到 Dockerfile
以使其工作。
Dockerfile:
# syntax = docker/dockerfile:experimental
FROM python:3.6-alpine
RUN --mount=type=cache,mode=0777,target=/root/.cache/pip pip install pyyaml
这里,
mode: File mode for new cache directory in octal. Default 0755.
第一个运行:
#8 [stage-0 2/2] RUN --mount=type=cache,mode=0777,target=/root/.cache/pip p...
#8 2.085 Collecting pyyaml
#8 2.455 Downloading PyYAML-5.3.1.tar.gz (269 kB)
#8 3.484 Building wheels for collected packages: pyyaml
#8 3.485 Building wheel for pyyaml (setup.py): started
#8 3.864 Building wheel for pyyaml (setup.py): finished with status 'done'
#8 3.865 Created wheel for pyyaml: filename=PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl size=44621 sha256=372ab164f3e88d66fa2571fa048317b22a7c48bf723dfecbac4e058e9037c4fd
#8 3.865 Stored in directory: /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc
#8 3.866 Successfully built pyyaml
#8 3.901 Installing collected packages: pyyaml
#8 3.961 Successfully installed pyyaml-5.3.1
#8 DONE 4.7s
第二个运行:
#8 [stage-0 2/2] RUN --mount=type=cache,mode=0777,target=/root/.cache/pip p...
#8 2.069 Processing /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc/PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl
#8 2.112 Installing collected packages: pyyaml
#8 2.166 Successfully installed pyyaml-5.3.1
#8 DONE 2.5s
更新:
如果我用next做实验:
RUN --mount=type=cache,target=/root/.cache/pip ls -alh /root/.cache/pip;pip install pyyaml;ls -alh /root/.cache/pip
我可以看到下一个:
#7 [stage-0 2/2] RUN --mount=type=cache,target=/root/.cache/pip ls -alh /ro...
#7 0.405 total 8K
#7 0.405 drwxr-xr-x 2 root root 4.0K Apr 29 01:04 .
#7 0.405 drwxr-xr-x 3 root root 4.0K Apr 29 01:04 ..
#7 1.957 Collecting pyyaml
#7 2.572 Downloading PyYAML-5.3.1.tar.gz (269 kB)
#7 3.552 Building wheels for collected packages: pyyaml
#7 3.553 Building wheel for pyyaml (setup.py): started
#7 3.885 Building wheel for pyyaml (setup.py): finished with status 'done'
#7 3.887 Created wheel for pyyaml: filename=PyYAML-5.3.1-cp36-cp36m-linux_x86_64.whl size=44621 sha256=4c971f0f901ff8ed3a20c5bb7d94069f15249c665ebe611430db17cb87969cc7
#7 3.887 Stored in directory: /root/.cache/pip/wheels/e5/9d/ad/2ee53cf262cba1ffd8afe1487eef788ea3f260b7e6232a80fc
#7 3.888 Successfully built pyyaml
#7 3.918 Installing collected packages: pyyaml
#7 3.972 Successfully installed pyyaml-5.3.1
#7 4.451 WARNING: You are using pip version 20.0.2; however, version 20.1 is available.
#7 4.451 You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
#7 4.512 total 20K
#7 4.512 drwxr-xr-x 5 root root 4.0K Apr 29 01:04 .
#7 4.512 drwxr-xr-x 3 root root 4.0K Apr 29 01:04 ..
#7 4.512 drwxr-xr-x 5 root root 4.0K Apr 29 01:04 http
#7 4.512 drwxr-xr-x 2 root root 4.0K Apr 29 01:04 selfcheck
#7 4.512 drwxr-xr-x 3 root root 4.0K Apr 29 01:04 wheels
#7 DONE 4.7s
Re运行 又是上面的,一样的。所以看起来如果没有设置 mode
,buildkit 将不会从 docker 主机装载缓存。但是如果设置模式mode
,即使是0755
也可以。
所以,现在我强烈猜测是MACOS上buildkit的一个bug,即:如果不设置mode
,它不会为你挂载缓存。但是 linux.
我在 ubuntu 测试 docker 构建,并且有相同的错误。如果不设置模式,缓存不工作。
构建命令
DOCKER_BUILDKIT=1 docker build --progress=plain --no-cache --secret id=git,src=$PWD/gitconfig -f Dockerfile -t app .
Docker 文件
# syntax = docker/dockerfile:1.2
FROM golang:1.16.5 as builder
WORKDIR /code
COPY . .
RUN --mount=type=secret,id=git,target=/root/.gitconfig,required=true,mode=0666 \
--mount=type=cache,mode=0777,id=gocache,target=/root/.cache/go-build \
--mount=type=cache,mode=0777,id=gomodcache,target=/go/pkg/mod \
ls -lha /root/.cache/go-build && \
ls -lha /go/pkg/mod && \
GO_ENABLE="0" go build -o app -v main.go && \
ls -lha /root/.cache/go-build && \
ls -lha /go/pkg/mod
FROM ubuntu:20.04
COPY --from=builder /code/app /usr/local/bin/
ENTRYPOINT app
docker版本
Client:
Version: 20.10.2
API version: 1.41
Go version: go1.13.8
Git commit: 20.10.2-0ubuntu1~20.04.2
Built: Tue Mar 30 21:24:57 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server:
Engine:
Version: 20.10.2
API version: 1.41 (minimum version 1.12)
Go version: go1.13.8
Git commit: 20.10.2-0ubuntu1~20.04.2
Built: Mon Mar 29 19:10:09 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4-0ubuntu1~20.04.2
GitCommit:
runc:
Version: 1.0.0~rc95-0ubuntu1~20.04.1
GitCommit:
docker-init:
Version: 0.19.0
GitCommit: