Docker - Managing Ports



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 of how this can be achieved.

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.

Step 1 − First, you need to do a simple sign-up on Docker Hub.

simple Sing up

Step 2 − Once you have signed up, you will be logged into Docker Hub.

Logged Docker Hub

Step 3 − Next, let’s browse and find the Jenkins image.

Run Command

Step 4 − 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.

Local Ubuntu Server

Step 5 − Now go to the Ubuntu server and run the command −

sudo docker pull jenkins 

Inspect Image

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

Docker Inspect 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 

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.

Unlock jenkins
Advertisements