What is the purpose of EXPOSE command in Dockerfile?

Technology CommunityCategory: DockerWhat is the purpose of EXPOSE command in Dockerfile?
VietMX Staff asked 3 years ago

When writing your Dockerfiles, the instruction EXPOSE tells Docker the running container listens on specific network ports. This acts as a kind of port mapping documentation that can then be used when publishing the ports.

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

or

docker run --expose=1234 my_app

But EXPOSE will not allow communication via the defined ports to containers outside of the same network or to the host machine. To allow this to happen you need to publish the ports.