Today, in this tutorial, we will learn about Docker Hub, including how to use it, how to create our own image, and how it helps in publishing and pulling images to and from Docker Hub. And, we will also spend some time exploring some of the popular repositories on Docker Hub.
Prerequisites
You require a basic understanding of Docker, familiar with Windows and Linux environment. This tutorial will use the Docker Desktop tool for practicing ongoing exercise, i.e., to build and push an image to Docker Hub. Windows and Mac users can download and install it from here, and Linux users follow this link.
What is Docker Hub?
Docker Hub is a repository registry service provided by Docker Inc. It allows us to pull and push docker images to and from Docker Hub. We can treat this as a GitHub, where we fetch and push our source code, but in the case of Docker Hub, we download or publish our container images. It is a cloud-based online repository that stores both types of repositories, i.e., pubic repository as well as the private repository. Public repositories are accessible to everyone, but the private one is accessible to the concerned owner of the repositories; also there is a cost associated with it if we store more than a certain number of repositories as a private.
Docker Hub Features
Docker Hub offers the following several features.
#1. Image Repositories
It helps us in finding and pulling container images from Docker Hub. It also helps us in pushing images as a public or private repository to the Docker Hub.
#2. Team and Organizations
It allows us to create workgroups and push the repositories as a private one, which is available for use within our organization only. This way, we have managed access to our private repositories of container images.
#3. GitHub and Bitbucket Integration
It allows integration with source code repositories such as GitHub and BitBucket.
#4. Automated Builds
If any change in the source code has been pushed to source code repositories, it automatically detects and builds container images from GitHub or BitBucket and pushes them to Docker Hub.
#5. Webhooks
Once we have pushed our images successfully, with the help of a webhook, it triggers an action to integrate Docker Hub with other services.
#6. Official and Publisher Images
The high-quality images provided by dockers are considered official images, and it can be pulled and used. Similarly, high-quality images provided by external vendors are publisher images, also called certified images, which gives support and compatibility guarantee with Docker enterprise. We will discuss more certified images later in this article.
Creating First Repository
This step requires logging in to the Docker Hub using your login credentials. If you don’t have an account, you can already create by clicking on the Sign-Up button available on the web page. Once you have signed in, you can create the repository by clicking on Create Repository on the welcome page.
Once clicked on Create Repository, It will ask for a name, give a name to your repository.
Select a visibility option from public or private.
We can also integrate our source code repositories like GitHub and BitBucket through build setting, but it is optional and can be done at a later stage as well.
Once everything is done, click on Create.
Congratulation! You have created your first repositories, which will look as follows. Docker Hub gives us only one private repository for free. Although, if we need more private repositories, we can upgrade our account to a paid plan. Now open the Docker Desktop tool/terminal, downloaded and installed above and sign-in to Docker Hub, by using a command.
Exploring the images
There are two ways to search public repositories and images from Docker Hub, that is, we can either search it from the Docker Hub website, or we can use the command-line tool and run the below command. Consider we want to search the MySQL repository image.
Downloading an image
We can download an image from the Docker Hub command using pull command as follows If we have already mysql image on our machine, then the above command will automatically update the image to the latest version. One thing to keep in mind here is that if we notice our docker search command output, there are many images of MySQL on Docker Hub, and that is because anyone can push an image. But it depends on us, which one to use based on our use-case., and we need to cheese the appropriate one. Let’s say we want to pull a bitnami/mysql image.
Creating an image
This process requires a Dockerfile. We can think a Dockerfile as an instruction manual that tells docker what to assemble. In short, it is a configuration file that keeps assembling instruction, what we tell it to assemble.
How does it work?
Docker reads the instruction from a Dockerfile and builds images automatically. Docker image is a layered file system, and it consists of multiple read-only layers, and each layer of a Docker image represents instructions of a Dockerfile. Let’s follow the below steps to create an image using Dockerfile. Create a Dockerfile, which specifies our application configuration. Note – The name of the file should be Dockerfile with a capital “D“. Let’s have a look at some of the important keywords used in Dockerfile
We can use # symbols to add a comment in a DockerfileThe “FROM” keyword defines the base image to be used.The “MAINTAINER” keyword is the person who is going to maintain that image.The “RUN” keyword is used to run the instruction given for the image. In our case, first, update the system and then install MySQL.The “CMD” keyword is used to execute a command once the container has been launched.The “COPY” keyword can be used to copy a file from our host OS to the docker container.The “EXPOSE” keyword is used to specify the port number to which the container will be running its process.
Run the below command to build our Docker image
Syntax:
docker build -t <your_username>/repo-name . In the above command, the name of the image is and “.” specifies the current working directory. This is an indication for Docker to look for the current directory of the docker file. “-t” is used to tag the image. We can see output similar to: Now let’s test our image by running it through run command of docker. Executing the above command will ask your Docker image to successfully install MySQL on your machine with all necessary updates, and finally, it will display echo message as well.
Pushing an image
Once our image has been successfully created and it is running, we can push it to Docker Hub through push command. We can check the image tags and status on Docker Hub, which will look something like this.
What is Docker Certified Images?
These are the official images pushed by either vendors or contributors. An image can only be certified by Docker Hub if its contents comply with rules, standards, and laws provided by Docker Hub. In short, that image must pass certain baseline tests. Docker Hub provides the inspectDockerImage tool, through which a vendor can self certify the images and plugins(Usually vendor or contributor publishes their plugins for logging volumes and networks). Note –
Popular images on DockerHub
There are many curated, and optimized images are available on Docker Hub. The popularity of these images depends on various factors such as pulls, market presence, ratings, satisfaction scores, etc. For a detailed list of most popular repositories, let’s navigate to the Docker Hub website. Use of an image also depends on OS and its architecture, If we know the pulled images will be used for which OS and its architecture, then we have to consider below key factors before pulling an image.
Look for a specific version using tags (mostly the latest).Opt the one which has maximum downloads and stars.Check for its updates (when it has been updated last).If possible, check its type, whether the verified publisher one or official(Docker Certified) one.
Webhooks
A webhook is a web callback that works with events, and it is a way for an application to provide real-time information to another application. Nowadays, almost every application provides a webhook facility, and so Docker Hub also has this feature. It is an HTTP push API triggered by user-designated events. We can use a webhook in docker to notify an application or service that uses the concerned images. Generally, we configure webhooks with docker as a pipeline of events, such that any upload of new images will trigger testing applications to run the underline test cases. Once the test result is successful, it will trigger another event, which will be container deployment, and once deployment has been done successfully, it will trigger other events to log the changes done so far.
Conclusion
I believe now you have a fair understanding of Docker Hub and how you can search, create, and push images. Keep in mind, when you push public images, it will be visible to anyone. If not already, check out this Docker installation guide.