Docker allows developers to package applications in containers - standardized executable components that combine application source code with the operating system (OS) libraries and dependencies needed to run that code in any environment. Amazon also has container launch and storage solutions - Elastic Container Registry (ECR). In this article, I will show you how to create your own container, upload it to the repository.
Create a Docker Image
First, let's create our own container. For the demonstration, I'll be using an EC2 instance on Ubuntu. The instance must have Docker installed and run the command:
For our example, we use the approach httpd on amazon linux. Next, write code (Image 1):
RUN yum -y update
RUN yum -y install httpd
RUN echo 'Hello World from ECR! This is version 1' > /var/www/html/index.html
Next, let's compile the image and run the command:
docker build -t hello-world-app .
Do not forget to write dot after -app. And then check that it is present in our images, run the docker images command (Image 2).
As we can see, you have two new images created. This completes the first step.
Creating a repository
The second, let's open the AWS Management Console and then to the ECR service, then go to the Repositories tab and click Create a repository (Image 3).
Here we need to enter the name of the repository and create a private one. I want to note that Amazon has restrictions on private, public repositories and be careful not to exceed the limit. Next, we will go to the repository, and as we see that it is impossible to upload our image to the repository. Moreover, we do not even have a button. Next, click the View push commands button and see Amazon's commands (Image 4).
These commands are for the old version of the CLI, but I will show you how to use a much simpler command. Next, we need to rename our image to the name that AWS suggests to us. Let's copy this freak name (Image 5).
Next, go to our terminal and run the command:
docker tag hello-world-app:latest 977701475595.dkr.ecr.us-east-1.amazonaws.com/app1:v1
You can see the result in image 6.
Next, as I described earlier, we will run a command to send our image to the repository. To do this, run the
aws configure command and enter the
Access key id and
Secret access key. Next, we will install the region in which you have deployed the repository (Image 7).
To push the image, you need to log in by entering the command:
aws ecr get-login --no-include-email --region us-east-1
Then we receive an unusual answer (Image 8). So let's copy it and run it (Image 9).
Using the command from View push command:
docker push 977701475595.dkr.ecr.us-east-1.amazonaws.com/app1:v1
Let's send our image to the repository. After that, we can open AWS Management Console and see our container in the ECR service (Image 10).
In conclusion, I showed how to create your own Docker container, then place your own container in the Elastic Container Registry. As you can see, this is not as complicated as it might seem at first glance, and now everyone, after reading this article, can do the same.