How To Know Which Port Is Exposed?

The one question which still remains is, how will we know which port in the container should we map our computers port to? Usually, this detail is provided on the image repository or using the inspect command.

  • Go to the Nginx repository on the docker hub and try to locate the expose port section:

 

  • Using the docker inspect command:
$ docker inspect nginx

This will list all configurations of the Nginx image, from where we can locate the ExposedPort property.

 

Step 2:  This data can be filtered out using the –format flag:

$ docker inspect --format="{{.ContainerConfig.ExposedPorts}}" nginx

 

Step 3:  Now that we know for sure that Nginx runs on TCP port 80 inside the container’s virtual network. We can use the docker run command.

$ docker run -p 8080:80 --name webhost -d nginx

 

 Step 4: Verify if Nginx is running on port 8080:

 

 

Docker – Managing Ports

Pre-requisites: Docker 

Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. These containers may need to talk to each other or to services outside docker, for this we not only need to run the image but also expose the container’s port and make it accessible by other services or containers which are on a different network.

Note: If you need to run docker as the root user, please remember to prepend sudo to the commands. 

Let us now see how we manage ports in Docker.

Similar Reads

Pulling Image From Docker Hub:

Docker Hub is a hosted repository service provided by Docker for finding and sharing container images. You can host your own images on Docker Hub, but we will use the official Nginx repository to pull images of Nginx which is an open-source reverse proxy server. An official image on docker has an official image tag....

How To Know Which Port Is Exposed?

The one question which still remains is, how will we know which port in the container should we map our computers port to? Usually, this detail is provided on the image repository or using the inspect command....

Creating And Exposing Local Containers:

Well, this was easy, let us create a local docker image for a react app and publish its port:...