✦ For everyone, free.

Practical knowledge for real and everyday life

Home

8.1.1.4 Copy Write Cost

A focused guide to Copy Write Cost, connecting core concepts with practical Docker and container operations.

Copy-on-write cost is the performance and storage overhead incurred specifically the first time a container modifies a file that originates from a read-only image layer, since that modification requires copying the entire file into the writable layer before the change can actually be applied.

Why the First Modification Carries This Cost

Before a write to a file from a read-only layer can take effect, the storage driver must first copy that file's full content into the container's writable layer — only after this copy does the actual modification get applied.

docker exec myapp sh -c "echo 'one more line' >> /app/large-data-file.csv"

If large-data-file.csv is large and originates from the image's read-only layers, this single append operation triggers copying the entire file before the small addition can actually be written.

Why Subsequent Modifications Don't Repeat This Cost

Once a file has been copied into the writable layer (whether due to a previous modification), further changes to that same file apply directly within the writable layer without needing another full copy.

docker exec myapp sh -c "echo 'another line' >> /app/large-data-file.csv"

This second modification, occurring after the file already exists in the writable layer, does not repeat the initial copy-on-write cost.

Why This Cost Can Matter for Specific Workloads

Applications that frequently modify large files originating from the image's read-only layers can experience a meaningful, repeated performance cost from this copy-on-write behavior, particularly if those modifications are spread across many different large files rather than concentrated in files already copied into the writable layer.

docker stats myapp

Monitoring resource usage during heavy file modification activity can help reveal whether copy-on-write overhead is contributing meaningfully to observed performance characteristics.

Mitigating This Cost When It Matters

For workloads genuinely sensitive to this overhead, structuring an application to write large, frequently modified files to a mounted volume (bypassing the layered filesystem and its copy-on-write behavior entirely) avoids this cost.

docker run -d -v large-data:/app/data myapp:1.0
Why Copy-on-Write Cost Matters

Understanding this specific overhead helps explain otherwise puzzling performance characteristics for workloads that frequently modify large files inherited from an image's read-only layers, and informs the decision to use a volume for data genuinely sensitive to this particular cost.