我在我的主机上创建了几个不同的目录,因为我试图学习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映像的过程中,还有什么我不知道的吗?当我在主机上的其他目录中构建更多映像时,内存是否在不断积累?


当前回答

我和FreeStyler有同样的问题。然而,我是从上下文上一个目录构建的。所以-f参数是正确的,上下文是不正确的。

project 
|
-------docker-dir 

从docker-dir建立以下是很好的

docker build -t br_base:0.1 . 

从dock-dir构建,构建上下文改变了。因此,我需要改变命令中的上下文。上下文由'给出。在上面的命令中。

项目目录中的新命令应该是

docker build -t br_base:0.1 ./base

这里的Context是由'./base'给出的

其他回答

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

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

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

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

总结一下,如果你的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.

我和FreeStyler有同样的问题。然而,我是从上下文上一个目录构建的。所以-f参数是正确的,上下文是不正确的。

project 
|
-------docker-dir 

从docker-dir建立以下是很好的

docker build -t br_base:0.1 . 

从dock-dir构建,构建上下文改变了。因此,我需要改变命令中的上下文。上下文由'给出。在上面的命令中。

项目目录中的新命令应该是

docker build -t br_base:0.1 ./base

这里的Context是由'./base'给出的

更新2019

从Docker v18.06开始,有一个选项可以使用一个名为Build Kit的新映像构建器。

它是预先与Docker捆绑在一起的,不需要安装任何东西。它向后兼容Dockerfile语法,无需更改Dockerfile。

传统Docker Build vs新Docker BuildKit

下面是一个用build目录中一个巨大的未使用的文件构建映像的例子:

遗留Docker构建:

$ time docker image build --no-cache .
Sending build context to Docker daemon  4.315GB
[...]
Successfully built c9ec5d33e12e

real    0m51.035s
user    0m7.189s
sys 0m10.712s

新Docker BuildKit:

$ time DOCKER_BUILDKIT=1 docker image build --no-cache .
[+] Building 0.1s (5/5) FINISHED                                                
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 37B                                        0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
[...]
 => => writing image sha256:ba5bca3a525ac97573b2e1d3cb936ad50cf8129eedfa9  0.0s

real    0m0.166s
user    0m0.034s
sys 0m0.026s

唯一的变化是DOCKER_BUILDKIT=1环境变量,时间差异很大。

dockerignore文件。

请注意,.dockerignore文件仍然有效且有用。一些Dockerfile命令,如COPY . .仍然会考虑到.dockerignore规则。但是build目录中的副文件(没有在Dockerfile中引用)不再被BuildKit复制为“构建上下文”。

在我的情况下,当我执行错误的-f参数-没有路径到Dockerfile所在的目录

docker build——no-cache -t nginx5 -f /home/DF/ dockerfile /home/DF/ - right

docker build——no-cache -t nginx5 -f /home/DF/Dockerfile -错误