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:
- Create a Docker Hub Account
- Generate an access token (why to not use password)
- Login to docker registry
- Building a custom image
- Tag it and Push the image to container registry
1. Create a Docker Hub Account :
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.
2. Generate access token:
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:
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 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.
4. Building a custom image:
I have a very simple example by using nginx as base image here is an example, Git Repo Link
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:
5. Tag it and Push the image to container registry
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'
Below is the screenshot from the Docker hub from my account.
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.
Top comments (0)