I am reading the official Docker docs, and it is unclear to me how many layers will be created for the below dockerfile.
# syntax=docker/dockerfile:1
FROM ubuntu:18.04
LABEL org.opencontainers.image.authors="[email protected]"
COPY . /app
RUN make /app
RUN rm -r $HOME/.cache
CMD python /app/app.py
As per my understanding, four layers will be created – first from FROM command, then COPY command, and then two layers from each RUN command.
I have another question: how many layers does the FROM command create? Does it create only one layer, or can it create more than one layer?
4
Answers
The key concept is:
so in your example, the
COPY
command and theRUN
commands will create one layer each (so 3 in total).These layers are added to whatever layers the base image has.
given:
then:
so you end up with whatever layers you had in the base image, plus the new layers.
FROM
in itself does not create a layer.It will create 4 layers. According to the docker documentation:-
Some are saying that, FROM doesn’t create a layer. So, for them, I am sharing documentation part. Please Look:-
Below is the quote from official documentation. Going by the official documentation it is clear that there will be four layers. Also, the answer from other member proves that there will be four layers.
The layers are inherited from the base image, whatever that image contains will be the starting point for your new image.
Those saying that your example will result in 4 layers are correct because ubuntu only has one layer to start with, not because the FROM line is repackaged into a single layer.
Lets take a better example, nginx contains several layers:
Next, lets compare the resulting layers in each of those images:
The first 6 layers are identical, then the RUN in the Dockerfile added a single new layer. These layers are shared since they are part of a content addressable store, it’s a hash of the tar+gz of the filesystem changes. They don’t get repacked into a single layer which would result in more space to store and bandwidth to distribute.
You can also see the steps in the history of the image, not every step creates a layer, and docker shows these in reverse chronological order: