Docker Hub is a centralized repository where you can publish your Docker image. In this post, we will look at how to push Docker image to Docker hub.

This is an important activity in our Docker Step-By-Step Learning if you want to share Docker images with other developers.

So without wasting any time, let’s start.

1 – The Requirements

To push docker image to docker hub, there are two basic requirements:

  1. You should also have an account on Docker hub.
  2. You should have an application with Dockerfile.

A Dockerfile forms the basis of how a Docker image should be built. In other words, a Dockerfile helps to create an isolated environment inside the container. Also, using a Dockerfile, we can specify important configuration parameters such as the application port, application files etc. These details help run the Docker image.

On the other hand, Docker hub is a central location for all the images that you can use with Docker. As a developer, you can also upload images onto Docker hub so that other developers can access those images.

2 – Creating a Docker Hub Account

To create a Docker Hub account, you need to register on hub.docker.com.

Once you register yourself, you can opt to create a public or a private repository by clicking the button Create Repository in the below screenshot:

docker hub create repository

In the next screen, you can fill out the details about your repository as below:

docker hub create repository form

NOTE: You can choose to make your repository public or private. Basically, public repositories can be seen by everyone. However, with private repositories, you can restrict the access to yourself or your team-mates.

Once the repository is ready, we can push Docker image to Docker hub. However, before that, we need to build our Docker image.

3 – Building a Docker Image

To build a Docker image, we need a Dockerfile and an application.

I already have a Dockerfile for React application using Nginx on Github. If you wish to know more about how this image was created, you can refer to the post about building multi-stage Dockerfile for React application.

In a nutshell, it is a simple React application that we build and deploy on Nginx server using Dockerfile.

To build the Docker image, we can execute the below command:

docker build -t dashsaurabh/progressive-coder .

This command will create a new Docker image with the name dashsaurabh/progressive-coder. Note here that the naming convention should match the format of @DOCKER_ID/@REPO_NAME. Basically, here dashsaurabh is the Docker Id whereas progressive-coder is the name of the repository.

4 – Push Docker Image to Docker Hub

Now that the image is built, we can push it to Docker hub in our repository.

To do so, first we have to use Docker CLI to login to the Docker hub. Below is the command to login:

docker login

This will open up a prompt to enter the Docker Id and password. Upon authentication, we can execute the next command to push our Docker image.

docker push dashsaurabh/progressive-coder

You should get see the progress bar in the command line after which the success message appears.

docker push to docker hub

Once the image is pushed, we can also view it on hub.docker.com in our repository. Also, note that our image has the latest tag.

docker hub repository view

Another important thing to note here is about the image name. Basically, we already built our image with the correct name. However, many times, our image is having a randomly generated name.

In order to push such images, we can create a new tag for them using the Docker tag command. Below is an example for such a command.

docker tag <image_name> dashsaurabh/progressive-coder:latest

Conclusion

With this, we have successfully used Docker CLI to push Docker image to Docker hub. We also created a new private repository on Docker hub to place our image under.

Using the Docker pull command, we can also pull such an image to our local Docker host.

Categories: BlogDocker

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *