我在我的主机上创建了几个不同的目录,因为我试图学习Docker,只是为了让我的dockerfile有条理。我刚刚运行的Dockerfile是这样的:

FROM crystal/centos
MAINTAINER crystal

ADD ./rpms/test.rpm ./rpms/ 
RUN yum -y --nogpgcheck localinstall /rpms/test.rpm 

我的实际转速只有1gb。但是当我尝试执行sudo docker build -t="crystal/test" .时,我得到将构建上下文发送到docker守护进程3.5 GB。在您继续构建Docker映像的过程中,还有什么我不知道的吗?当我在主机上的其他目录中构建更多映像时,内存是否在不断积累?


当前回答

解决由不充分的.dockerignore排除引起的大型上下文问题的一个选项是使用以下命令:

Rg -uuu——ignore-file .dockerignore——files——排序路径。

哪个使用这个工具:https://github.com/BurntSushi/ripgrep

它工作得很好。

其他回答

如果你有一个.dockerignore文件,构建上下文仍然很大,你可以使用Silver Searcher检查什么被发送到docker构建上下文:

ag --path-to-ignore .dockerignore --files-with-matches

注意,某些**模式可能无法正常工作。

更多评论请参见Github: https://github.com/moby/moby/issues/16056

Docker客户端将整个“构建上下文”发送给Docker守护进程。构建上下文(默认情况下)是Dockerfile所在的整个目录(即整个rpm树)。

你可以设置一个.dockerignore文件来让Docker忽略一些文件。你可能想尝试一下。

或者,您可以将rpm文件夹移动到Dockerfile上面一个目录级别,并且只进行符号链接测试。rpm到Dockerfile的目录。


你经常会想要把。git文件夹添加到。dockerignore文件夹中,这是导致评论中一些用户150MB -> 5GB差异的原因。

如果你在创造图像和获取信息 将构建上下文发送到docker守护进程,该守护进程需要日志时间来复制,

然后添加.dockerignore文件。它应该包括文件或目录 不需要复制。

解决由不充分的.dockerignore排除引起的大型上下文问题的一个选项是使用以下命令:

Rg -uuu——ignore-file .dockerignore——files——排序路径。

哪个使用这个工具:https://github.com/BurntSushi/ripgrep

它工作得很好。

总结一下,如果你的Docker映像构建上下文太大,你可以做什么:

make sure you don't have unused files in the context (unused files - those that still untouchable during image building); add unused files and/or directories to .dockerignore (as mentioned here); make sure you specify the right folder as an image build context (as mentioned here); try to use BuildKit with DOCKER_BUILDKIT=1 (as mentioned here); try to split your project into smaller parts to optimize content of build context; if you have a complex project you may want to manually collect all necessary files in separate folder before build and use it as a build context for a specific image.