Docker Cache – How to Do a Clean Image Rebuild and Clear Docker's Cache

Ask questions Research chat →

https://www.freecodecamp.org/news/docker-cache-tutorial/ · scraped

deploy

Attachments

Scraped Content

— 1038 words · 2026-02-14 17:42:05 UTC ·

Excerpt

![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/e87251c8-a9c8-4547-bce7-b0ccf7bcc900/docker-cache-guide.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB4663U6KS4MA%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174205Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJGMEQCIAlKU8d%2BiVfxqplZq7Y4k0GSxVmmD6OylQ2LRRlhyD9xAiAa4pVs6l9ZMDXww9qle7TvYTMFD9wssy1ZY9qVqWMNCCr%2FAwgKEAAaDDYzNzQyMzE4MzgwNSIM3VasAmyZRPuurxmsKtwDIHFsoUCsRB5oSUy10jw7lWj9Ekxk9kBKjb%2B109mi%2FKVlduvd4L6Pz5ZuXXJLZOVsWgA4tfXoXgr1Xlg40x6Uh8lY%2FHeTasaHzfByCsY4Nqn2vMsD7Sw6NfIMaFpscT%2FMZYg1TwguvAJB%2Bv1olVKO1ca62jv82z%2FcXlbsQ2O8dwOP0%2BY%2BprM%2Fr8eW8tHualkWefoqUkrSS1TuidsoVBCAnuKbxUijlF%2BzI4f5Xi40klN92YdEEt%2FZ4d%2F6CpuVMScgqEWoGjugPkX1B%2BW4LWpg6CvDs%2BXvR42S0U%2FhryUFknoPdi6xzOauojHOdAwWTi9at0kmKvqGg7ZKY7DRv5p0fSLhmGCyBWVzAUcfRYNiTk3ZFEyW0stnaSKUtJB0fpiKlCqoKt5qNwNQ2y5Mi9gb3CRaR9p9
![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/e87251c8-a9c8-4547-bce7-b0ccf7bcc900/docker-cache-guide.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB4663U6KS4MA%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174205Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJGMEQCIAlKU8d%2BiVfxqplZq7Y4k0GSxVmmD6OylQ2LRRlhyD9xAiAa4pVs6l9ZMDXww9qle7TvYTMFD9wssy1ZY9qVqWMNCCr%2FAwgKEAAaDDYzNzQyMzE4MzgwNSIM3VasAmyZRPuurxmsKtwDIHFsoUCsRB5oSUy10jw7lWj9Ekxk9kBKjb%2B109mi%2FKVlduvd4L6Pz5ZuXXJLZOVsWgA4tfXoXgr1Xlg40x6Uh8lY%2FHeTasaHzfByCsY4Nqn2vMsD7Sw6NfIMaFpscT%2FMZYg1TwguvAJB%2Bv1olVKO1ca62jv82z%2FcXlbsQ2O8dwOP0%2BY%2BprM%2Fr8eW8tHualkWefoqUkrSS1TuidsoVBCAnuKbxUijlF%2BzI4f5Xi40klN92YdEEt%2FZ4d%2F6CpuVMScgqEWoGjugPkX1B%2BW4LWpg6CvDs%2BXvR42S0U%2FhryUFknoPdi6xzOauojHOdAwWTi9at0kmKvqGg7ZKY7DRv5p0fSLhmGCyBWVzAUcfRYNiTk3ZFEyW0stnaSKUtJB0fpiKlCqoKt5qNwNQ2y5Mi9gb3CRaR9p9lRUUPzYQ%2BkFBcxRrQ6zBQHyCa0DHsZVWZkkaEZnUZtthKcQWviui5SYknVMYPdvyqMlpuxkbpS3F3Z5pPDZcO4vm4K9WmDHDNVE%2FexJ9MaeqZRBpxMr2Ut1oK3dz0pH%2Bi9tOThzorxP2wjjioYC6D9LVEMzRqHzXAyJTQu0PwHuEq2NEfrbb4cK88lpzWd%2FOBpV7eYX%2F%2BD%2F9T9UMceBj7J13LpvFQmIwiNLCzAY6pgHRv14aD4tKKO1%2FzoKVk7c6gbkSjkDEMN4ZM%2ByA7lyph0L20jSOnXsoxXOMPnVqEUtaB%2FRE2Ytcg%2FXkqN%2FOcEb6y%2Bj8PN79eiZyGKKk4zASBWbu92adL%2BWJXpc%2FZtEXjkSttvy5KQU9SSqvlOJ1TazHlsNHIkTl5hMEkkXHOJvjSayX55mCFN7gXIGRvxaliy%2Bldc0bY%2FRM6uM3sGjxWbDJqjZrMfwf&X-Amz-Signature=88f207d72dce233cba2bed2e6ead8799345e4338ebbaeda29ad0e9897ca1d83c&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) Containers enable you to package your application in a portable way that can run in many environments. The most popular container platform is Docker. This tutorial will explain how to use the Docker build cache to your advantage. ## Docker Build Cache Building images should be fast, efficient, and reliable. The concept of Docker images comes with immutable layers. Every command you execute results in a new layer that contains the changes compared to the previous layer. All previously built layers are cached and can be reused. But, if your installation depends on external resources, the Docker cache can cause issues. ## How to Leverage the Docker Build Cache To understand Docker build-cache issues, let’s build a simple custom nginx Docker application. Before you build the image, create a Dockerfile that updates libraries and adds a custom startpage: ```plain text FROM nginx:1.21.6 # Update all packages RUN apt-get update && apt-get -y upgrade # Use a custom startpage RUN echo '<html><bod>My Custom Startpage</body></html>' > /usr/share/nginx/html/index.html ``` You can now build the Docker image: ```plain text $ docker build -t my-custom-nginx . => [1/3] FROM docker.io/library/nginx:1.21.6@sha256:e12... 5.8s => [2/3] RUN apt-get update && apt-get -y upgrade 3.6s => [3/3] RUN echo '<html><bod>My Custom Startpage... 0.2s => exporting to image 0.1s => exporting layers 0.1s => writing image 0.0s => naming to docker.io/library/my-custom-nginx [+] Building 11.3s (7/7) FINISHED ``` In this example, I removed some output for readability. If you build the image the first time, you see that it takes quite some time, in my case 11.3s. One long executing step is apt-get update && apt-get -y upgrade depending on how many dependencies are updated and how fast your internet speed is. It checks for package updates on the operation system and installs them if available. Now, you execute it again, and you benefit from the Docker build cache: ```plain text $ docker build -t my-custom-nginx . => [1/3] FROM docker.io/library/nginx:1.21.6@sha256:e1211ac1… 0.0s => CACHED [2/3] RUN apt-get update && apt-get -y upgrade 0.0s => CACHED [3/3] RUN echo '<html><bod>My Custom Startpage... 0.0s => exporting to image 0.0s => exporting layers 0.0s => writing image 0.0s => naming to docker.io/library/my-custom-nginx Building 1.1s (7/7) FINISHED ``` This time, the image build is very fast because it can reuse all previously built images. When you customize your startpage in the Dockerfile, you see how the caching behavior is affected: ```plain text FROM nginx:1.21.6 # Update all packages RUN apt-get update && apt-get -y upgrade # Use a custom startpage RUN echo '<html><bod>New Startpage</body></html>' > /usr/share/nginx/html/index.html ``` Now, build the image again: ```plain text $ docker build -t my-custom-nginx . => [1/3] FROM docker.io/library/nginx:1.21.6@sha256:e1211ac1… 0.0s => CACHED [2/3] RUN apt-get update && apt-get -y upgrade 0.0s => [3/3] RUN echo '<html><bod>My Custom Startpage... 0.2s => exporting to image 0.0s => exporting layers 0.0s => writing image 0.0s => naming to docker.io/library/my-custom-nginx Building 2.1s (7/7) FINISHED ``` This time it only rebuilt the last layer because it recognized that the RUN command had changed. But, it reused the intense 2nd build step and did not update operation system dependencies. The caching behavior is intelligent. Once 1 step needs to rebuild, every subsequent step is built again. Therefore, it’s good to put frequently changing parts at the end of a Dockerfile to reuse previous build layers. Still, maybe you want to force a rebuild of a cached layer to force a package update. Forcing a rebuild can be necessary because you want to keep your application safe and use the newest updates when available. ## How to Use the Docker Build -no-cache Option There can be different reasons for disabling the build-cache. You can rebuild the image from the base image without using cached layers by using the --no-cache option. ```plain text $ docker build -t my-custom-nginx . => CACHED [1/3] FROM docker.io/library/nginx:1.21.6@sha256:... 0.0s => [2/3] RUN apt-get update && apt-get -y upgrade 3.5s => [3/3] RUN echo '<html><bod>My Custom Startpage... 0.2s => exporting to image 0.1s => exporting layers 0.0s => writing image 0.0s => naming to docker.io/library/my-custom-nginx Building 5.5s (7/7) FINISHED ``` New layers were constructed and used. The docker build runs both commands this time, which comes with an all-or-nothing approach. Either you provide the --no-cache option that executes all commands, or you will cache as much as possible. ## How to Use Docker Arguments for Cache-Busting Another option allows providing a little starting point in the Dockerfile. You need to edit your Dockerfile like this: ```plain text FROM nginx:1.21.6 # Update all packages RUN apt-get update && apt-get -y upgrade # Custom cache invalidation ARG CACHEBUST=1 # Use a custom startpage RUN echo '<html><bod>New Startpage</body></html>' > /usr/share/nginx/html/index.html ``` You add a CACHEBUST argument to your Dockerfile at the location you want to enforce a rebuild. Now, you can build the Docker image and provide an always different value that causes all following commands to rerun: ```plain text $ docker build -t my-custom-nginx --build-arg CACHEBUST=$(date +%s) . => [1/3] FROM docker.io/library/nginx:1.21.6@sha256:e1211ac1... 0.0s => CACHED [2/3] RUN apt-get update && apt-get -y upgrade 0.0s => [3/3] RUN echo '<html><bod>My Custom Startpage... 0.3s => exporting to image 0.0s => exporting layers 0.0s => writing image 0.0s => naming to docker.io/library/my-custom-nginx Building 1.0s (7/7) FINISHED ``` By providing --build-arg CACHEBUST=$(date +%s), you set the parameter to an always different value that causes all following layers to rebuild. ## Summary Docker’s build-cache is a handy feature. It speeds up Docker builds due to reusing previously created layers. You can use the --no-cache option to disable caching or use a custom Docker build argument to enforce rebuilding from a certain step. Understanding the Docker build cache is powerful and will make you more efficient in building your Docker container. I hope you enjoyed this article. If you liked it and feel the need to give me a round of applause or just want to get in touch, follow me on Twitter. I work at eBay Kleinanzeigen, one of the world’s biggest classified companies. By the way, we are hiring! ## References If you read this far, tweet to the author to show them you care. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation