Docker build 命令在 windowsfilter 文件夹中创建大量文件夹:目的是什么以及如何控制

Docker build command creates large amount of folders in windowsfilter folder: what's the purpose and how to control

我正在像这样构建一个新图像 docker build . -t test 并将主机 OS 文件夹中的内容复制到其中:

Docker文件

FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir test
COPY test /root/test
    #USING 'COPY' here instead of 'ADD' because of 

我在 Powershell 中得到了这个输出:

PS D:\Programs> docker build . -t test
Sending build context to Docker daemon  1.644GB
Step 1/7 : FROM mcr.microsoft.com/windows/servercore:ltsc2019
ltsc2019: Pulling from windows/servercore
4612f6d0b889: Pull complete
c67ded6868b6: Pull complete
Digest: sha256:1be9c8378b8c32d31538253a4b96169f5138a5f6d4c0a04e8d8f9a80b9ac8097
Status: Downloaded newer image for mcr.microsoft.com/windows/servercore:ltsc2019
 ---> d1724c2d9a84
Step 2/7 : SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
 ---> Running in 1bfefefbe433
Removing intermediate container 1bfefefbe433
 ---> 37de702deb33
Step 3/7 : RUN mkdir root
 ---> Running in e26d6b49ced7


    Directory: C:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         9/8/2021   1:07 PM                root


Removing intermediate container e26d6b49ced7
 ---> 451c4d3f3ea1
Step 4/7 : RUN cd root
 ---> Running in 74a228f8118f
Removing intermediate container 74a228f8118f
 ---> 3f175ac67f1d
Step 5/7 : WORKDIR /root
 ---> Running in 5f783d5b2332
Removing intermediate container 5f783d5b2332
 ---> 68b24e033f87
Step 6/7 : RUN mkdir test
 ---> Running in 5771bb7a593a


    Directory: C:\root


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         9/8/2021   1:08 PM                test


Removing intermediate container 5771bb7a593a
 ---> 35fa0b2af157
Step 7/7 : COPY test /root/test
 ---> 60eab8242865
Successfully built 60eab8242865
Successfully tagged test:latest

它在主机 OS 上的文件夹“\docker\windowsfilter”中创建八个(!)文件夹“

这似乎太过分了,它看起来像是我 Dockerfile 中每个步骤的文件夹,那么这些文件夹有什么用,我可以防止 creation/autodelete 不需要的文件夹以保持一切整洁吗?

主机OS:Windows服务器2019标准版本1809 17763.2114
Docker:版本 20.10.4,内部版本 110e091

请注意,我不使用 Docker 桌面应用程序, 我的安装方式 Docker。

更新 1

我根据@Noam 的建议更新了我的 Docker 文件,如下所示:

#RUN mkdir root
#RUN cd root
#WORKDIR /root
WORKDIR /root #creates root directory if not exists, then enters it

此更改后,在 windowsfilter 中创建了 6 个文件夹(而不是 8 个),所以我的直觉是 Docker 为 Docker 文件中的每个命令创建了一个文件夹似乎是正确。

你好@Flo 这个问题似乎是一个长期问题,特别是cidocker桌面,

原因:

有时似乎可以解释的一个选项是从旧版本的 docker 桌面升级,并且软件不会清理旧目录。

另一个 docker 将构建的图像层保存在那里,因此如果您有同一图像的多个版本,您的悬挂图像可能会驻留在那里请求 docker image prune 的 运行 甚至可能docker system prune

解决方案:

一个开源清理脚本已经发布,它似乎有很大帮助,它被称为 docker-ci-zap 并且在大多数情况下它似乎可以很好地处理它。

如果问题仍然存在,docker for desktop github issue page 中建议的修复方法是向 windowsfilter 目录中的文件夹名称添加 -删除 后缀通过重新启动以允许 docker 清理不需要的文件夹

如果它仍然导致问题,请尝试返回 factory deafults and if that doesn't do it either maybe a fresh docker for desktop install 可能会达成协议,尽管有些人遇到了一些困难,所以最终如果它真的很麻烦并且您需要使用它,重新安装 windows 通常看起来就像最后一击。

Dockerfile 改进建议

在您的 Dockerfile 中,描述文件夹的行可能会导致出现问题,因为非绝对路径和 Dockerfile 命令的使用不当。

替换:

RUN mkdir root
RUN cd root
WORKDIR /root

与:

WORKDIR /root

它达到相同的目的(创建并进入新的(如果尚不存在)目录)

docker-ci-zap 有什么作用?

你可以通过查看 the code 来大致了解驻留在 GitHub 存储库中的用于编译 exe 文件的文件:

它使用 hcsshim driver and its DestroyLayer 函数从主机磁盘中删除图层文件,或者如文档所述:

DestroyLayer will remove the on-disk files representing the layer with the given path, including that layer's containing folder, if any.