mszymborski
mszymborski

Reputation: 1654

Squishing docker image without spinning a container

I am using docker to analyse changes to the file system caused by various commands. I simply add relevant RUN lines in the Dockerfile, build it, use docker save to see what changes were made. Unfortunately I use many commands, end even though most of the time I can put several of them into one layer, I usually exceed the rather low 127 parent layers limit.

To deal with this I squish images with many layers into images with just one layer, and use the resulting image as the parent for the rest of the new layers. The best way to do that is to spin up a container and then use docker export, and then to docker import that to get a nice single layer of changes. I am looking for ways to shave down the time it takes to perform that operation.

Is there a simple way to do the same thing (export the fully expanded file system of an image, not just the layers stack as in docker save) without spinning up a container?

Upvotes: 4

Views: 1617

Answers (1)

Matt
Matt

Reputation: 74831

No matter what the method, squashing a complete container is always going to be a process of reading the layered file system and writing a new flat file system, which involves io, which is relatively slow.

Creating a container is extremely light weight in comparison so I'm not sure you will gain much by getting rid of that step

time container=$(docker create debian)
time docker export $container | docker import -  debian_flat
time docker rm $container

On an ssd based system, results in:

Create

real    0m0.050s
user    0m0.015s
sys     0m0.010s

Export/Import

real    0m8.213s
user    0m2.867s
sys     0m0.768s

Remove

real    0m0.019s
user    0m0.007s
sys     0m0.008s

There are other tools that let you squash selected layers. This may save you time as you only need to read and write a portion of the data. If your requirement is the squashing the complete image then I'm not sure this will be much quicker than an export/import as you are still reading and writing the same amount of data.

There's a github issue to cover the squash feature in official docker and pull requests for support to directly squash or flatten layers in docker and builds. Each request seems to slowly die off before being accepted though.

Upvotes: 3

Related Questions