Hey developers! Today we are going to see how to upload docker image to Docker Hub. So, let’s say you have a project of which you’ve created a docker image. Awesome! Now you need to store it in an online repository so that you, and everyone can pull the same image. That remote repository could be a number of places. But for our example, we will use the Docker Hub container registry to publicly upload our docker image. So, let’s begin.
Table of Content
- Log in to Docker Hub Locally
- Tag the Docker Image
- Push the Image to Docker Hub
- Pull Image from Docker Hub Registry
- Resolving Docker Issues
STEP 1: Login to Docker Hub From Command Line
To begin with, in order to login to docker hub from command line, make sure that the Docker service is enabled. If you are on Windows, then open the Docker Desktop tool. Next, you need to login to Docker Hub. Thus, open a terminal or CMD if on windows. I am using GitBash.
Open your Commandline and execute the following command:
This would ask you for your Docker Hub username and password. Just login by entering your credentials and it will prompt “Login Successful”.
STEP 2: Tag Docker Image
Next, you would tag Docker image so that it is uniquely identifiable when pulling the image. Let’s take the Docker image that I already have on my computer, named ubuntu. To attach a tag name, execute the following command:
// command format: docker tag IMAGE_NAME USERNAME/NEW_NAME:TAG docker tag ubuntu daniyal1217/my_ubuntu_image:0.0.1
- IMAGE_NAME is the name of the image on my local machine named “ubuntu”.
- USERNAME is the username of the Docker Hub
- NEW_NAME is the new name you may want to give to the image. It can remain the same too.
- TAG is the unique identifier. Usually, it’s given the version number of the project.
**Note: The TAG name MUST start with your Docker Hub username. Followed by a forward slash “/” and then the version number.
As a result of the above command execution, a new image would now have been built. To check it, execute the following command to list all the images.
Take reference from below image:
Create Image From Dockerfile
If you have a custom Dockerfile, from which you want to create an image while assigning a tag; then execute the following command:
// MAKE SURE YOUR TERMINAL IS OPENED IN THE SAME DIRECTORY WHERE THE DOCKERFILE IS docker build tag ubuntu daniyal1217/my_ubuntu_image:0.0.1 .
The dot “.” at the end of the above command denotes the location of the Dockerfile in your project.
How to remove Tags from Docker Image
// docker remove tag from image docker rmi REPOSITORY:TAG
STEP 3: Push an Image to Docker Hub
Finally, we are ready to push local image to docker hub. Simply execute the following command to publish it to your repository.
docker push daniyal1217/my_ubuntu_image:0.0.1
And you are done! Open Docker Hub account and see the published image there, as shown in the image below:
STEP 4: How to Pull Image From Docker Hub
To pull the image to any other machine, simply go to the image repository you pushed on Docker Hub. On the right-hand side, you will see the command to pull image from docker hub into your machine. Take reference from the image below:
Simply copy and paste the command to your terminal or CMD and the image would get downloaded. From there just execute the ‘docker run IMAGE_NAME:TAG’ command to run its container.
STEP 5: Resolving Docker Issues
1. Restart Docker Services
Before anything else, make sure to enable or restart Docker services. If you are on Linux or Mac, then run these commands:
sudo systemctl stop docker sudo systemctl start docker
To start docker service windows, simply open Docker Desktop that you installed on your windows machine.
2. Username when tagging Docker Image
When you publish your docker image, it needs a location of the account to publish in. That will be your Docker Hub account location. Which it will identify when you add your Docker Hub account username while you are attaching a tag to the Docker image.
Format = USERNAME / TAG_NAME
3. Retry Logging in
If it’s still not working, it’s worth trying to log out and log back in again. To do that, execute these commands:
docker logout docker login
STEP 6: FAQ
Yes! Public repositories are free. However, you only get 1 private repository and would have to pay to get more private repositories.
Read here to get more info: Docker Hub Documentation
Yes it is. Currently Docker Hub container registry is housing 3.5 million repositories as of 2020.
As claimed by Docker themselves: ?
“Docker containers are, by default, quite secure; especially if you run your processes as non-privileged users inside the container.”
- Amazon Elastic Container Registry (ECR)
- JFrog Artifactory
- Azure Container Registry
- Google Container Registry
- Nexus Repository Manager