Managing ports and private registries in Docker

Last updated on May 27 2022
Raigiri Patil

Table of Contents

Managing ports and private registries in Docker

Docker – Managing Ports

In Docker, the containers themselves can have applications running on ports. When you run a container, if you would like to access the application in the container via a port number, you would like 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 would like to do a simple sign-up on Docker Hub.

Page 1 Image 1 62
signup

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

Page 2 Image 2 55
logon

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

Page 2 Image 3 30
jenkins

Step 4 − If you scroll down on the equivalent page, you can see the Docker pull command. This will be employed to download the Jenkins Image onto the local Ubuntu server.

Page 3 Image 4 29
pull

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

sudo docker pull jenkins

Page 3 Image 5 23
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

Page 5 Image 6 15
inspect jenkins

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 would like to change the Docker run command and add the ‘p’ option which specifies the port mapping. So, you would like to run the subsequent 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’ll see Jenkins up and running.

Page 6 Image 7 7
docker host

Docker – Private Registries

You might have the need to have your own private repositories. You may not want to host the repositories on Docker Hub. For this, there is a repository container itself from Docker. Let’s see how we can download and use the container for registry.

Step 1 − Use the Docker run command to download the private registry. This can be done using the subsequent command.

sudo docker run –d –p 5000:5000 –-name registry registry:2

The subsequent points need to be noted about the above command −

  • Registry is the container managed by Docker which can be employed to host private repositories.
  • The port number exposed by the container is 5000. Hence with the –p command, we are mapping the equivalent port number to the 5000 port number on our localhost.
  • We are just tagging the registry container as “2”, to differentiate it on the Docker host.
  • The –d option is employed to run the container in detached mode. This is so that the container can run in the background
Page 7 Image 8 6
registry

Step 2 − Let’s do a docker ps to see that the registry container is indeed running.

Page 7 Image 9 7
docker ps

We have now confirmed that the registry container is indeed running.

Step 3 − Now let’s tag one of our existing images so that we can push it to our local repository. In our example, since we have the centos image available locally, we are going to tag it to our private repository and add a tag name of centos.

sudo docker tag 67591570dd29 localhost:5000/centos

The subsequent points need to be noted about the above command −

  • 67591570dd29 refers to the Image ID for the centos
  • localhost:5000 is the location of our private repository.
  • We are tagging the repository name as centos in our private repository.
Page 8 Image 10 4
centos

Step 4 − Now let’s use the Docker push command to push the repository to our private repository.

sudo docker push localhost:5000/centos

Here, we are pushing the centos image to the private repository hosted at localhost:5000.

Page 8 Image 11 3
push

Step 5 − Now let’s delete the local images we have for centos using the docker rmi commands. We can then download the required centos image from our private repository.

sudo docker rmi centos:latest sudo docker rmi 67591570dd29

Page 9 Image 12 3
rmi centos

Step 6 − Now that we don’t have any centos images on our local machine, we can now use the subsequent Docker pull command to pull the centos image from our private repository.

sudo docker pull localhost:5000/centos

Here, we are pulling the centos image to the private repository hosted at localhost:5000.

Page 9 Image 13 1
pulling centos

If you now see the images on your system, you’ll see the centos image as well.

 

So, this brings us to the end of blog. This Tecklearn ‘Managing ports and private registries in Docker’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Docker and build a career in DevOps domain, then check out our interactive, Containerization using Docker Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/containerization-using-docker/

Containerization using Docker Training

About the Course

Tecklearn has specially designed this Containerization using Docker Training Course to advance your skills for a successful career in this domain. his Docker training online course will help you learn Docker containerization, running Docker containers, Docker image creation, Dockerfile, Docker orchestration, security best practices and more through hands-on projects and case studies. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Docker.

Why Should you take Containerization using Docker Training?

  • Average salary of Docker Expert is $110k – Indeed.com
  • According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.
  • Intuit, PayPal, Splunk, Uber & many other MNC’s worldwide use Docker across industries

What you will Learn in this Course?

Introduction to DevOps

  • What is Software Development
  • Software Development Life Cycle
  • Why DevOps?
  • What is DevOps?
  • DevOps Lifecycle
  • DevOps Tools
  • Benefits of DevOps
  • How DevOps is related to Agile Delivery
  • DevOps Implementation

Containerization using Docker – Part 1

  • Introduction to Docker
  • Understanding Docker Lifecycle
  • Docker Architecture
  • Components of Docker Ecosystem
  • Common Docker Operations
  • Committing changes in a Container
  • Hands On

Containerization using Docker – Part 2

  • Building Custom Docker Images
  • Docker Image
  • Introduction to Docker Swarm
  • Deploying a 2-Node Cluster using Docker Swarm
  • Hands on

 

0 responses on "Managing ports and private registries in Docker"

Leave a Message

Your email address will not be published. Required fields are marked *