Does Docker EXPOSE make a new layer?

后端 未结 4 1703
时光说笑
时光说笑 2021-02-14 17:07

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 Doc

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-14 17:43

    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
    

提交回复
热议问题