October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Docker Layer Duplication: What Deleting or Replacing Files Really Costs

A Docker layer deletion hides a file from the merged filesystem but does not erase its bytes from an earlier layer. Learn which build practices address image size, cache reuse, and runtime copy-up.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Deleting or replacing a file in a later Dockerfile instruction usually does not reclaim the bytes stored by an earlier image layer. The later layer changes the container’s merged filesystem view, but the earlier layer remains part of the image. To keep temporary files out of the final image, create and remove them in the same filesystem-changing instruction; use multi-stage builds to leave build-only tools and outputs behind.

Why a later deletion does not shrink an earlier layer

A Docker image is assembled from filesystem layers. When a later layer deletes a file that an earlier layer added, the deletion hides that file in the merged filesystem presented to the container. It does not reach back and erase the bytes recorded in the earlier layer. Docker describes this storage behavior in its storage drivers documentation.

As an Amazon Associate I earn from qualifying purchases.

Replacing a file has the same underlying issue: the earlier version remains represented in its layer, while the replacement is recorded later. The active filesystem view may show only the replacement, but the image contains the changes from both layers. That is the duplication penalty: one version is no longer visible, yet its stored bytes have not been removed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep temporary files and their cleanup in one instruction

If package indexes, archives, or other temporary files are created during an image build, remove them in the same filesystem-changing instruction when the goal is to avoid retaining them in the image. For example:

RUN apt-get update 
    && apt-get install -y --no-install-recommends ca-certificates 
    && rm -rf /var/lib/apt/lists/*

Because the installation and cleanup happen within one RUN, the temporary package lists do not remain as additions in an earlier image layer followed by a deletion in a later one. Docker’s storage-driver guidance explicitly warns that removing cache files in another layer does not remove their contribution from the prior layer.

This is not a reason to combine every Dockerfile operation into one enormous RUN. Combine operations when intermediate files must disappear together; retain useful boundaries where they support cache reuse and make the build easier to maintain.

Separate image size from build-cache reuse

Docker’s build cache is a separate concern from the bytes stored in the final image. When an instruction’s inputs change, Docker may need to rebuild that layer and subsequent layers. Placing relatively stable dependency manifests before frequently edited application source can let dependency-installation layers remain reusable when only source files change.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, a project may copy its dependency manifest, install dependencies, and only then copy the rest of its source. The exact files and commands depend on the project’s dependency model. Docker’s build cache documentation explains downstream invalidation, and its cache-use example demonstrates ordering package metadata before the rest of an application.

Good ordering can shorten rebuilds; it does not erase bytes already recorded in image layers. Treat cache reuse and final-image contents as distinct goals.

Use multi-stage builds to exclude build-only contents

A multi-stage build lets one stage compile or assemble an application while a later runtime stage starts from an appropriate base and copies in only selected outputs. This keeps compilers, development dependencies, and intermediate artifacts out of the final runtime image when they are not needed to run the application. Docker covers this approach in its building best practices.

Choose the copied artifacts deliberately: the runtime stage must still include everything the application needs to start and operate. Multi-stage builds solve a different problem from cleaning temporary files in one instruction: they separate build-time contents from the final runtime image.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When COPY –link can help

In supported Dockerfile and BuildKit workflows, COPY --link can put copied files in a layer that is independent of preceding layers. That can make the copied layer reusable across changes to earlier layers. Consult the Dockerfile reference and verify that the builder and Dockerfile syntax used by your project support the option before relying on it.

Best Value
Docker Container Linux Devops Programming Coding T-Shirt
  • Docker, Docker Swarm, Docker Compose, Programmer, Developer, Coding, Programming, Software Engineer, Code, DevOps, Deploy, Deployment, Kubernetes, Salt, Puppet, Chef, Terraform, Container, AWS, Azure, Cloud, Geek, Funny, Computer, Software, Tech, IT
  • Integration, Scrum, Compile, Compilation, Science, Bug, Debug, Python, Linux, Java, Javascript, Scala, Dotnet, Kotlin
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

COPY --link is a cache and layer-reuse feature, not a cleanup mechanism. It does not remove unwanted files that were already added to another layer.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do not confuse build layers with runtime OverlayFS copy-up

There is a separate use of “copy” that can matter after a container starts. Under the OverlayFS storage driver, the first time a container writes to a file that exists in a lower image layer, OverlayFS copies the whole file into the container’s writable upper directory; the write then applies to that copy. A small edit to a large file can therefore involve copying the entire file on that first write. Later writes use the upper copy rather than repeating the initial copy-up. See Docker’s OverlayFS storage-driver documentation.

This runtime behavior concerns write work and the container’s writable layer. It is not the reason a build-time deletion leaves bytes in an image, and it should not be generalized to storage drivers other than OverlayFS.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the fix for the problem you actually have

  • Unwanted temporary files in the final image: create and clean them in the same filesystem-changing instruction.
  • Build tools or intermediate outputs in the runtime image: use a multi-stage build and copy only the required artifacts.
  • Slow rebuilds after source edits: place stable dependency inputs before frequently changing source where the project permits, and preserve useful cache boundaries.
  • A copied layer that should survive changes to earlier layers: consider COPY --link only with a supporting builder and syntax.
  • Slow first writes to lower-layer files at runtime: assess the OverlayFS copy-up behavior separately from image-layer cleanup.

Docker’s reviewed documentation does not provide a general numeric estimate of image-size or build-time savings from avoiding layer duplication; the result depends on which files and build steps are involved.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.