At different places I found the information that a docker image can only consist of up to 42 layers. This seems to be a limitation of the used AUFS file system.
Can anybody tell me why this limit exists or does anybody have some documentation explaining this?
The maximum amount of memory the container can use. If you set this option, the minimum allowed value is 6m (6 megabytes). That is, you must set the value to at least 6 megabytes. The amount of memory this container is allowed to swap to disk.
I'm aware of the AUFS limit of 42 layers. It makes sense to keep the number of layers small for widely used images because it helps other images built on top of them fit the restriction. However, there are another storage drivers and images for other purposes.
(Except for multi-stage builds, where usually only the layers in the final image are pushed, or when an image is squashed to a single layer). Layers are used to avoid transferring redundant information and skip build steps which have not changed (according to the Docker cache).
At different places I found the information that a docker image can only consist of up to 42 layers. This seems to be a limitation of the used AUFS file system. Can anybody tell me why this limit exists or does anybody have some documentation explaining this? Show activity on this post.
Docker Hub limits the number of Docker image downloads (“pulls”) based on the account type of the user pulling the image. See the pricing page for current options. Some images are unlimited through our Open Source and Publisher programs. Unlimited pulls by IP is also available through our Large Organization plan.
We notice here, that the intermediate containers do have a size of 0B, just what was expected. Only the RUN and COPY command from the Dockerfile contribute to the size of the Docker image. The layers of the openjdk:10-jdk image are also listed and are recognized by the missing keyword.
A user’s limit is equal to the highest entitlement of their personal account or any organization they belong to. To take advantage of this, you must log in to Docker Hub as an authenticated user. For more information, see How do I authenticate pull requests.
I'm starting to suspect that there isn't any such a hard limit.
Create the following python script, and name it "makeDockerfile.py"
with open("Dockerfile", "w") as file:
file.write("from alpine:3.8\n")
for i in range(0, 201):
file.write("run echo {i}\n".format(i=i))
then run
python makeDockerfile.py && docker build --tag manylayer . && docker run -it manylayer /bin/sh
You'll see that you are running a working container with > 200 layers.
(note, this was tested with linux containers on linux)
Note that this doesn't mean that this many layers are necessarily SUPPORTED, just that they are possible in some cases.
In fact, I've seen containers fail with far fewer than 42 layers, and removing any arbitrary layer seems to fix it. (see https://github.com/docker/for-win/issues/676#issuecomment-462991673 )
EDIT:
thaJeztah
, maintainer of Docker, has this to say about it:
The "42 layer limit" on aufs was on older versions of aufs, but should no longer be the case.
However, the 127 layer limit is still there. This is due to a restriction of Linux not accepting more than X arguments to a syscall that's used.
Although this limit can be raised in modern kernels, it's not the default, so if we'd go beyond that maximum, an Image built on one machine may not be usable on another machine.
( see https://github.com/docker/docker.github.io/issues/8230 )
From the Docker BP documentation:
Minimize the number of layers
You need to find the balance between readability (and thus long-term maintainability) of the
Dockerfile
and minimizing the number of layers it uses. Be strategic and cautious about the number of layers you use.
They also give advices on how to avoid too many layers:
That way you can delete the files you no longer need after they’ve been extracted and you won’t have to add another layer in your image
[..]
Lastly, to reduce layers and complexity, avoid switching USER back and forth frequently.
TL;DR: the benefit of minimizing the number of layers can be likened to the benefit of minimizing the number of small files but rather have fewer bigger ones. A docker pull
is also faster (try downloading 2048 files of 1kB or one file of 2MB) . and having fewer layers reduces the complexity of an image, hence the maintainability.
As for the 42
limit. Well... I guess they had to come up with a number and they pick this particular one ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With