參考教程:https://docs.docker.com/engine/reference/builder/
環境
- virtual box 6.1
- centos 7.8
- docker 19.03
EXPOSE
EXPOSE <port> [<port>/<protocol>...]
The EXPOSE
instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.
EXPOSE 指令通知 Docker 容器在運行時偵聽指定的網絡端口。您可以指定端口是偵聽 TCP 還是 UDP,如果未指定協議,則默認值為 TCP。
The EXPOSE
instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p
flag on docker run
to publish and map one or more ports, or the -P
flag to publish all exposed ports and map them to high-order ports.
EXPOSE
指令實際上並未發布端口。它充當構建鏡像的人員和運行容器的人員之間的一種文檔類型,有關打算發布哪些端口的信息。要在運行容器時實際發布端口,請在 docker run
上使用 -p
標志來發布和映射一個或多個端口,或使用 -P
標志來發布所有公開的端口並將它們映射為高階端口。
By default, EXPOSE
assumes TCP. You can also specify UDP:
默認情況下,EXPOSE
采用 TCP。您還可以指定 UDP:
EXPOSE 80/udp
To expose on both TCP and UDP, include two lines:
要同時在 TCP 和 UDP 上公開,請包括以下兩行:
EXPOSE 80/tcp
EXPOSE 80/udp
In this case, if you use -P
with docker run
, the port will be exposed once for TCP and once for UDP. Remember that -P
uses an ephemeral high-ordered host port on the host, so the port will not be the same for TCP and UDP.
在這種情況下,如果將 -P
和 docker run
一起使用,則該端口將僅對 TCP 公開一次,對於 UDP 公開一次。請記住,-P
在主機上使用臨時的高階主機端口,因此該端口對於 TCP 和 UDP 將是不同的。
Regardless of the EXPOSE
settings, you can override them at runtime by using the -p
flag. For example
無論使用哪種 EXPOSE
設置,都可以在運行時使用 -p
標志覆蓋它們。例如
docker run -p 80:80/tcp -p 80:80/udp ...
To set up port redirection on the host system, see using the -P flag. The docker network
command supports creating networks for communication among containers without the need to expose or publish specific ports, because the containers connected to the network can communicate with each other over any port. For detailed information, see the overview of this feature.
要在主機系統上設置端口重定向,請參閱使用 -P
標志。docker network
命令支持創建網絡以在容器之間進行通信,而無需暴露或發布特定端口,因為連接到網絡的容器可以通過任何端口相互通信。有關詳細信息,請參閱此功能概述。
示例
Dockerfile 文件
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
構建結果
[root@master demo1]# docker build -t jiangbo:0.0.1 .
Sending build context to Docker daemon 547MB
Step 1/5 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/5 : ARG JAR_FILE=*.jar
---> Running in fb006ab9edc3
Removing intermediate container fb006ab9edc3
---> a270e9ae613b
Step 3/5 : COPY ${JAR_FILE} app.jar
---> 9081948fad4a
Step 4/5 : EXPOSE 8080
---> Running in f1b492d6fd16
Removing intermediate container f1b492d6fd16
---> a169c40b22ef
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
---> Running in 58cbac48f911
Removing intermediate container 58cbac48f911
---> 8bb974935c05
Successfully built 8bb974935c05
Successfully tagged jiangbo:0.0.1
查看結果
[root@master demo1]# docker run -d -p:8080:8080 jiangbo:0.0.1
368f4661e8ba62d73cf1f07b84830f5496ab6a7a27fff075fbe24b15279a8933
[root@master demo1]# curl localhost:8080
Hello Docker World
[root@master demo1]# docker run -d -P jiangbo:0.0.1
95a7dd17d06e352a890161937d1bda598cd6c4444425c39493b0d6371b40d71b
[root@master demo1]# docker port 95
8080/tcp -> 0.0.0.0:32768
[root@master demo1]# curl localhost:32768
Hello Docker World
總結
介紹了 Dockerfile 中 EXPOSE 指令的使用。