
- Docker Tutorial
- Docker - Home
- Docker - Overview
- Docker - Installing Docker on Linux
- Docker - Installation
- Docker - Hub
- Docker - Images
- Docker - Containers
- Docker - Working With Containers
- Docker - Architecture
- Docker - Container & Hosts
- Docker - Configuring
- Docker - Containers & Shells
- Docker - File
- Docker - Building Files
- Docker - Public Repositories
- Docker - Managing Ports
- Docker - Private Registries
- Building a Web Server Docker File
- Docker - Instruction Commands
- Docker - Container Linking
- Docker - Storage
- Docker - Networking
- Docker - Setting Node.js
- Docker - Setting MongoDB
- Docker - Setting NGINX
- Docker - Toolbox
- Docker - Setting ASP.Net
- Docker - Cloud
- Docker - Logging
- Docker - Compose
- Docker - Continuous Integration
- Docker - Kubernetes Architecture
- Docker - Working of Kubernetes
- Docker Useful Resources
- Docker - Quick Guide
- Docker - Useful Resources
- Docker - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.

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

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

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.

Step 5 − Now go to the Ubuntu server and run the command −
sudo docker pull jenkins

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

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.
