0

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?

Donat
  • 4,157
  • 3
  • 11
  • 26
  • 1
    Maybe someone will drop by with a better suggestion, but I imagine you could `mkdir myimage && podman image save | tar -C myimage -xf-`, then manipulate the image configuration, tar it back up, and read it back in with `podman image load`. – larsks Mar 28 '23 at 15:34
  • @larsk Thank you for your suggestion. I managed to restore the old image. I would accept your suggestion if you would put it in an answer. – Donat Mar 29 '23 at 10:43

2 Answers2

2

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
larsks
  • 277,717
  • 41
  • 399
  • 399
0

In this particular case (commit over a previous image), you can display the history and give a new tag to the old version:

$ podman image history test
ID            CREATED        CREATED BY                                     SIZE        COMMENT
1cf9ef46244e  7 minutes ago  /bin/sh                                        3.84 MB     FROM localhost/test:latest
12598d7c57b5  2 days ago     /bin/sh                                        51.2 MB     FROM localhost/test:latest
09f343ca7882  6 days ago     /bin/sh                                        13.9 MB     FROM localhost/test:latest
409ad3c8e0f8  6 days ago     /bin/sh                                        72.7 MB     FROM localhost/test:latest
a7cddf736673  6 weeks ago    /bin/sh                                        295 MB      FROM localhost/test:latest
<missing>     6 weeks ago    /bin/sh                                        99 MB       FROM localhost/test:latest
<missing>     6 weeks ago    /bin/sh                                        170 MB      FROM localhost/test:latest
$ podman image tag 12598d7c57b5 test:latest
$ podman image history test
ID            CREATED        CREATED BY                                     SIZE        COMMENT
12598d7c57b5  2 days ago     /bin/sh                                        51.2 MB     FROM localhost/test:latest
09f343ca7882  6 days ago     /bin/sh                                        13.9 MB     FROM localhost/test:latest
409ad3c8e0f8  6 days ago     /bin/sh                                        72.7 MB     FROM localhost/test:latest
...

If the image is a fresh build (podman build or commit from a different source that overwrote the image), podman image list will still list the image without tag so this is also recoverable, but that is a separate issue.

Hope this helps whoever comes next, it's slightly easier than editing the metadata :)

Asmadeus
  • 300
  • 2
  • 11
  • Sorry, this was not possible. I could not see any IDs except the to level ID when I executed the command ```podman image history```. But I did get the ID of the layer below the top level layer when I used the command ```podman image tree```. But even this ID did not help. The answer of @larsk was the solution for my problem. – Donat Mar 31 '23 at 14:46
  • IDs in `podman image tree` are layer ids, not image ids, it's a bit confusing but they're not interchangeable so this is expected. I'm curious why you'd have no ID listed in `podman image history` just after a commit (old IDs would be pruned by prune or something like that), but in that case yes modifying the image directly is probably the most reliable way. – Asmadeus Apr 03 '23 at 00:59