Smaller Container Images with zstd!
Do you know how the layers of your container images are compressed?
Most likely with gzip. Not because somebody decided it. It is the default, and defaults rarely get changed.
I ran into this while working on the node image for Tabernetes, my experiment where a real Kubernetes control plane runs as WebAssembly in a browser tab. To join a real node, you start a container with a kubelet and containerd. Nobody wants to wait for a large download before a demo starts, so I tried to make that image as small as possible.
Removing unused tools and stripping binaries helped, as expected. What surprised me was one line in the build workflow: switching the layer compression from gzip to zstd. The image became about 20 % smaller. Without changing a single file inside it.
So this is not one of the classic image optimization guides, where you put all RUN lines into one layer or pick a smaller base image.
Those change what is inside the image.
Here, the content stays the same. Instead, we tell our build engine to use a more efficient compression.
zstd has been part of the OCI image specification for years. So why is almost every image I pull still gzip?
I could not find a good reason. For images that you build and run yourself, I think zstd should be the default.
How image layers are compressed
A container image is not one file. It is a manifest, a config, and a list of layers. Each layer is a tar archive with the files that one build step added or changed. The registry does not store this tar archive directly. It stores a compressed version of it, a blob.
When a node pulls an image, the container runtime:
- Fetches the manifest and reads the list of layer blobs.
- Downloads each blob that is not already on the node.
- Decompresses the blob.
- Unpacks the tar archive into the snapshotter, for example overlayfs.
The manifest tells the runtime how each blob is compressed, through its media type. A gzip layer looks like this:
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"digest": "sha256:...",
"size": 160934127
}For zstd, the media type ends with tar+zstd instead.
zstd is not a new image format. It is the same tar archive, only compressed differently.
But the compression is not hidden inside the blob. It is part of the image format, and every client must understand the media type, or it cannot pull the image.
Keep this in mind, it is the only real risk of the switch. I come back to it below.
gzip is the default because Docker used it from the beginning, long before the OCI specification existed. It was a good choice at that time. zstd came later, in 2016. It fits this workload well: decompression is fast at every level, and a wide range of levels lets you trade build time for size. gzip has neither.
Measuring the difference
I did not want to trust benchmarks from someone else, so I measured it myself, with BuildKit, because that is what builds most images.
I took three popular images from Docker Hub (nginx:1.29, postgres:17, and python:3.13-slim, all linux/amd64)
and built each root filesystem again as a single layer:
FROM postgres:17 AS src
FROM scratch
COPY --from=src / /That way, BuildKit has to compress every byte itself and cannot reuse the gzip blobs from Docker Hub.
Before every run, I pruned the build cache with docker buildx prune -af, so every setting starts from zero.
The builder is a docker-container builder with BuildKit v0.34 and buildx 0.38 on Docker Desktop (Apple M1).
| BuildKit output | nginx:1.29 | postgres:17 | python:3.13-slim | Total | vs. gzip | Compression | Decompression |
|---|---|---|---|---|---|---|---|
compression=gzip | 63.5 MB | 160.9 MB | 43.2 MB | 267.6 MB | 100 % | 19.3 s | 3.70 s |
| gzip, level 9 | 63.4 MB | 160.3 MB | 43.1 MB | 266.8 MB | 99.7 % | 67.4 s | 3.74 s |
compression=zstd | 55.8 MB | 146.5 MB | 38.0 MB | 240.3 MB | 90 % | 4.4 s | 0.63 s |
| zstd, level 9 | 53.3 MB | 138.3 MB | 36.1 MB | 227.8 MB | 85 % | 5.3 s | 0.62 s |
| zstd, level 19 | 49.8 MB | 128.1 MB | 33.9 MB | 211.8 MB | 79 % | 34.1 s | 0.65 s |
Compression is the time of the “exporting layers” step for all three images, as BuildKit reports it.
Decompression is the total single-threaded time for all three layers, best of five runs,
with the same Go decoders that containerd uses: compress/gzip from the standard library and klauspost/compress for zstd.
Three things stand out for me.
gzip level 9 is not worth it. It needs three to four times the CPU of the default level and saves less than 1 %. Tuning gzip does not help. Switching does.
The default zstd setting compresses about four times faster than the default gzip setting, and the result is still 10 % smaller. Level 19 needs around 15 seconds more than gzip for all three images and makes them 21 % smaller. For me, that is an easy trade in a CI pipeline.
The bigger difference is decompression. zstd decompresses about six times faster than gzip, and the level does not matter for that. The higher level is paid once in CI, not on every node that pulls the image.
You may notice that level 19 is not as small as you would expect from the zstd command-line tool.
BuildKit does not use libzstd. It uses the pure Go encoder from klauspost/compress, which maps the zstd levels to four internal speed modes.
All levels above 10 end up in its “best” mode, which is roughly comparable to zstd level 11.
I also loaded the postgres:17 image into Docker with the containerd image store (docker load, three runs each, on a linux/amd64 VM).
The gzip image took 6.5 seconds on average, the zstd images 5.3 to 5.4 seconds.
That is about 18 % faster, although unpacking there is mostly limited by writing 450 MB to disk, not by decompression.
A real pull from a registry also downloads up to 20 % less data. I did not measure that part.
Faster deployments
Every Pod that lands on a node without its image waits for the pull: download the layers, then decompress and unpack them. A rollout, a new node from the autoscaler, a Pod that moves after a node failure: all of them start with a pull.
zstd makes both parts faster.
A smaller image means less data to download, and faster decompression means the layers are ready sooner.
The docker load above was already 18 % faster, without any network in between. On a real pull, the smaller download comes on top.
For me, this is the part that matters most. Not the saved registry storage, but Pods that start sooner.
Building zstd images with BuildKit
The image exporter of BuildKit has four options that matter here:
| Option | Values | What it does |
|---|---|---|
compression | uncompressed, gzip, estargz, zstd | Compression for new layers. The default is gzip. |
compression-level | 0..22 for zstd, 0..9 for gzip | The compression level. The default for zstd is 3. |
force-compression | true, false | Also re-compress existing layers, such as the layers of the base image. |
oci-mediatypes | true, false | Use OCI media types. The Docker media types have no zstd variant. |
A complete build command looks like this:
docker buildx build \
--platform linux/amd64,linux/arm64 \
--output type=image,name=ghcr.io/example/app:1.0.0,push=true,compression=zstd,compression-level=9,force-compression=true,oci-mediatypes=true \
.In my tests, BuildKit switched to OCI media types automatically as soon as zstd was selected.
I still set oci-mediatypes=true, because it documents the intent for the next person who reads the build script.
You need either a docker-container builder (docker buildx create --use) or Docker with the containerd image store.
The containerd image store is the default for new installations since Docker 29. The classic Docker image store cannot export zstd layers.
With docker/build-push-action in GitHub Actions, replace push: true with an outputs line:
- uses: docker/build-push-action@v7
with:
context: .
platforms: linux/amd64,linux/arm64
tags: ghcr.io/example/app:1.0.0
outputs: type=image,push=true,compression=zstd,compression-level=19,force-compression=true,oci-mediatypes=trueThis is the line I added for the Tabernetes node image.
The two BuildKit cache traps I ran into
While measuring, I lost some time because the results did not change when I expected them to. Both problems come from the BuildKit cache, and neither of them prints a warning.
Trap 1: without force-compression, existing layers stay gzip
BuildKit applies the compression option only to layers it creates in this build.
Layers that already exist in the cache, including every layer of the image in your FROM line, are reused as they are.
I built the same image twice, first with gzip and then with compression=zstd.
The second export still contained a tar+gzip layer with exactly the same digest as before. Only force-compression=true converted it.
The tricky part is the base image. In a typical application image, it holds most of the bytes.
Without force-compression=true, you can end up with an image that is still mostly gzip, while the build script says zstd.
Trap 2: the compression level is not part of the cache key
After building a zstd layer at level 3, I built again with compression-level=19,force-compression=true.
BuildKit returned the level 3 blob from the cache, with the same size and the same digest.
BuildKit converts a layer when the compression type is wrong. A blob with the right type but a different level is fine for it. In CI with a fresh builder, this does not matter. On a long-living builder or with a shared cache, prune the cache once after you change the level.
Because of these two traps, I do not trust the flags alone. I check the media types in the registry:
docker buildx imagetools inspect --raw ghcr.io/example/app:1.0.0 | jq '.manifests[0].digest'
docker buildx imagetools inspect --raw ghcr.io/example/app:1.0.0@sha256:... | jq '.layers[].mediaType'Every layer should be application/vnd.oci.image.layer.v1.tar+zstd.
Podman and Buildah
Podman and Buildah handle compression differently. Locally, they store layers uncompressed.
The compression happens when you push the image, so you set it on push, not on build:
podman build -t registry.example.com/app:1.0.0 .
podman push --compression-format zstd --compression-level 19 registry.example.com/app:1.0.0buildah push accepts the same flags. For zstd, the level range is 1 to 20.
--force-compression exists here too, but it is enabled automatically when you pass --compression-format on the command line.
So the first BuildKit trap does not hit you in the same way.
If you do not want to remember the flags, set a default in containers.conf:
[engine]
compression_format = "zstd"
compression_level = 19Podman also supports zstd:chunked. It is still zstd, but with additional metadata that allows CRI-O and Podman to pull only the files that are not already on the node.
Other runtimes treat it as normal zstd.
Converting existing images
If you want to try zstd before you change any build, skopeo and regclient can re-compress an image while they copy it to another registry:
docker run --rm ghcr.io/podman-container-tools-community/skopeo:v1 copy --all \
--dest-compress-format zstd --dest-compress-level 19 --dest-force-compress-format \
docker://docker.io/library/postgres:17 \
docker://registry.example.com/mirror/postgres:17-zstdWithout --dest-force-compress-format, skopeo may reuse the existing gzip blobs, like BuildKit without force-compression.
This fits well if you already mirror public images into an internal registry, which I recommend anyway.
Who can pull zstd images
Here the media type comes back.
A client either understands tar+zstd, or the pull fails with an error about an unsupported media type. There is no automatic fallback to gzip.
| Component | zstd support |
|---|---|
| OCI image spec | application/vnd.oci.image.layer.v1.tar+zstd, part of the spec for years |
| containerd | since 1.5 (2021) |
| CRI-O, Podman, Buildah | via containers/image for years, including zstd:chunked |
| Docker Engine | since 23.0 (2023) |
| BuildKit / Docker Buildx | compression=zstd in the image exporter |
Kubernetes itself does not care about the compression. The kubelet never touches layers. The container runtime does. Current Kubernetes distributions ship containerd 1.7 or 2.x, or a current CRI-O, and all of them pull zstd images.
So the clusters are not the problem. The tools around them are:
- CI runners and developer machines that still run a Docker Engine older than 23.0.
- Security scanners, SBOM tools, and admission controllers that download and unpack layers themselves. Current versions of the common tools support zstd, but check the version you actually run, not the latest release.
- Lazy pulling: eStargz and SOCI are built around gzip. If you depend on them, zstd is not a drop-in replacement. More on that below.
- Registries store blobs and usually do not look at the media type. Docker Hub, GHCR, ECR, ACR, Google Artifact Registry, Quay, and Harbor all serve zstd images. Very old self-hosted registries and proxies with a media type allowlist can be an exception.
When I would not use zstd
If you publish an image that anybody can pull, somebody will pull it with Docker 20.10 on a forgotten build server, and the bug report will end up with you. For public images, I would keep gzip for the default tag and publish zstd as an additional tag, at least until the old clients are really gone.
It is technically possible to put gzip and zstd variants of the same platform into one image index, for example with podman manifest push --add-compression zstd.
But older clients do not select a manifest by compression, so it does not help the clients that need it.
Very small images are the other case. A 10 MB distroless image with a single Go binary will not pull noticeably faster. Switching does not hurt, but do not expect a visible difference.
For internal images that run on your own clusters, I see no reason to wait.
What about eStargz?
eStargz is a gzip layer with an index of its files, so a node with the stargz snapshotter can start the container before the whole image is downloaded and fetch the other files on demand. This is called lazy pulling.
The zstd alternative is zstd:chunked, which I mentioned in the Podman section. CRI-O and Podman can already pull it lazily.
BuildKit cannot build it yet. compression=estargz is always gzip, so with BuildKit you have to choose between lazy pulling and zstd.
I would not wait for it. Lazy pulling needs a special snapshotter on every node, and I think it is still a niche. If you do not use it today, zstd costs you nothing here.
My current approach
- Build every internal image with
compression=zstd,force-compression=true,oci-mediatypes=true. - Use level 9 as the default. It is barely slower to build than level 3 and around 5 % smaller.
- Use level 19 for large images that are pulled very often, such as node images, CI runner images, or ML images. For me, the extra CI minutes are worth it.
- Re-compress public images with zstd when I mirror them into an internal registry.
- Check the tools before the switch, especially old Docker Engines in CI and the scanners in the pipeline.
- Verify the media types in the registry instead of trusting the build flags.
For the Tabernetes node image, it was one line. Are you already using zstd for your images, or did something in your toolchain stop you?