在使用docker时,有时需要在docker run时使用-p来公开一些容器中的端口以提供服务。然而,在编写Dockerfile时,也会用到EXPOSE命令来公开某些端口。那么,EXPOSE和-p有啥关系吗?他们的作用是什么呢?

其实答案很简单,引用Docker官方文档中的一段话,这段话完美诠释了EXPOSE和-p之间的关系:

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.

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐