DEV Community

orca forge
orca forge

Posted on Originally published at forge.workstyle.tech

Why container image deletions persist with layered builds

📝 Originally published (in Japanese) at forge.workstyle.tech.

TL;DR Layered image builds include added or overwritten files, but do not carry forward deletions from the base image. In this case, two files deleted in the 10-09 change remained in the image after build time fell from 18 minutes to a few seconds. Use whiteout files or a full build to apply deletions, and check file contents, ownership, and mode before deployment.

The Conclusion First

Adding a diff layer lets you apply additions and overwrites, but deletions of files in the original image are not carried through. To apply deletions, use whiteouts (files that mark deletions) or perform a full build. Before stacking layers, also check their contents, ownership, and mode.

Why Deletions Persist in Overlay Layers

An overlay layer bundles changed files and adds them on top of a production image. This approach can add or overwrite files, but deletions made before the layer was created are not included in later layers. Files deleted in Git were still present in images used by several services.

At first, I thought the change that deleted the files had not been applied. So I compared the entire app/ directory and sorted the differences into three groups: “different contents,” “only in Git,” and “only in the Pod.” When I checked the files that existed only in the Pod (the environment where the app is running), I found that files I thought I had deleted were still there.

Some of the leftover files were .pyc files. If .pyc files remain, the deleted code can still be imported. I added RUN rm and included .pyc files in the cleanup. When using overlay layers, you also need to explicitly apply deletions from the base image.

How to Check Contents and Ownership Before Applying a Layer

I tried to create a layer containing only the files changed in another task. The first tar archive included directory entries as well as files. The directories were owned by root.

In production, the directories belong to the application user. If I had applied that layer as-is, their ownership would have changed to root. I caught this by inspecting the contents with tar -tv before deployment, then rebuilt the layer with only the files using tar --no-recursion --owner=<uid> --group=<gid>. I also set the mode to 664 to match production.

After deployment, I checked the file contents’ MD5 hashes and ownership inside the pod and confirmed that both matched production. Match the ownership and mode to production, as well as including only the files you want in the layer.

What Kind of Fix Requires a Full Rebuild?

A patch layer is enough when you can update the image simply by adding the changed files. An older authentication library always rejected tuple values passed as the issuer, which meant every login was rejected. Fixing this required a full rebuild.

Patch layers shorten build times, but they can’t fix state outside the patch. Don’t try to handle a fix that requires deleting files or rebuilding everything with a patch alone. Also, the created timestamp on an image built with a patch layer comes from the base image, so it doesn’t tell you when the patch was added.

How to Report and Apply Deletions

After this work, I established two operational rules. Reports must always include a list of deleted files, and deletions must be called out explicitly as part of the diff, just like additions and changes. When applying deletions, use whiteouts (.wh.<name>) or perform a full build. Simply adding a diff layer does not remove files from the original image.

Reducing creation time from 18 minutes to a few seconds was an improvement. But building quickly and reaching the intended state are two different things. An update is complete only after confirming that the diff layer contains the right files, that deleted files are gone, and that ownership and mode match production.

Key takeaways

  • Compare all of app/ and classify differences as content mismatches, Git-only files, or Pod-only files.
  • Check Pod-only files for deleted files that remain, including .pyc files.
  • List deleted files in the report and apply deletions with whiteout (.wh.<name>) or a full build.
  • Inspect layer contents with tar -tv before stacking; after deployment, compare md5 and ownership in the Pod with production.

Top comments (0)