How to create a Docker Container Registry

How to create a Docker Container Registry  Ashish Pandey
Posted on Aug. 21, 2019, 10:06 a.m.

Running a Local Docker Registry

One of the most frequently asked question by developers is, how to set up a container registry for storing the Docker images?

A local container registry is useful especially when you do not want to push your local code to public container registries such as Docker-Hub or GCR.

In this article, I am going to explain how to create a local container registry on your own machine. This registry can be accessed from your local system as well as from other servers in the same private network (Provided the hosts’ name etc. is configured properly on the requesting servers).

To begin with, please understand that for this exercise, the registry itself is running as a container and listening at port 5000 of the host. A “Registry” application has been created, we will use the same.

We will launch the registry along with a bind mount, so that data does not go away when the registry is removed.

Prerequisite:

Docker needs to be installed on your server and its service needs to be running.

For example, for a centos-based system, first run:

$ sudo su
$ yum install docker -y
$ service docker start

The daemon needs to be explicitly set up to use HTTP for the insecure registry.

$ sudo vi /etc/docker/docker
# add this line

DOCKER_OPTS="--insecure-registry localhost:5000"

Close and save the file, then restart the daemon.

$ sudo service docker restart

Run the local registry:

Before we launch the registry containers, we need to arrange for persisting container data on host. This is because the containers are ephemeral, we should persist the content of the registry on host file system. To do that we will use abind mount”. The content of the registry will be persisted on ‘pwd’/registry-data.

In this example, the new container will use a host-mounted directory for bin-mount. When the registry server in the container writes image layer data, it appears to be writing to a local directory in the container, but it will be writing to a directory on the host.

Create a directory on the host machine for storing registry data

$  mkdir registry-data

We launch a local container of name “registry”, with “registry:2” image present on public remote registry(dockerhub.com).

$ sudo docker run -d -p 5000:5000 \
--name registry \
-v `pwd`/registry-data:/var/lib/registry \
registry:2

Pull a container image from dockerhub.com

$ sudo docker pull hello-world

Push the image in the local registry

These commands pull a public image from its store, tag it for use in the private registry with the full name localhost:5000/hello-world, and then push it to the local private registry:

$ sudo docker tag hello-world localhost:5000/hello-world
$ sudo docker push localhost:5000/hello-world

Test if the registry is working fine

On the local machine, you can remove the new image tag and the original image, and pull it again from the local registry to verify it was correctly stored:

$ sudo docker rmi localhost:5000/hello-world
$ sudo docker rmi hello-world
$ sudo docker pull localhost:5000/hello-world

Check the content of registry-data on the host

$ tree registry-data

You can try accessing this registry from other systems in your network by properly adding this server’s name to the requester’s /etc/hosts directly. When pushing or pulling the image from this registry you need to give full URL of the server, e.g. <servername>:5000/hello-world:latest


Ashish Graduated from MNNIT Allahabad in Computer Science stream. With ~10 years of experience Corporate experience in companies like Aricent in telecom domain & Guavus in big data domain, Ashish brings a lot of insight in Tech Industry Space. Here goes his expertise: Cloud Technologies - AWS, Azure, Docker, Kubernetes, Terraform, DevOps Tools - Chef, Puppet, Ansible, Jenkins, Programming - Python, C, Java. He worked with Nokia Siemens Network regarding Infrastructure Management. Apart from Tech. industry, he is passionate about the social sector.




Keywords : containers microservices docker


Recommended Reading


How to install Kubernetes Cluster on AWS EC2 instances

When someone begins learning Kubernetes, the first challenge is to setup the kubernetes cluster. Most of the online tutorials take help of virtual boxes and minikubes, which are good to begin with but have a lot of limitations. This article will guide you t...


Container is the new process and Kubernetes is the new Unix.

Once a microservice is deployed in a container it shall be scheduled, scaled and managed independently. But when you are talking about hundreds of microservices doing that manually would be inefficient. Welcome Kubernetes, for doing container orchestration ...


Why Learning Docker Containers is so important in IT industry?

Containerization/Dockerization is a way of packaging your application and run it in an isolated manner so that it does not matter in which environment it is being deployed. In the DevOps ecosystem containers are being used at large scale. and this is just...