Differrence between docker publish and docker expose

Docker is an advance and most popular containerization platform majorly used by developers for development of applications. In this article, we will explore the difference between docker expose and publish. What can be used to connect container internally and from out side world.

Here we have three options which can be used to differentiate docker publish and docker expose

  1. Neither specify expose nor publish (-p)
  2. Only specify expose
  3. Specify expose and publish (-p)

docker publish and docker expose
  1. If you specify neither expose nor publish (-p): The service in the container will only be accessible from inside the container itself.
  2. If you expose a port, the service in the container is not accessible from outside docker but from inside other docker containers. So this is good or it can be used for inter-container communication.
  3. If you use both expose and publish (-p), the service in the container is accessible from anywhere , even outside docker.

There is a catch if you only use publish (-p):

If you use publish (-p) and do not use expose, docker does an implicit expose. This is because, if a port is opened to the world, it is automaticaly also opened to other docker containers. Thus publish (-p) includes expose by default. Learn more about docker and containers.

Conclusion:

To summarize the difference between docker publish and docker expose is that we can use docker expose for inter-container communication or for pod networking whereas publish (-p) can be used to expose or access the container publicly from anywhere. To install and setup Docker, refer this link.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments