Differences between EXPOSE And Publish
The meaning of “EXPOSE and PUBLISH” are both used to expose the ports of containers to the outside world. But the use cases are different.
- EXPOSE: This is used in Dockerfile the port on which the container will listen to incoming connections. With the help of Docker “EXPOSE” others who are using the Docker image, they will get to know which port the container will listen to it EXPOSE does not actually publish the port and does not make the port accessible from outside the container.
- PUBLISH: This command is used to publish or map a port on the host computer to a port on the container when a container is first started. By using the docker run command, the host and container ports are mapped using the -p or —publish flag. As a result, additional services or customers can connect to the container over the mapped port and access the container port from outside the container.
In conclusion, PUBLISH is a runtime command that translates a container port to a host port, allowing external access to the container service over the specified port. EXPOSE is a statement in the Dockerfile that lists the ports on which the container listens.
How to Map Ports in Docker?
In docker, all the application in the container runs on particular ports when you run a container. If you want to access the particular application with the help of a port number you need to map the port number of the container with the port number of the docker host.