# EXPOSE

```
EXPOSE <port> [<port>/<protocol>...]
```

**EXPOSE指令**通知**Docker**容器在运行时, 在指定的网络端口上进行监听。可以指定端口是监听在**TCP**上还是**UDP**上，如果未指定协议，则默认值为**TCP**。

**EXPOSE指令**实际上不会真的发布端口。它充当构建镜像的人员和运行容器的人员之间的一种文档功能的角色，告诉用户打算发布的端口的信息。要在运行容器时真正发布端口，请在**docker run**上使&#x7528;**-p**发布并映射一个或多个端口，或使&#x7528;**-P**发布所有需要暴露的端口并将其随机映射到主机的端口。

默认情况下，**EXPOSE**假定使用**TCP**。我们也可以指定**UDP**：

```
EXPOSE 80/udp
```

要同时在**TCP**和**UDP**上暴露端口，请包括以下两行：

```
EXPOSE 80/tcp
EXPOSE 80/udp
```

在这种情况下，如果&#x5C06;**-P**与**docker run**配合使用，则该端口仅对**TCP**公开一次，对于**UDP**公开一次。请记住，**-P**在主机上使用临时的主机端口，因此该端口对于**TCP**和**UDP**将是不同的。

无论**EXPOSE**设置如何，都可以在运行时使&#x7528;**-p**覆盖它们。例如:

```bash
docker run -p 80:80/tcp -p 80:80/udp ...
```

要在主机系统上设置端口重定向，请参阅[**使用-P**](https://docs.docker.com/engine/reference/run/#expose-incoming-ports)。**docker network**命令支持创建网络以便在容器之间进行通信，而无需暴露或发布特定端口，因为连接到网络的每一个容器都可以在端口之上相互通信。有关详细信息，[**请参阅此功能的概述**](https://docs.docker.com/network/)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://camelgemonion.gitbook.io/docker/dockerfile-zhi-ling/expose.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
