Docker Container Management on AWS: ECS, Fargate and ECR - Part 1

·

5 min read

Docker Container Management on AWS: ECS, Fargate and ECR - Part 1

Hello Guys !! I hope you are doing great😊

In this article let's learn about Containers on AWS. We heard about these services when working on AWS DevOps. Let's learn about this in this article.

AWS ECS (Elastic Container Service), EC2 (Elastic Compute Cloud), Lambda, and Fargate are all compute services.

We know about docker, docker hub, image, containers, and Kubernetes. Let us understand what are the services in AWS that offer the same features as Docker and its components.

ECR (Elastic Container Registry)

Let's revise what a docker hub is. Docker Hub is a cloud-based registry/repository for Docker images. Similarly, AWS ECR is a fully managed container registry offered by Amazon. It is a proprietary service offered as part of AWS.

We write a docker file and need to push it to the docker hub or we can also push it to the AWS ECR and pull from ECR and run the image which will create a container.

ECS (Elastic Container Service)

ECS manages and orchestrates containers. It allows you to run and manage Docker containers without worrying about the underlying infrastructure. There is one service that plays similar roles as ECS where you can manage containers. That is EC2.

ECS Vs EC2

Let's know the difference between ECS and EC2. In EC2 we need to install and configure software as per our requirements. You have to take care of managing the servers and the installation process, whereas in ECS, many of those tasks are abstracted away and taken care of by the service itself.

• ECS (Elastic Container Service) utilizes EC2 (Elastic Compute Cloud) instances in the background to run your containers.

• AWS takes care of provisioning and managing the EC2 instances required to run your containers.

• ECS abstracts away the underlying EC2 infrastructure and provides a simplified container management experience, it still relies on EC2 instances to actually execute and host the containers. ECS takes care of provisioning, managing, and scaling the EC2 instances as needed to ensure the smooth operation of your containers

If you use EC2 to manage containers:

  • You would typically deploy your application components, such as the frontend, backend, and database, on a single EC2 instance.

  • All the components would reside on the same server, and you would have to manage the installation, configuration, and scaling of each component manually.

  • This approach is known as a monolithic architecture, where all components are tightly coupled within a single instance.

On the other hand, if you use ECS to manage containers:

  • Containers are created separately for each component of your application, such as the front end, back end, and database.

  • Each component is containerized and can be managed independently, allowing for better isolation and scalability.

  • You can deploy multiple containers across multiple EC2 instances within an ECS cluster, distributing the workload and resources more efficiently.

  • This approach is typically based on a microservices architecture, where components are decoupled and can be scaled independently.

    Fargate

Fargate provides a serverless experience where the infrastructure management is abstracted away, making it simpler to operate and eliminating the need for manual provisioning and scaling of EC2 instances.

ECS Vs Fargate

Amazon ECS offers two launch types:

EC2 and AWS Fargate. While both options allow you to run containers.

EC2 Launch Type:

With the EC2 launch type in ECS, you have control over the EC2 instances that run your containers. You are responsible for provisioning, configuring, and managing the EC2 instances. You need to choose the appropriate instance types, manage security updates, handle scaling, and monitor the instances yourself. You have more control over the underlying infrastructure, but it requires more operational overhead.

AWS Fargate Launch Type:

Fargate is a serverless compute engine for containers that abstracts away the underlying infrastructure. With Fargate, you no longer need to provision or manage EC2 instances. Instead, you can define and run your containers as tasks directly on Fargate. It automatically handles the underlying infrastructure, including server provisioning, scaling, and management. You only pay for the resources consumed by your containers, without worrying about the EC2 instances.

Fargate offers a higher level of abstraction and removes the need for manual infrastructure management, while the EC2 launch type requires more involvement in managing the underlying EC2 instances.

Hands-On

Let's deploy node js app using ECS Fargate 🙂

Create an instance and connect using SSH

Clone the repo

https://github.com/AasifaShaik029/node-todo-cicd.git

Attach IAM role with the necessary permissions

Create a IAM role which have below permission assigned.

Note :

If we use other servers instead of AWS Instance, we use AWS access keys to connect and do AWS Configure. As we are using AWS service ( instance ) we need to use IAM role to assign permissions.

AmazonEC2ContainerRegistryFullAccess
AmazonElasticContainerRegistryPublicFullAccess

Now add the IAM to this instance as below

So far we set one server with the necessary permissions needed to interact with ECR.

Now by using this instance, we can now push our image to ECR which is like a docker hub.

Steps to push the image into ECR

  1. Create public repo

Execute PUSH commands

When you click on 'view push commands', you will see the command used to push the image to ECR

Execute the commands in the ec2 instance

Before that install the below commands to install AWS CLI and Docker

sudo apt update
curl "https://d1vvhvl2y92vvt.cloudfront.net/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt install unzip
unzip awscliv2.zip
sudo ./aws/install
sudo apt  install docker.io -y 
sudo reboot

We now have both things installed

Now execute all the commands to push the image to ECR

Our image is successfully pushed into ECR

Create an ECS Cluster

Cluster is created

Create Task definition

To run a Docker container in ECS, you need a task definition

Now create Task definition

paste the image URL from ECR Repo, ports

Run the Task

Now lets RUN the task

Select lunch type as fargate

Add security group by allowing 8000 under networking

Now my task is running

Check if our app is running by using ipaddress from the task

🤩🌈Our App is successfully deployed 🤩🌈

Â