DevOps

How to Use Docker Registry

Docker is a technology which allows one to create virtual machines which encapsulate applications and all its dependences in a container unlike a hypervisor which emulates an operating system and components on top of it. The advantage of this is the encapsulated containers then can be distributed among fellow developers through a Docker registry.

Docker consists of multiple important parts and they are Docker file which is actually the source code of the image, Docker Image which is a template of the container, is compiled and ready to be executed, Docker Registry is the service where images are located, finally the Docker Container which is the encapsulated virtual machine running on top of Docker Engine. Docker containers share the same operating system; hence the resource consumption is minimum to low compared to a hypervisor and similar virtual machines. This article mainly discusses about Docker registry, but discussing about other parts are important as they all are necessary to deal with a Docker registry.


How to Install Docker in a Nutshell?

Since this tutorial is about Docker registry, installation phase isn’t covered thoroughly, however this is quite enough to go through the installation as it contains the default way to install Docker straightly from its repository instead of the Ubuntu repository.

sudo su
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu zesty stable"
apt-get update
apt-get install docker-ce

The commands start from getting administrator rights with sudo su command, then it uses curl to add the docker package’s signature key to the system to make sure apt-get allows to continue the installation without showing a warning message for insecure packages, then the path to the repository is added from where apt-get retrieves the package files when docker package is summoned, then apt-get update command updates the local repository information with the latest package details. This is quite useful to make sure when upgrade command or install command is called, it definitely uses latest packages instead of older ones. Finally, it summons the docker community edition package to install in the system.


How to Use Docker Registry?

Docker registry is a service where images are hosted. There are two types of registries, private and public. As private some of the popular ones are Google Container, Quay, AWS Container, Docker Hub which is the default registry provided by Docker themselves. Docker registry is a community based host to where images can be uploaded, and from where images can be downloaded. The following steps demonstrate how to download an existing image from a registry, how to use it in the system, and how to upload a new image back to the registry.

How to Access to a Registry?

As the registry this tutorial uses the default public registry provided by docker themselves. However, it requires the user to register in the website. Even though registration isn’t required for downloading images, it requires for uploading new images back to the registry; hence this step is recommended.

  1. Visit the following web URL
    https://hub.docker.com/
  1. Register in the website with a username/email address
  1. Once registered in the registry, visit the following web url to browse available images
    https://hub.docker.com/explore/
  1. Pick one of them. This tutorial uses PHP image for demonstrating purpose, and its page is located at this location
    https://hub.docker.com/_/php/
  1. Use the following command in terminal window with administrator rights (by using sudo su). What it does is downloading the php image to install in the system.
    docker pull php
  1. Type the following command to open the docker file to execute the codes.
    nano dockerfile
  1. As the codes type the following command lines. What it does is to retrieve the php 7 files, copy command copy the files in the source directory to the destination directory, workdir command instructs to set the working directory as the given path, so when the container is running the dependencies are searched from here, cmd is for stating the file to be executed, here it uses a php script file which is later going to be executed.
    FROM php:7.0-cli
    COPY . /usr/src/myapp
    WORKDIR /usr/src/myapp
    CMD [ "php", "./donscript.php" ]
    
  1. Once the dockerfile is crafted, it has to be compiled with the build command. Compiling the dockerfile results in a docker image which is assigned a name here as well.
    docker build -t donapp .
  1. If the php script requires the assistance of a web browser to display its contents, the default web host shipped with php can be initiated with the following command.
    docker run php -S localhost:8000
  1. The script file has to be created and placed in the same directory as the dockerfile, which is created in the home folder by default in Linux, as seen in the following screenshot. The script name should be same as the name stated with step7’s CMD command.
  1. Finally, the image can be executed with the following command. As seen in the screenshot, once the image is executed it displays the following message written in the script.
    docker run donapp
  1. Alternatively, the file can be executed even without compiling with the following command. The highlighted string is the name of the script which is intended to be executed.
    docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:7.0-cli php donscript.php
    

How to Search Images in Registry?

Docker provides an inbuilt function to search images within the terminal window, this is useful to browse images with ease without opening the web browser. To search images from the registry, use the following command.

docker search <image name>

example : docker search ubuntu

How to Upload Images to The Registry?

Just as images can be downloaded to utilize, they can also be uploaded to the registry to share with public or coworkers depending on the nature of the registry. If it’s a private registry, it’s recommended for either personal usage or limited number of people, but if it’s a public registry, it’s recommended to share the images with strangers as well. Either way the registry has to be accessed prior to upload images, and it can be done with the following command. This step assumes that previous steps are followed, and there is already an account in Docker Hub along with its user credentials.

  1. Type the following command along with the username of the user
    docker login –username MYUSERNAME
    Type the password when it promotes
  1. Tag the application with following format. What it does is to tag donapp app as dondilanga/donapp, here dondilanga means the username of the user whom account is used to upload the image.
    docker tag donapp dondilanga/donapp
  1. Now type the following command to upload the image file. It appears as it uploads a large amount of data even if the script is quite small, the reason is it uploads the dependencies of the executable or script along with it, and thus other users can download it and use it right away without worrying about missing dependencies
    docker push dondilanga/donapp

For next steps see some of the Docker related links below:

https://linuxhint.com/how-to-create-a-docker-image/

https://linuxhint.com/networking-storage-docker/

https://linuxhint.com/optimizing-docker-images/

About the author

Admin

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.