Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the reason for the 42 layer limit in Docker?

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?

like image 369
P.J.Meisch Avatar asked Sep 08 '16 04:09

P.J.Meisch


People also ask

Is there a limit for Docker container?

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.

How many Docker layers are there?

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.

Why do Docker images have layers?

(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).

How many layers can a docker image consist of?

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.

Is there a limit on the number of Docker image downloads?

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.

What is the size of an intermediate container in dockerfile?

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.

What is a user's limit in Docker Hub?

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.


2 Answers

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 )

like image 94
cowlinator Avatar answered Oct 01 '22 13:10

cowlinator


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 ;)

like image 37
Auzias Avatar answered Oct 01 '22 14:10

Auzias