build context for docker image very large

后端 未结 10 890

I have created a couple different directories on my host machine as I try to learn about Docker just to keep my dockerfiles organized. My Dockerfile I just ran looks like t

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 13:00

    Update 2019

    Starting from Docker v18.06 there is an option to use a new image builder called Build Kit.

    It's pre-bundled with the Docker, no need to install anything. It's backward compatible with the Dockerfile syntax, no need to change the Dockerfile.

    Legacy Docker Build vs New Docker BuildKit

    Here is an example of building an image with a huge unused file in the build directory:

    Legacy Docker Build:

    $ 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
    

    New 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
    

    The only change is the DOCKER_BUILDKIT=1 environment variable, the difference in time is huge.

    .dockerignore File

    Please note, that the .dockerignore file is still valid and useful. Some Dockerfile commands like COPY . . will still take into account the .dockerignore rules. But the side files in the build directory (not referenced in the Dockerfile) are not getting copied anymore as a "build context" by the BuildKit.

提交回复
热议问题