Image builds can take some time
Images are made of layers defined by…
Build cache speeds up consecutive builds
Step 7/11 : ADD entrypoint.sh /
---> Using cache
---> a6b2bb261372
–
Dockerfile
Builds may not not run on the same host
Use local images to warm cache
docker pull myimage:1
docker build --cache-from myimage:1 --tag myimage:2
Internal build cache is ignored when using --cache-from
Added in Docker 1.13
Image must be present locally
–
# Push image
docker run -d -p 5000:5000 registry:2
docker build --tag localhost:5000/hello-world-java .
docker push localhost:5000/hello-world-java
# Reset Docker
docker system prune --all
# Pull image
docker pull localhost:5000/hello-world-java
# Build with cache from local image
docker build --cache-from localhost:5000/hello-world-java .
Internal build cache is still used when cache image does not exist
Large image downloads for few helpful layers
Use remote images to warm cache
Image layers will be downloaded as needed
Same syntax using --cache-from
Added in Docker 19.03
–
Build with cache from remote image:
# Run Docker 19.03-rc
docker run -d --rm --name dind --privileged --network host \
--volume $(pwd):/src --workdir /src docker:19.03-rc-dind
docker exec -it dind sh
# Run inside container
apk add --update-cache --no-cache curl jq
export DOCKER_BUILDKIT=1
docker build --tag localhost:5000/test:1 \
--build-arg BUILDKIT_INLINE_CACHE=1 .
docker push localhost:5000/test:1
docker system prune --all
docker build \
--cache-from localhost:5000/test:1 .
–
Check manifest for cache information:
curl -s \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
localhost:5000/v2/test/manifests/1 \
| jq --raw-output '.config.digest' \
| while read CONFIG_DIGEST; do \
curl -s \
-H "Accept: application/vnd.docker.container.image.v1+json" \
localhost:5000/v2/test/blobs/${CONFIG_DIGEST} \
| jq --raw-output '."moby.buildkit.cache.v0"' \
| base64 -d \
| jq; \
done