Search code examples
podmanbuildah

podman -- how can I remove the top layer of a container image?


I have an old image, may its name be B:latest. It has been built on top of another image, may its name be A:latest.

I have still the script that has been used for building. It is something like that:

conta=$(buildah from A:latest)
buildah run $conta ...
buildah copy $conta ...
...
buildah commit --rm $conta B:latest

A colleague has built the image years ago. I know, using always the tag latest was not a good choice, but this is another story.

My problem is now, I have to build a modified version of image B. I want to use a modified version of the old build script and build it on top of the old image A.

I have still a copy of image B. But the image A is no longer there. I can see only image B when I execute the command:

podman images --all

I can display the layers of the image by

podman image tree B:latest

So my hope was that I can add a new tag to the layer below the top layer and I could reuse this layer as a copy of image A, but it does not work. May the layer below the top layer have the id 11deadbeef00. I cannot see an image with this id with the command podman images --all, but it must still be contained somehow in image B. This was what I have tried:

podman image tag 11deadbeef00 newname

The error message was "Error: 11deadbeef00: image not known".

My question: Can I somehow access the layers below the top layer of a container image and make an image out of it that I can use independently?


Solution

  • With the caveat that this seems like a pretty clunky solution, you might be able to export the image to a directory:

    podman image save B:latest |
      tar --one-top-level=imageB -xf-
    

    Edit the image metadata to remove the top layer:

    $ vim manifests.json
    ...
    $ vim $(jq -r jq .[0].Config manifest.json)
    ...
    

    And then load the modified image back into podman:

    tar -C imageB -cf- . | podman image load