Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / docker

How to Run Docker Container using AWS Elastic Container Service

5.00/5 (1 vote)
30 Aug 2021CPOL4 min read 5.8K  
An overview of AWS Elastic Container Registry (ECR) and Elastic Container Service (ECS)
In this article, I will show you how to create your own container, upload it to the repository and run it. I will walk you through the process step by step using many screenshots, and after reading, everyone will be able to run containers using AWS.

Introduction

If you're into programming, you've clearly heard of Docker. Docker is an open source containerization framework. It 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 are Elastic Container Registry(ECR) and Elastic Container Service (ECS). In this article, I will show you how to create your own container, upload it to the repository and run it.

Create an Image Container

First, let's create our own container. For demonstration, I'll be using an EC2 instance on Ubuntu. The instance must have Docker installed and run the command:

nano DockerFile

For our example, we use the approach httpd on Amazon Linux. Next, write code (Image 1):

FROM amazonlinux

RUN yum -y update

RUN yum -y install httpd

RUN echo 'Hello World from ECR! This is version 1' > /var/www/html/index.html

CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

EXPOSE 80

Image 1

Image 1 - Creating a Dockerfile

Next, let's compile the image and run the command:

docker build -t hello-world-app .

Do not forget about write dot after -app.

Next, to check that it is present in our images, run the docker images command (Image 2) .

Image 2

Image 2 - Docker images

As we can see, you have two new images created. This completes the first step. Let's open to the AWS Management Console and then to the ECR service, then go to the Repositories tab and click Create repository (Image 3).

Image 3

Image 3 - Create the repository

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 simply upload our image to the repository. Moreover, we do not have even a button. Next, click the View push commands button and see the commands that Amazon offers us (Image 4).

Image 4

Image 4 - Push commands

These commands are for the old version of the CLI, but I am going to 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).

Image 5

Image 5 - URL of repository

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 it in image 6.

Image 6

Image 6 - Rename the repository

Next, as I described earlier, we will run a command that will 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).

Image 7

Image 7 - Login on AWS

To do this, you need to log in by entering the command:

aws ecr get-login --no-include-email --region us-east-1

Then, we will receive an unusual answer (Image 8). Let's copy it and run it (Image 9).

Image 8

Image 8 - Login on AWS ECR

Image 9

Image 9 - Login Succeeded

Using the command from View push commands:

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 ECR sevice (Image 10).

Image 10

Image 10 - Our private repository is created

Create a Task Definition and Cluster

In order to deploy our repository, let's go to ECS and create a new Task Definition (Image 11).

Image 11

Image 11 - Create a new Task Definition

Then choose EC2 type (Image 12).

Image 12

Image 12 - Type of Task Definition

Next, let's give a name and add a container. Next, give a name to the container and specify a repository. To do this, go to ECR and copy the URI, then leave everything as default and click Create (Image 13).

Image 13

Image 13 - Create a new container based on ECR repository

Next, to run the Task Definition, we need to create a new cluster. Choose a Cluster and then tap on Create Cluster button (Image 14).

Image 14

Image 14 - Creating a cluster

Then, select the type of cluster EC2 linux+Networking (Image 15).

Image 15

Image 15 - Type of cluster

Next, enter a name for the container and select the number of instances (Image 16).

Image 16

Image 16 - Container details

This action takes a certain amount of time. Let's wait until our resources are created.

Next, add the container to the cluster. To do this, go to the Tasks tab in the cluster, select Run new Task. Here, we select the name of our Task and select Revision (Image 17).

Image 17

Image 17 - Create Run the Task

The new Task has been created. Next, we can check its work for this, go to the Task properties and find the Network bindings item, and there, we will find the External Link, by clicking on which we will receive the response from our container (Images 18,19).

Image 18

Image 18 - Task's details

Image 19

Image 19 - Container running

As you can see, our container has started.

Conclusion

In conclusion, I showed how to create your own Docker container, then place your own container in the Elastic Container Registry , and then run it on Elastic Container Service. As you can see, this is not a very complicated process as it might seem at first glance, and now everyone, after reading this article, can do the same.

History

  • 29th August, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)