Deploying Docker Containers to AWS ECS using EC2 instances

Sanath Reddy
7 min readJun 29, 2021

--

This article will demonstrate how to upload a docker image to AWS Elastic Container Repository and deploy that image using an AWS Elastic Container Services Cluster with EC2

What’s Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

What’s ECS?

ECS is the core service to deploy your application update it and keep the desired number of instances of your app running (known as Tasks). More formally, it is a container management service that lets you run applications on a managed cluster of Amazon EC2 instances. Key terms to understand here are:

  • ECS cluster: An ECS cluster is a logical grouping of Amazon EC2 instances. You can use the grouping in any way you want. The most common use is to have separate clusters for prod, pre-prod, and staging environments.
  • Task definition: It is a recipe to execute your app. It tells ECS which docker container to use which deployment script to run how much memory to reserve what are the environment variables and so on. ECS uses task definition in order to spin up the desired number of instances of your app. Each such running instance is called a task.
  • Versioning: Once created task definitions are immutable. The only way to change any field is to make a copy of the existing version update the field and save the new version. Typically you only keep the latest version active and update the ECS service with it.
  • ECS service: This represents your application in its entirety. Here you can define the number of tasks you want running at any time which task definition version to use what the rate of deployment should be and what the task placement strategy should be.
  • EC2 container registry (ECR): The service offered by Amazon to manage a Docker environment. In general, a registry is used to host and distribute container images. For running Docker on AWS with ECS, it is not mandatory to use ECR, you could just as well use Docker Hub (both as a public or a private registry). An advantage of ECR is for instance, that it integrates nicely with ECS. Besides, it also allows using IAM (Amazon’s Identity and Access Management service) for authentication and permission management.

AWS ECS Launch Types

ECS tasks can be run in 2 modes, depending on your requirements:

  1. EC2: you are responsible for provisioning the EC2 instances on which your tasks will run.
  2. Fargate: AWS will provision the hardware on which your tasks will run. All you need to do is specify the memory and CPU requirements. Note that Fargate currently only supports nonpersistent storage volumes.

We’ll be using the EC2 launch type in this example.

Prerequisites

To keep this example as simple as possible, we’re going to assume you already have the following setup:

  • an AWS account with AWS CLI access setup
  • a default VPC (AWS creates this by default when you create an AWS account)
  • Basics of AWS(EC2, Vpc, Security groups, IAM)

Here We Go !!

Step 1: Create ECR instance and upload the Docker Image

we need to create a new ECS repository as shown below:

  1. In the ECS console, head over to Elastic Container Registry (ECR).
  2. Click on get started.
  3. Enter the repository name. I called mine touploaddockerimage.

4. Leave the remaining settings as default and click on Create repository. You should have a screen like the below:

Upload the image on AWS ECR

click on ‘View Push Commands’ (upper right-hand corner).

The first logs you into the ECR. The second builds the image on your machine. The third tags the image for upload. The fourth uploads the image. If you have already built the image, you only need to run the first, third and fourth.

Note: To do this, you will need to be logged into the AWS CLI with a user that has the correct permissions.

The image will be now published and available on ECR, If you look at AmazonECR repositories you can see the newly created image.

Step 2: Creating and Configuring Amazon ECS Cluster

Now the time has come to launch our first EC2 instance using AWS ECS. To begin with, let’s first search ECS on AWS console and follows the below steps.

Create Cluster — The cluster creation console provides a simple way to create the resources and it lets you customize several common cluster configuration options.

create cluster

on the Elastic Container Service console, click create cluster.

choose your EC2 instance

Select cluster template is the place where we need to make a choice. Do we want to manage EC2 servers or do we want to go on fully serverless. Networking only option is the Fargate option (serverless), If you choose EC2 Linux or EC2 Windows, ECS will create EC2 instances to orchastrate the containers. let’s move on with the EC2 Linux + Networking.

Launch EC2 instance — In this step, we are doing the configuration of our cluster. Some of these configurations are Network configuration, CloudWatch Container Insights, and Auto-Scaling groups. This is the most crucial step while creating your cluster because some of the configurations after the creation of the cluster cannot be reverted.

Next, you need to configure your cluster. On cluster configuration, we have 3 main sections — Cluster Name, Instance Configuration, Networking.

  1. I called my cluster name asec01.
  2. Set Provisioning Model as On-Demand Instance.
  3. For EC2 Instance type, select t3a.micro.( Ensure you select the correct instance configurations specific to your requirements)
  4. Under networking, set the VPC to the default VPC( Here, there is an option to create a new VPC, and Subnets should the user not have created his/her own).
  5. Set the Subnets to the first subnet in the dropdown.
  6. Set the Auto-assign public IP to Enabled.
  7. For the Security group use default value.
  8. Click on create then wait for the process to finish.

You can see the window as above. Once the cluster is created, we can now define our Tasks.

Step 3: Create a new ECS Task definition

A task is a set of metadata (memory, CPU, port mapping, environmental variables, etc) that describes how a container should be deployed.

To create a task definition, click on the Task Definitions tab on the far left-hand side and then the Create new Task Definition button and select the launch type as EC2.

  1. Enter the task name. I used testAppTask for the name of the task definition.

2. Enter task memory size as128 and click add container.

3. Add the name of the container. and set the container image URL(copy from ECR)

4.Set the port mappings. In the Host port, enter 80, and in the Container port enter8080.

5. Click add and scroll down to click on create.Now that the task is created.

Deploy the created task

Go back to your clusters and select the cluster we have created.

  1. select the tasks tab, and then ‘run new task’.
  2. select EC2 as the launch type.

3. Under task definition, select the task you created. It automatically fills in.

4. In the cluster name select the name of the cluster you have created and scroll down to run the task

There you have it, Your container is up and running!!🙂

Final Thoughts

Many other factors come into play I suggest exploring how to use ECS’s auto-scaling and load balancing features to deploy scalable containerized applications.

--

--

Sanath Reddy
Sanath Reddy

No responses yet