Build & Push Docker Image to DockerHub using CLI

Introduction

Docker image is a set of command instructions for getting an application up and running, and we can create our own custom Docker image and push it to the DockerHub registry. 

Prerequisites

  • Basic knowledge of Docker.
  • Basic knowledge of Linux commands.

Step 1: Create a Sample Dockerfile

We need to create a sample dockerfile like Apache webserver that will use in build and push docker image.

To create a directory.

mkdir apache && cd apache 

To create Dockerfile.

nano Dockerfile

Paste the following command in oerder to build the docker image.

FROM ubuntu:latest
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y apache2
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

To build the apache’s custom docker image, We need to use follwoing command to build docker image.

To build docker image.

docker build -t aftab70/custom_apache:1.0.0 .

I am using aftab70/custom_apache DockerHub repository in order to where aftab70 would be account name and custom_apache would be repository name with tags.

Step 2: Verify Docker Image

We need to validate the custom Apache Docker image by following the given command.

To view the docker images.

sudo docker images

You should get output like this.

Test the custom apache image.

sudo docker run -p 80:80 -d aftab70/custom_apache:1.0.0

Test apache web page.

Step 3: Creating DockerHub Repository

We must first ensure that our Dockerhub account and repository are both available; if not, we can create them by following the instructions. 

To login or create Dockerhub account.

URL – https://login.docker.com/

After logged in Dockerhub account So then you need to click on Create repository button.

Then, as shown in the screenshot, enter the repository name, In my case i am putting custom_apache

When the Dockerhub repository is successfully created, the output should look like this. 

Step 4: Pushing Image to DockerHub

We are good to push the custom apache docker image by following command.

To login DockerHub using CLI.

docker login

You will be asked for username and password, You need to enter your cred and after that you should get Login Succeeded message on your terminal.

To push docker image.

docker push aftab70/custom_apache:1.0.0

You should get output like this.

Note: If you have a Docker image without a Dockerhub account name, then you need to tag your first image using the given command.

docker tag custom_image:tag_here account_name/custom_image_here:tag_here

Lets verify the Pushed Docker image in Dockerhub account.

Conclusion

We have successfully build and pushed docker image to DockerHub using command-line on ubuntu 22.04 LTS, If you still have questions, please post them in the comments section below.

Author

Build & Push Docker Image to DockerHub using CLI

7 thoughts on “Build & Push Docker Image to DockerHub using CLI

  1. Good post! We will be linking to this particularly great post on our site. Keep up the great writing

  2. Try to slowly read the articles on this website, don’t just comment, I think the posts on this page are very helpful, because I understand the intent of the author of this article.

Leave a Reply

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

Scroll to top