DEV Community

Zahraa Jawad
Zahraa Jawad

Posted on

BUILD, TAG, AND PUSH DOCKER IMAGES TO DOCKER HUB AND AMAZON ELASTIC CONTAINER REGISTRY

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

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

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

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
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

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
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

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

Then enter your Docker Hub username and password:

Image description

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

Image description

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

Image description

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

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

Then the command cd to enter the directory:

Image description

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

Image description

Image description
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

The image is being built

Image description

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

Image description

Image description

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

Image description

My Image implementation:

Image description
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

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
First, Execute the following commend:

Image description

and then executing a push command:

Image description

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

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
Copy files to the image: You can copy files to the image using the copy instructions.

Image description

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

Image description

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

Image description

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

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

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

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

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

Image description

• 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

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

When you open the warehouse it is empty.

Image description

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

Login Succeeded

Image description

• 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

• 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

Image description

• 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

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/

Top comments (0)