docker 中定义了几种方式用于暴露容器中的端口,现将其总结如下.
dockerfile 中可以使用EXPOSE
指令
,仅说明容器需要对外暴露的端口,没有实际的暴露出去
EXPOSE [/…]
启动容器的时候通过参数指定
# 暴露特定端口到主机的特定端口
docker run -p 80:80
# 暴露容器的所有端口(exposed 端口)到主机的随机端口
docker run -P
# 添加dockerfile中expose 的端口
docker run -expose
dockerfile EXPOSE 指令
TheEXPOSE
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.
TheEXPOSE
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 on