BUILD, TAG, AND PUSH DOCKER IMAGES TO DOCKER HUB AND AMAZON ELASTIC CONTAINER REGISTRY
Published: 2023-09-22 23:03:09

**HEADLINES**
 Docker Image Tag
 Tag an Image by Docker tag Command
 Simple steps to create an image and tag it
 Upload the image to Docker Hub
 Amazon Elastic Container Registry (ECR)
 Upload the image to Amazon Elastic Container Registry (ECR)



**Docker Image Tag** 

In Docker, a "Tag" is an identifying tag attached to a Docker image to distinguish it from different versions of the same image or to specify other uses for it. The tag is appended to the image name with a validated comma (:) to create a unique name for this version or interpretation of the image. For example, if you are building a Docker image for a web application, you can assign a Tag to that image to tag different versions of the application.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/006s1wx7lqu50dert354.png)


• **SOURCE_IMAGE** is the name of the image you want to tag.
• **TAG** is the tag associated with the source image (optional).
• **TARGET_IMAGE** is the new name you want to give to the image.
• **TAG** is the new tag you want to assign to the image (optional).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9t5ozerazzgbnod6pdcv.png)

In this example: the image named my-image with the tag latest is being tagged as my-image:v1.0 More than one tag can refer to the same image. Docker images can contain multiple tags pointing to the layers of the same image, allowing you to easily manage versions and references. Each image has a tag, so when the image is pulled without any tag, it pulls the latest. When specifying the tag, it removes the delimiter from it, for example, redis:alpain.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqju3yu48dze3y1zgl2j.png)



**Tag an Image by Docker `tag` Command**


 We can explicitly tag the image with the Docker tag command. Tagging an image creates an alias for the name of the image. Here we will explore the way to tag an image. 
**Tag without name, by the command:** 
`docker tag [image] [user name of docker hub/image]` 
For example: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pk2bjtsxqleeqy8e24ak.png)
When we run the `docker Images` command, we show that the image is tagged and takes the (latest) name because it has no name given.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0iqsni9clg3mleauawq.png)


Note: I used Docker installed on an instance in the AWS account, and this was explained in the article:

https://dev.to/zahraajawad/docker-basics-with-some-of-its-commands-and-how-to-install-docker-by-aws-7b6 

**Tag with name, by the command:** 
`docker tag [image] [user name of docker hub/image]:[tag name]` 
For example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7t5ox43xihdef6a5gn8y.png)
And when we run the `docker Images` command, we show that the image is tagged and takes the name that is specified to it.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5k65y6aqps00n8pvdtsd.png)


Now, to upload the image to Docker Hub (the account must be available on Docker Hub to be able to upload the image), you must first login to your Docker Hub account with the command: 
`docker login` 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnl3vv3r59537u221kwg.png)


Then enter your Docker Hub username and password: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/glva3xaubhi368jxnkmh.png)


Now the image can be uploaded to Docker Hub by command: 
`docker push [username/image]`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4cqakzu6e01783uyy1f.png)

To make sure the image has been pushed to Docker Hub, go to Account and note that the image has been pushed.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56ngybt1jdk0q457krl9.png)







**Dockerfile**

Mind you, the application is still running on your machine, and you don’t have a Docker image yet. Of course, there are no magic wands you can wave at your app and turn it into a Docker container all of a sudden. You’ve got to write a Dockerfile and build an image out of it.
Docker’s official docs define Dockerfile as “a text document that contains all the commands a user could call on the command line to assemble an image.” Now that you know what a Dockerfile is, it’s time to write one.
Docker builds images by reading instructions in dockerfiles. A docker instruction has two components: instruction and argument.
A docker instruction can be written as :
`RUN npm install
`“RUN” in the instruction and “npm install” is the argument. There are many docker instructions but below are some of the docker instructions you will come across often and the explanation. Mind you, we’ll use some of them in this post.

**Docker Instructions**



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0pvxggm22bstxdjxnar.png)

**Simple steps to create an image and tag it**

• **Create a Dockerfile** 

Create a Dockerfile describing how to build the image. This file contains the instructions necessary to install and configure the application within the image.
 we can do it by the command: 
`mkdir dockerfile` 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edxfl9qe3hj27kwo91gt.png)


Then the command `cd` to enter the directory:



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9814qi94tunizmsnwrhr.png)


Then by the command `nano` write the instructions needed to install and configure the application within the image


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udfmv3ujojk46ss306lh.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xkugczqv1mqndkar9zx3.png)
This file uses the latest Ubuntu image as a base and prints a simple sentence. After writing the necessary instructions for the image “**This Is My Image!**” . Then follow the steps to store and exit the file: 
Ctrl+x : to exit 
Y : to save then enter.


**Building images:** 
Now build the image using the docker build command and command `tag` to add a name to the image 
`docker build - -tag [Image name]`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24wpqqfy7glvkai6cela.png)

The image is being built

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhixrxiokipggxmtemmp.png)

After completing building the image, it can be displayed with some details through the `docker Images` command.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y0ttkum1ur47bdl7gww.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k0c6ugo7v7yagfz4hcab.png)

To run the image by the command `docker run [image]`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mxyeft8b6ifh48vkx0t.png)

My Image implementation:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plmy2iobrz9qjg025k8m.png)
In the same manner as previously described, pushing to the Docker Hub can be made by logging into your Docker Hub account by the command `docker login`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rb2lrj7b19dpi4phsxr.png)

**Upload the image to Docker Hub:** 

After active login, you can upload the image to Docker Hub using the docker push command. Replace your docker-hub-username with your username on Docker Hub and your-image-name with the name you gave the image.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tbt5pd7wk53xg53haep.png)
First, Execute the following commend: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mb2h85feexq81jnmw4cj.png)


and then executing a push command:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynm6pfwca3lclurrf0i9.png)

**To confirm the upload:** 
After the upload is complete, you will be able to see the image on Docker Hub in your personal account, and everyone else can download the image from Docker Hub using docker pull using the full name of the image: docker.io/your-docker-hub-username/your-image-name. To make sure the image has been pushed to Docker Hub, go to Account and note that the image has been pushed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kmbt1g02w208tu6g6sio.png)

More elements can be added to your Docker image definition to achieve more functionality. That is, you can build your image to achieve the functionality you need and build and test it well to make sure it works as expected. Here are some examples of what you can add: Install additional packages or software: You can install additional packages or software within the image using commands such as apt-get for Ubuntu systems.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yptj19judsdmfpowy202.png)
Copy files to the image: You can copy files to the image using the copy instructions. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktw5dekwtyyrxg2h8yyp.png)


Define environmental variables: You can set environmental variables to configure the behavior of the image. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ntf3v1lvesy87c8pl52.png)


Run additional commands: You can set the commands that you want to be executed when the image is played.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di5joftym3wus07y2avj.png)

**Amazon Elastic Container Registry (ECR)** 

Amazon Elastic Container Registry (Amazon ECR) is a fully managed container registry offering high-performance hosting, so you can reliably deploy application images and artifacts anywhere. 

Use cases:
• Manage software vulnerabilities
• Streamline your deployment workloads
• Manage image lifecycle policies


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/280x3zhxrobmchql1yzg.png)


**Upload the image to Amazon Elastic Container Registry (ECR):**

Amazon Elastic Container Registry (ECR) is a managed AWS Docker registry service. In this topic, we will use the Docker CLI to push an image into Amazon ECR.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9eydfu48wt7g26vcc49a.png)



Here I am using Docker installed on an instance in my AWS account So, the first step here to upload the image to Amazon Elastic Container Registry (ECR) is to install the AWS Command Line Interface (CLI) to do the configuration with my AWS account.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f96kybfiq3q0ynvehrf6.png)

**To do this we follow the following steps:**

• Install awscli in my instance by the commands: 
`Sudo apt update` (To update the sudo library for Linux systems use APT) 
Then the following command to install the awscli: 
`Sudo apt install awscli`
• After the installation process, we perform the configuration process with the AWS account through the command `aws configure`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqm7vhqqeuys5lhyvktg.png)

• Enter the user's access key, secret access key, and the AWS account region in which the repository will be built.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14n0chta4q52r9p7nu8c.png)


• Create a repository in ECR to which you will upload the image by the command: 
`aws ecr create-repository --repository-name my-repo-name --region your- region`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfw7ys078ewo4hc75z0j.png)

To confirm that the repository has been created (ECR), go to Amazon Elastic Container Registry in your AWS account, and we will notice that the repository has been created successfully. 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zshn3w22ikwxhwfdt0ex.png)



When you open the warehouse it is empty.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5kjl8gyzst3pwnhgg7c.png)

Now return to the command line interface to log in to the repository and upload our image.
• Log in to ECR to obtain a permit by the command: 
`aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account-id.dkr.ecr.your-region.amazonaws.com` 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9m6tozy8sqsfvp4pm45.png)

Login Succeeded

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtc34lddpfxe2boyzbju.png)

• Describe the image using the address of the ECR repository you created: 
`docker tag my-image:latest your-account-id.dkr.ecr.your- region.amazonaws.com/my-repo-name:latest`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8r6gjqt3mari037ws9qz.png)


• Now use the docker command to `push` the image to the ECR repository:
docker push your-account-id.dkr.ecr.your-region.amazonaws.com/my-repo-name:latest


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kpn5d8584q6w5pqafxf6.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u3cpy4pcxbbirs2wptp.png)

• The image has been successfully uploaded to the repository in your AWS account.You can confirm by going to the repository in the account and noting the presence of the image.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6nom7eo2ho0sk4zj48d.png)




References:
• https://dev.to/zahraajawad/docker-basics-with-some-of-its-commands-and-how-to-install-docker-by-aws-7b6
• https://stackify.com/docker-build-a-beginners-guide-to-building-docker-images/
• https://aws.amazon.com/ecr/