By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Layer caching optimizes Docker builds by reusing previously built layers, reducing build time and storage usage. Use the --cache-from flag to leverage layer caching.
--cache-from
docker system df
docker build --cache-from <image_name> -t <image_name> .
docker container prune
docker image prune -a
Purpose: Build an image from a Dockerfile using cached layers.Command: docker build --cache-from my-base-image -t my-app-image . Flags: --cache-from leverages the cache from the specified image, -t sets the image name.
docker build --cache-from my-base-image -t my-app-image .
-t
Purpose: Build multiple images from a shared base image.Command: docker build --cache-from my-base-image -t my-app-image . && docker build --cache-from my-app-image -t my-extended-image . Flags: --cache-from leverages the cache from the specified image.
docker build --cache-from my-base-image -t my-app-image . && docker build --cache-from my-app-image -t my-extended-image .
Purpose: Use a private registry to store and reuse cached layers.Command: docker build --cache-from <registry_url>/<image_name> -t <image_name> . Flags: --cache-from leverages the cache from the specified registry image.
docker build --cache-from <registry_url>/<image_name> -t <image_name> .
Error message: Error: Cache not found for <image_name> Why it happens: The specified image does not exist in the cache.Fix: docker build --no-cache -t <image_name> . (build without caching)
Error: Cache not found for <image_name>
docker build --no-cache -t <image_name> .
Error message: Error: Cache conflict for <image_name> Why it happens: Multiple images with the same name are in the cache.Fix: docker image prune -a (prune unused images)
Error: Cache conflict for <image_name>
Error message: Error: Cache timeout for <image_name> Why it happens: The cache has timed out due to inactivity.Fix: docker system prune -af (prune all unused resources)
Error: Cache timeout for <image_name>
docker system prune -af
Situation: You want to check the current cache usage.Command: docker system df Correct response: A list of Docker images, their sizes, and cache hits.
Situation: You want to check the number of cache hits.Command: docker build --cache-from <image_name> -t <image_name> . Correct response: A list of layers being built, with cache hits marked.
Situation: You want to check for cache conflicts.Command: docker image prune -a Correct response: A list of unused images that can be pruned.
• --cache-from: Leverages the cache from the specified image.• --no-cache: Builds without caching.• docker system df: Displays Docker image and cache usage.• docker image prune -a: Prunes all unused images.• docker system prune -af: Prunes all unused resources.• docker container prune: Deletes all stopped containers.• docker build: Builds an image from a Dockerfile.• docker image: Manages Docker images.• docker system: Manages Docker system resources.• ⚠️ docker container prune deletes all stopped containers.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.