Skip to main content

Why does Docker cache layers but not RUN commands that modify external states?

Junior Docker
Quick Answer Docker caches layers based on the instruction and its context at build time. RUN commands that download from the internet (apt-get, curl) are cached based on the instruction text รขโ‚ฌโ€ not the external content. So if an apt package updates, Docker still uses the cached layer. To force a fresh download, break the cache manually.

Answer

Docker caches based on file changes only. External states do not invalidate cache.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice