Docker port Expose

Docker port Expose

For Interview

In the world of modern software development, containers have emerged as a revolutionary tool, streamlining the way applications are built, shipped, and deployed. Among the core aspects of containerization is the concept of Docker port Expose*. It enables a pivotal feature that enables seamless communication between containers and the outside world. In this article, we will delve into the intricacies of Docker port exposure, and uncover its significance.*

We know that containers are designed to be isolated, self-contained units, ensuring that they are self-sufficient to run in any environment. However, this isolation holds back the container from exposing them to the outside network useless specific actions are taken.

The command for Docker port Expose:

docker run -td --name techserver -p 80:80 ubuntu
# we are here bulding a container from ubuntu image and exposing them on port 80.
# -t: terminal
# -d : daemon
# -p : publish
# 80:80 -> host:container
docker ps # displays all the running containers
docker port techserver 
# this command displays: 80/TCP --> 0.0.0.0/80

What is the difference between exposing and publishing a docker?

You have three options:-

  • Neither specifies expose nor publish

    • If you specify neither expose nor -p, the service in the container will only be accessible from inside the container itself.
  • Only specify expose

    • 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 for inter-container communication.
  • Specify expose and publish

    • If you expose and -p a port, the service in the container is accessible from anywhere even outside docker.

If you do -p but do not expose docker does an implicit expose. This is because, if a port is open to the public, it is automatically also open to the other docker containers.

Hence -p includes expose.


When you have built a container you have two ways to access it. One via docker attaches and the other via docker exec. Let's see the difference between them.

Difference between docker attach and docker exec?

Docker exec created a new process in the container's environment while docker attach just connects the standard Input/Output error of the current terminal.

docker exec is specifically for running new things in already started containers be it a shell or some other processes.