BuildKit optimizes Docker image builds by inspecting the Dockerfile and sending only needed content in the build context. Smaller contexts reduce transfer time, especially when building on a remote engine. section 17.1
Use BuildKit-related context optimizations to:
- Copy only required application folders with precise COPY instructions. section 17.1
- Exclude unnecessary files and folders with .dockerignore instead of adding complex COPY patterns. section 17.1
- Avoid deleting unwanted files in a later layer, because deleted files can remain part of the image layers. section 17.1
Compare build contexts
Build the original image, add .dockerignore, and rebuild without cache: docker image build -t diamol/ch17-build-context:2e-v3 --no-cache .; mv dockerignore.txt .dockerignore; docker image build -t diamol/ch17-build-context:2e-v4 --no-cache .
Figure 17.4 Using a .dockerignore file reduces the size of the build context and the time to send it.
See how .dockerignore reduces the build-context size and the time required to send it. section 17.1
