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.
Step2 β Once you have signed up, you will be logged into Docker Hub.
Step3 β Next, letβs browse and find the Jenkins image.
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
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.
In this guide, we learned about Managing Ports in docker. To know more click here.
Pingback: Docker - Compose - Adglob Infosystem Pvt Ltd