dockerTools.buildLayeredImage: Possible to compress layers?

How difficult would it be to extend buildLayeredImage to compress the layers?

I realize that the final output is gzipped and that it isn’t necessary to have layers gzipped to load into a container repository. However, I have a use-case where I’m loading the container.tar into Bazel via container_load and gzips layers if they are not already gzipped. See:rules_docker/ at 3b0187852774ef0e424c92ec7b78a2a199a0503b · bazelbuild/rules_docker · GitHub This is slow for large images.

You can probably easily call out to another tool to squash the layers.

That tool can be added on the dockerBuildLayers command ?

Do you know of any such tools? For clarity, I’m not looking to “flatten” an image into a single or fewer layers, mostly to make the intermediate tar files compressed.

I don’t fully understand what you mean in terms of the intermediate layers and I know youve said youre not looking to flatten but here are some of the things I found out.

I couldnt get docker-slim to work and since nix utilizes the store which is readonly Im not sure it could do much

you can try play with maxLayers, it needs to be more than 1 but you can end up squishing things quite flat, the size will end up the same though in my experience

Also FYI you can use dive to inspect an image’s layers which is very nice

Maybe you could read through this mergeImages shell script and find something useful?

I’m not looking to squash or remove or slim down an image, I’m looking to store the individual layers in application/vnd.oci.image.layer.v1.tar+gzip.

A container image is a single tarball made up of a manifest, a configuration, and then 1 or more layers. I’m not concerned about the outer layer being a compressed or uncompressed tar, I’m interested in the intermediate layers being compressed tarballs.