Dockerhub or the official docker registry consists of numerous pre-built Docker Images as well as customized images by other users that you can pull to your system if they have been made public. In order to pull or push images to the docker registry, you need to first have an account in Dockerhub.
To create an account and a repository in dockerhub, you can follow these steps −
Visit the docker hub (Link − https://hub.docker.com/).
Create an account or if you already have one, sign in with your account details.
After signing in, click on create repository on the welcome page.
Fill in the details such as, repository name, visibility (public or private), etc.
Let us first see how you can pull an existing image from dockerhub.
You can do so by using the following command −
sudo docker run −it ubuntu
This will check if an ubuntu image already exists in your system or not. If it doesn’t, it will start pulling from the dockerhub.
You can also simply pull the image using the following command.
sudo docker pull ubuntu
Now, we are going to see how to publish your own customized image on dockerhub. After you have created your account on dockerhub and verified your email ID, you are all set to publish your first image.
After you have created the repository in dockerhub using the above discussed steps, open a terminal and run the following command to login yourself.
sudo docker login −−username=<USERNAME> −−email=<EMAIL ID>
Using your username and email ID in the above command, run the command and it will prompt you to enter your correct password. After entering your password, run the following command to check the list of images in your local system
sudo docker images
You can tag the image you want to publish using the following command.
sudo docker tag <image−id> <user−name>/<image−name>:<tag>
You should use such a tag name which will describe your image properly. You can either use the version of your image or simply a suitable project name.
After that you can push your image to the docker registry using the following command −
sudo push <user−name>/image−name
After this, your image will be published on dockerhub and if it has been made public, anybody on dockerhub will be able to pull it and use it accordingly.
To pull the image back and run it, you can use the following commands −
sudo docker pull <user−name>/<image−name>:<tag> sudo docker run −it <user−name>/<image−name>:<tag>
However, if your target is to just keep a backup or store your image somewhere so that you will be able to restore it back for future use, you should avoid publishing your image on dockerhub. The reason behind that is if you want to make frequent changes to your image and publish it multiple times and then push it back, it will consume a lot of bandwidth and if you are working on multiple images, it will cost you a lot of resources as well. One possible solution to this, is to just save the image as a tar file in your local system and load it back in case you need it.
To save a local copy of an image as a tar file, you can use the following command.
sudo docker save image−name > tar−file−name.tar
To load the image back, you can use −
sudo docker load −−input tar−file−name.tar
To conclude, in this article, we have seen how to create an account in the docker’s official registry called dockerhub, create a repository there, push a customized docker image there and pull it back and run it. We have also seen an alternative and effective way if you want to backup and restore your images by saving it in a tar file and loading it back for further use.
Creating a backup for docker images is very necessary because in case of a mishap, you don’t want to lose all your work and re creating all the stuff of that image exactly without having access to your previous dockerfile is a very hefty task.