为什么COPY指令在我的Docker构建中导致缓存未命中 [英] Why is the COPY instruction causing a cache miss in my Docker build

查看:115
本文介绍了为什么COPY指令在我的Docker构建中导致缓存未命中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目的docker文件中的复制说明导致缓存未命,即使没有文件被复制自从上次将映像推送到docker hub以来,已经发生了变化。这将导致所有后续层丢失高速缓存,从而使构建花费的时间大大超过了应有的时间。我还注意到,当我在本地计算机上 docker build 时,属于每一层的哈希似乎与它们不同。这可能是因为Docker版本不匹配吗?这里发生了什么?我该如何诊断呢?

解决方案

检查使用 docker历史记录构建的不同映像的层- -no-trunc $ image 。在复制步骤中,您将看到要复制的文件的 file:abc哈希:

 图像创建者SIZE COMMENT 
sha256:202cb043f70a2565ea40629e891642e1e24be7b52e29116a6520736f47183904 9分钟前/ bin / sh -c#(nop)COPY文件:d523f0d1cac93e44179baf9c36a7a4feff221b604224e26TX75b p>

如果正在构建的两个映像之间的哈希值不同,则将使构建缓存无效并导致未命中。请记住,文件的元数据也会导致缓存未命中,尤其是文件权限。如果您仍然遇到问题,请更新问题以包括 docker历史记录--no-trunc ... 来自不同版本的输出。


The copy instruction in the docker file for my project seems to cause a cache miss, even though none of the files being copied have changed since the image was last pushed to docker hub. This causes all subsequent layers to miss the cache which makes the build take much longer than it should. I've also noticed that the hashes belonging to each layer seem to be different than what they are when I docker build on my local machine. Could this be because of a docker version mismatch? What is going on here? How can I diagnose this?

解决方案

Check the layers of the different images being built with docker history --no-trunc $image. On the copy step, you'll see a "file:abc" hash of the files being copied:

IMAGE                                                                     CREATED             CREATED BY                                                                                                        SIZE                COMMENT
sha256:202cb043f70a2565ea40629e891642e1e24be7b52e29116a6520736f47183904   9 minutes ago       /bin/sh -c #(nop) COPY file:d523f0d1cac93e44179baf9c36a7a4feff221b604224e26900075ddb02812448 in /test/test.txt    12B

If that hash is different between the two images you are building, then that will invalidate the build cache and result in the miss. Keep in mind the metadata of the files can also cause a cache miss, especially the file permissions. If you're still having problems, then please update the question to include the docker history --no-trunc ... output from the different builds.

这篇关于为什么COPY指令在我的Docker构建中导致缓存未命中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆