Pushing Custom Docker Image to Docker Hub
Tags: docker, dockerhub, dockerfile, container
Published: 2022-09-27 15:09:53


In this tutorial, we will be going through steps for building, tagging and pushing a custom docker image on a Docker Registry. Docker Hub is one of the most popular Registry, another names are ACR (Azure Container Registry) and Amazon ECR ( Amazon Elastic Container Registry). In this article we will be going through the steps to push our custom image on Docker Hub.

For simplification I have kept this article in few sections, those are:

1. Create a Docker Hub Account
2. Generate an access token (why to not use password)
3. Login to docker registry
4. Building a custom image
5. Tag it and Push the image to container registry

---

**<u>1. Create a Docker Hub Account :</u>**
 
First, we need a docker hub account, please visit the page https://hub.docker.com/ and signup for an account, the free account provide you to publish unlimited public repository, if you need private repository, you will have to upgrade your account. Username of your account is important. To access any image from your repository we will have to use 

```
Docker pull <username>/<repository name>:<tag>
```
If we don't pass tag it will fetch the latest image.

**<u>2. Generate access token:</u>**
For login into docker hub account from docker cli we can use username and password of the account, but this way it will be getting the full access of your account, and also if you are changing the password, you will have to login again.
The Recommended approach is to login in docker hub from cli to use access token so you can limit the access:
Go to Account Settings > Security , In my case it is showing some existing token but in your case it may be blank, Now click on New Access Token:

![Docker Hub Access Token List](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5sf7lly6l7qqt9nedgu0.png)
We want to Read and Write So we can select Read & Write, but if you want to perform delete too then use that permission and then click generate. After generate it will show the token, copy it and keep it at safe place, it will not show again.


![Docker Hub Access Token Add New](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsds1xmurlz7gx05knnn.png)
**<u>3. Login to Docker Registry:</u>**

```
docker login -u <username> -p <accesstoken or account password>
```
Or just run run command `docker login` and it will ask username and password.

> The recommended approach is ,To increase security, use the - password-stdin flag to instruct Docker to read your password from STDIN. This lets you pipe in a password file, preventing plain text from being captured in your shell history and CI job logs.

```
cat password.txt | docker login - username <username> - password-stdin
```

Here, In passwor.txt, we have saved our access token value.

![Docker Login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u14nb42teovjdb6zoy3j.png)

**<u>4. Building a custom image:</u>**
I have a very simple example by using nginx as base image here is an example, [Git Repo Link](https://github.com/nitin27may/docker-images-example.git) 
 
We just need a sample html file and then we have to create a Dockerfile and keep both file in same folder:
Dockerfile code:

```
FROM nginx:latest
COPY index.html /usr/share/nginx/html
```

In this docker file, we are pulling latest nginx image and top of that we are copying sampleindfex.html in to html folder, let's build the custom image: 
```
docker build -t my-custom-nginx .
```
In above command we are building image by passing build context as current folder, so that we are passing . (dot).
Output:
![Docker Build Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7gpqo59557h9qkgs5bh.png)

**<u>5. Tag it and Push the image to container registry</u>**
Now tag locally created image with docker hub username and custom image name:

```
docker tag my-custom-nginx nitin27may/my-custom-nginx:1.0
docker push nitin27may/my-custom-nginx:1.0
```

In above command, 'nitin27may' is my account name, 'my-custom-nginx' repository name in docker hub and '1.0' is the tag, so the full tag is 'nitin27may/my-custom-nginx:1.0'

![Docker push Image Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hyulun6ri6im27r8fkyv.png)
Below is the screenshot from the Docker hub from my account.
![Docker Hub Repository Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x61bqg665do3hurgbdza.png)
In Next Article, I will be explaining that how we can automate this using GitHub Action.

If you have any suggestion, question, please reach out to me.