Docker – Managing Ports

In this topic, we will discuss managing ports in docker. In Docker, the containers themselves can have applications running on ports. When you run a container, if you want to access the application in the container via a port number, you need to map the port number of the container to the port number of the Docker host. Let’s look at an example.

In our example, we are going to download the Jenkins container from Docker Hub. We are then going to map the Jenkins port number to the port number on the Docker host.

Step1 βˆ’ First, you need to do a simple sign-up on Docker Hub.

signup

Step2 βˆ’ Once you have signed up, you will be logged into Docker Hub.

welcome to dockerhub

Step3 βˆ’ Next, let’s browse and find the Jenkins image.

jenknis image search

If you scroll down on the same page, you can see the Docker pull command. This will be used to download the Jenkins Image onto the local Ubuntu server.

pull command

Step 5 βˆ’ Now go to the Ubuntu server and run the command βˆ’

sudo docker pull jenkins 
ubuntu command

Step 6 βˆ’ To understand what ports are exposed by the container, you should use the Docker inspect command to inspect the image.

Let’s now learn more about this inspect command.

docker inspect

This method allows one to return low-level information on the container or image.

Syntax

docker inspect Container/Image 

options

Container/Image βˆ’ The container or image to inspect

Return Value

The low-level information of the image or container in JSON format.

Example

sudo docker inspect jenkins 

output

output

The output of the inspect command gives a JSON output. If we observe the output, we can see that there is a section of “ExposedPorts” and see that there are two ports mentioned. One is the data port of 8080 and the other is the control port of 50000.

To run Jenkins and map the ports, you need to change the Docker run command and add the β€˜p’ option which specifies the port mapping. So, you need to run the following command βˆ’

sudo docker run -p 8080:8080 -p 50000:50000 jenkins 

Then, the left-hand side of the port number mapping is the Docker host port to map to and the right-hand side is the Docker container port number.

When you open the browser and navigate to the Docker host on port 8080, you will see Jenkins up and running.

jenkins unlock

In this guide, we learned about Managing Ports in docker. To know more click here.

This Post Has One Comment

Leave a Reply