Docker EXPOSE 会创建一个新层吗? [英] Does Docker EXPOSE make a new layer?

查看:19
本文介绍了Docker EXPOSE 会创建一个新层吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建 docker 文件和阅读文档,我想知道这个问题:是否将 EXPOSE 命令添加到我的 Dockerfile 会添加一个层?(如果确实如此,我为什么要关心/它在文件中的放置位置是否重要?)

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?)

文档中没有具体说明.

我理解 RUNCOPYADD 创建层是因为它们会更改文件系统,但暴露只是将元数据添加到容器中,确实如此它的变化生成了一个层?

I understand RUN, COPY and ADD create layers because they change the file system, but expose simply adds metadata to the container, does it's change generates a layer?

推荐答案

我意识到我可以自己测试这个.而且我发现添加 EXPOSE 不会添加新的文件系统层,但它确实添加了一个层,而且为缓存层制作 docker 文件的顺序也很重要.

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 .

不同于:

FROM ...
COPY smthing .
EXPOSE 80

多次执行时(比如在开发环境中).

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

在第一个示例中,EXPOSE 命令被缓存,即使 smthing 文件更改也不执行.如果某些文件发生变化,docker build 只会重新执行此命令(其余部分取自缓存).

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).

在第二个例子中.如果 smthing 文件改变,EXPOSE 命令也将被重建.(因为复制命令之后的所有内容都无效并在 docker build 上重新执行).

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).

我是否会更改 EXPOSE 端口,第一种情况必须重新执行复制命令,而第二种情况不会.

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

这篇关于Docker EXPOSE 会创建一个新层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆