Does Docker EXPOSE create a new layer?

I played with the creation of docker files and read the documentation, and I was asked this question: does the EXPOSE command add a layer to my docker file? (and if so, why would I be interested / matters where it is placed inside the file?)

This is not indicated in the documentation .

I understand that RUN, COPY, and ADD create layers because they change the file system. but exposing just adds metadata to the container, does it change the layer?

+6
source share
3 answers

Yes, every command in the Docker file generates a new layer for the resulting image.

However, layers created using EXPOSE are empty layers. That is, their size is 0 bytes.

Despite the fact that they do not affect your memory size, they rely on the use of the layer cache when creating or pulling / clicking images from the registry.

A good way to understand image layers is to use the docker history . For example, given the following Docker file:

 FROM scratch EXPOSE 4000 EXPOSE 3000 

do

docker build -t test/image .

If you then docker history test/image , you will see:

 IMAGE CREATED CREATED BY SIZE COMMENT ab9f435de7bc 4 seconds ago /bin/sh -c #(nop) EXPOSE 4000/tcp 0 B 15e09691c313 5 seconds ago /bin/sh -c #(nop) EXPOSE 3000/tcp 0 B 

If you reorder the EXPOSE statements and build again, you will see that the layer cache is ignored.

+6
source

All instructions create new layers, but commands that do not change the file system will create an empty layer.

It is worth seeing how the Docker file system add-in works, which you can read about here or here for AUFS .

In fact, the new layers in the file system consist of those files that have been changed from the layer below them, like the diff stack. Thus, if there is no change, there is no layer. Basically...

Each command in the Docker file will create an image layer, but for AUFS (in the case of EXPOSE ) this layer will be empty (no difference between it and below it).

+2
source

I realized that I could check it myself. and I found that adding EXPOSE does not add a new level to the file system, but it does add a level nonetheless, it does matter what order you make with your docker files for your cache levels.

basically: each command creates a new level, each command that changes the file system creates a file system level.

 FROM ... EXPOSE 80 COPY smthing . 

differs from:

 FROM ... COPY smthing . EXPOSE 80 

It is executed several times (say, in a development environment).

in the first example, the EXPOSE command is cached and not executed, even if the smthing file changes. If any file changes, docker build will only re-execute this command (rest is taken from the cache).

In the second example. if the smthing file changes, the EXPOSE command will also be rebuilt. (since everything after the copy command is invalid and re-runs on docker build ).

I would change the EXPOSE port, the first case would be to re-execute the copy command, where the second example would not be.

But both of them will lead to the same file system file of the final result.

 docker inspect imageName #shows the file system layer docker history imageName #shows all the layers 
+1
source

Source: https://habr.com/ru/post/1014063/


All Articles