Docker

What is the Difference Between “expose” and “publish” in Docker?

Docker is a well-known tool for building and organizing containers. Containers are separate environments that can run applications and services. One of the features of Docker is that it permits users to share their containers with others, either by exposing or publishing them. The “expose” and “publish” are two different ways of making a container port accessible to other containers or external clients but they have some differences.

This article will illustrate the following:

What is “expose” in Docker?

In Docker, the “expose” is a way to specify which network ports a container listens to and make available to other containers on the same Docker network. It does not open the port up to external access or from the host machine. It only informs Docker about the internal port that the container uses and is used for inter-container communication, such as linking containers together.

It can be specified in the Docker file using the EXPOSE instruction:

EXPOSE 8080

Alternatively, users can also utilize the “–expose” option when running a container to expose the port:

$ docker run --expose 8080 nginx

What is “publish” in Docker?

The “publish” is a way of mapping a container’s port to the host or another container so that the container port can be accessed from outside the container. This allows users to access the container from the host system or the outside world using the host port. It is utilized for exposing services to external clients, such as web browsers or Curl.

To specify which ports are published by the container, the “-p” or “–publish” option can be used in the “docker run” command:

docker run -d -p 8080:80 myimg

Alternatively, it can also be specified in the Docker Compose file using the “port” option:

version: '3.8'
services:
  web:
    image: nginx
    ports:           //specifying port
      - "80:80"

What is the Difference Between “expose” and “publish” in Docker?

The main difference between “expose” and “publish” is that the “expose” allows other containers on the same network to access the specified port while the “publ0ish” makes the container available to others for deployment. Moreover, the “expose” is used for internal container-to-container networking, while “publish” is utilized for communicating with systems beyond the container world.

Conclusion

The “expose” and “publish” are two different ways of making a container port accessible to other containers or external clients. The “expose” only makes the port accessible to other containers on the same network, while “publish” makes the port accessible to the host machine and any external clients. They can be used in the Docker file or “docker run” commands to specify which ports are used by the container.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.