“expose"和“expose"有什么区别?和“发布"在 Docker 中? [英] What is the difference between "expose" and "publish" in Docker?

查看:47
本文介绍了“expose"和“expose"有什么区别?和“发布"在 Docker 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验 Dockerfiles,我想我理解大部分逻辑.但是,在这种情况下,我没有看到公开"和发布"端口之间的区别.

I'm experimenting with Dockerfiles, and I think I understand most of the logic. However, I don't see the difference between "exposing" and "publishing" a port in this context.

我第一次看到的所有教程都在 Dockerfile 中包含了 EXPOSE 命令:

All the tutorials I have seen first include the EXPOSE command in the Dockerfile:

...
EXPOSE 8080
...

然后他们从这个 Dockerfile 构建一个镜像:

They then build an image from this Dockerfile:

$ docker build -t an_image - < Dockerfile

然后在运行图像时发布与上面相同的端口:

And then publish the same port as above when running the image:

$ docker run -d -p 8080 an_image

或使用

$ docker run -d -P an_image

在 Dockerfile 中公开端口有什么意义,如果它无论如何都会被发布?是否需要先公开端口,然后发布?实际上,我想在创建映像时指定我将在 Dockerfile 中使用的所有端口,然后不再打扰它们,只需简单地运行它们:

What is the point of exposing a port in the Dockerfile, if it will be published anyway? Would there ever be a need to expose a port first, and not publish it later? Effectively, I would like to specify all the ports that I will use in the Dockerfile when creating the image, and then not bother with them again, running them simply with:

$ docker run -d an_image

这可能吗?

推荐答案

基本上,您有三个选择:

Basically, you have three options:

  1. 既不指定EXPOSE 也不指定-p
  2. 只指定EXPOSE
  3. 指定EXPOSE-p
  1. Neither specify EXPOSE nor -p
  2. Only specify EXPOSE
  3. Specify EXPOSE and -p

1) 如果您既不指定EXPOSE 也不指定-p,则容器中的服务将只能从容器本身内部 访问.

1) If you specify neither EXPOSE nor -p, the service in the container will only be accessible from inside the container itself.

2) 如果你EXPOSE一个端口,容器中的服务不能从Docker外部访问,而是从其他Docker容器内部访问.所以这有利于容器间通信.

2) If you EXPOSE a port, the service in the container is not accessible from outside Docker, but from inside other Docker containers. So this is good for inter-container communication.

3) 如果你 EXPOSE-p 一个端口,容器中的服务可以从任何地方访问,甚至在 Docker 之外.

3) If you EXPOSE and -p a port, the service in the container is accessible from anywhere, even outside Docker.

两者分开的原因恕我直言,因为:

The reason why both are separated is IMHO because:

  • 选择主机端口取决于主机,因此不属于 Dockerfile(否则将取决于主机),
  • 通常情况下,如果容器中的服务可以从其他容器访问就足够了.

文档明确指出:

EXPOSE 指令公开链接中使用的端口.

The EXPOSE instruction exposes ports for use within links.

它还指向您如何链接容器,这基本上是容器间我谈过的沟通.

It also points you to how to link containers, which basically is the inter-container communication I talked about.

PS:如果你做了-p,但没有EXPOSE,Docker 会隐式地执行EXPOSE.这是因为如果一个端口对公众开放,它也会自动对其他 Docker 容器开放.因此-p 包括EXPOSE.这就是为什么我没有在上面将其列为第四种情况.

PS: If you do -p, but do not EXPOSE, Docker does an implicit EXPOSE. This is because if a port is open to the public, it is automatically also open to other Docker containers. Hence -p includes EXPOSE. That's why I didn't list it above as a fourth case.

这篇关于“expose"和“expose"有什么区别?和“发布"在 Docker 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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