1

I have a docker image that has 13 set of modifications since its creation. For each set of modification, a new image has been created. Thus there are currently 13 image versions with the 13th one being the newest. Also, each image is dependent on the previous version. In short, the 13th version has a dependency that has recursive dependencies. How to create an complete independent image from 13th version ?

More abstractly, How to generate an independent docker image from an image that has recursive dependencies ?

I checked the answere here but it doesn't answer my question

Noor
  • 19,638
  • 38
  • 136
  • 254
  • Can you add your `Dockerfile` to the question, to illustrate what you mean by "dependent" here? – David Maze Jul 14 '19 at 00:04
  • There was no docker file, a ubuntu image was pulled and modifications were made to it. For every set of modifications, a new version was created. E.g from raw ubuntu to v1, from v1 to v2, from v2 to v3,...v13. Thus, every version is dependent on the previous version. By dependent here, i mean every version `n` is a child image of version `n-1` until the raw image (v0). – Noor Jul 14 '19 at 06:55
  • 2
    The usual way to create Docker images is by writing a [`Dockerfile`](https://docs.docker.com/engine/reference/builder/) and using the [`docker build`](https://docs.docker.com/engine/reference/commandline/build/) command, and then you can check the `Dockerfile` into source control. When you do that you have control over the layer structure, your colleagues can easily reproduce your image, and you're not at risk of a stray `docker rmi` losing work. – David Maze Jul 14 '19 at 10:15
  • Since we didn't do that, is there now a way to reproduce that independent image from the the existing dependent images ? – Noor Jul 14 '19 at 10:18
  • In principle, [How to generate a Dockerfile from an image?](https://stackoverflow.com/questions/19104847/how-to-generate-a-dockerfile-from-an-image/30793515), but you don't have the metadata to do it; your entire `docker history` is "the filesystem changed" 13 times. – David Maze Jul 14 '19 at 10:23

1 Answers1

2

There are a couple of things to try:

Squash newly built layers into a single new layer

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47
  • for `multi-stage` builds, the moment i use `FROM` in the dockerfile, doesn't the resulting image become an image dependent on the images referenced in the dockerfile using `from` – Noor Jul 14 '19 at 06:57
  • So you're doing a `commit` in your container and then another? – KeepCalmAndCarryOn Jul 14 '19 at 10:37
  • the way the final image was build is as follows: an image was pulled, and was loaded in a container, changes was made to the image and these changes were commited to create a new image. This new image was loaded into another container and new changes were made and commited. Like this, subsequent versions each having a set modifications were created to obtain the 13th versions – Noor Jul 14 '19 at 10:54
  • 1
    As is mentioned in a comment above. To leverage the power of docker, you need to use a Dockerfile and rebuild (ideally with CI). Never maintain state in your image and use volume mounts for persistence. When you need a change, fix the Dockerfile and rebuild. Otherwise its just a VM – KeepCalmAndCarryOn Jul 14 '19 at 10:59
  • agree with what your advise, but now the harm is already made, is there any solution ? – Noor Jul 14 '19 at 11:01
  • There is always a solution. Your main problem is losing your data with a random image failure, or stray `docker rmi ...` Once that is secure, you could restart your container with a volume mount then `exec` into it to move all your data out to the volume mount, which is easier to have a backup process. After that, you can have a new image to use the volume. Probably outside the scope of SO now... – KeepCalmAndCarryOn Jul 14 '19 at 11:19