dockerdockerfiledocker-layer

Does Docker EXPOSE make a new layer?


I have been playing around with creating docker files and reading the documentation and I was wondering this question: Does adding an EXPOSE command to my Dockerfile add a layer? (and if it does why would I care/ does it matter where it is placed inside the file?)

It is not specifly said in the documentation.

I understand that RUN, COPY, and ADD create layers because they modify the file system. However, does the use of EXPOSE generate a layer since it simply adds metadata to the container?


Solution

  • I realised i could test this myself. and I've found that adding EXPOSE does not add a new file system layer, but it does add a layer none the less, also it does matter which order you make your docker files for your cache layers.

    basically: every command creates a new layer, every command that changes the file system creates a filesystem layer.

    FROM ...
    EXPOSE 80
    COPY smthing .
    

    is different from:

    FROM ...
    COPY smthing .
    EXPOSE 80
    

    When executed multiple times (say in a development environment).

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

    In the second example. if the smthing file changes, the EXPOSE command will also be rebuild. (since everything after the copy command is invalidated and re executed on docker build).

    Would i change the EXPOSE port the first case would have to re-execute the copy command, where the second example wouldn't.

    But both would lead to the exact same end result file-system layer wise.

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