Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of commands in Dockerfile

Tags:

docker

I noticed that each line in the Dockerfile creates a separate image. Is there any limit on the number of images that are created?

Should we try to do a oneliner of RUN cmd1 && cmd2 && cmd3 instead?

How would this differ if we use a service like Quay?

Thanks!

like image 553
Reza S Avatar asked Jan 30 '14 21:01

Reza S


People also ask

Can a Dockerfile have multiple commands?

Run Multiple Commands With a Shell Script We can use the Dockerfile COPY directive to copy the shell script file to our container's filesystem. We use the COPY directive to copy the shell script to the container's filesystem. Moreover, we execute the script with the CMD directive.

Can we have 2 ENTRYPOINT in Dockerfile?

According to the documentation however, there must be only one ENTRYPOINT in a Dockerfile.

Does a Dockerfile need a command?

Both ENTRYPOINT and CMD are essential for building and running Dockerfiles—it simply depends on your use case. As a general rule of thumb: Opt for ENTRYPOINT instructions when building an executable Docker image using commands that always need to be executed.


2 Answers

As Alister said, there is an upper limit on the number of layers in a Docker image if you are using the AUFS file system. At Docker version 0.7.2 the limit was raised to 127 layers (changelog).

Since this a limitation of the underlying union file system (in the case of AUFS), using Quay or other private registries won't change the outcome. But you could use a different file system.

The current alternative filesystem is to use devicemapper (see CLI docs). These other filesystems may have different limitations on the number of layers -- I don't think devicemapper has an upper limit.

You're right, by RUNning multiple commands in a single RUN statement, you can reduce the number of layers.

Alternatively, if you really need a lot of layers to build your image, you could build an image until it reaches the maximum and then use docker export to create an un-layered copy of the image's file system. Then docker import to turn it back into an image again, this time with just one layer, and continue building. You lose the history that way though.

like image 67
Andy Avatar answered Oct 29 '22 12:10

Andy


There is a limit, of 42 - apparently a hard limit imposed by AUFS.

It can help to be somewhat avoided by putting what would be done in individual RUN commands into a script, and then running that script. You would then end up with a single, larger image layer, rather than a number of smaller files to merge. Smaller images (with multiple RUN lines) make initial testing easier (since a new addition on the end of the RUNlist can re-use the previous image), so it's typical to wait until your Dockerfile has stabilised before merging the lines.

You can also reduce the potential number of images when ADDing a number of files, by adding a directory-full, rather than a number of individual files.

like image 20
Alister Bulman Avatar answered Oct 29 '22 13:10

Alister Bulman