Deploy Docker Container On AWS ECS !
Step by Step guide for DevOps Engineer friends
In this blog post, we will see how to deploy a docker container on AWS ECS platform, as usual, this blog is more focused on a particular guide rather than theory. To get a core understanding of ECS and ECR read amazon web service official documentation.
Let’s jump and login into the AWS console.
Create New Policy!
To avoid permissions and policies issues, let’s create a new custom policy first, under IAM > Policies option click Create Policy button. Using Choose a Service option type “Elastic Container Registry” and select it.
Under Action section select “All Elastic Container Registry actions” checkbox.
Under Resources section select Specific option and click on the Add ARN option.
Create ARN (Amazon Resource Name) by filling all fields as per your requirements.
It’s optional to add some tags to your new policy, in the last step give some proper name to your policy like “custom_ecr_policy”.
Detail view of policy in JSON form.
Create New User !
Next we need to create a new user with name of container_testuser using IAM option of aws console. Select the required checkbox Access Key - Programmatic access and Password — AWS Management Console access options under AWS credential type and set the custom password.
Next we need to add required permission using AmazonECS_FullAccess and newly created custom_ecr_policy policy using Attach existing Policies Directly tab.
Up to this moment, we have a user account with required permission set perfect. !
Create ECR Registry
Now create ECR registry using aws console, in this scenario we created a private repository with the name of nodejs-express-server. We have already created a sample project and you found it on my GitHub repo. Clone my sample project any where in your computer or server.
To get a complete detail instruction and step, select the newly create repository in aws console and click on View Push Command.
We have already build our docker image in the last blog please follow this, now we need to Tag the image using following command.
docker tag nodejs-express-server:latest [XXX].dkr.ecr.ap-southeast-1.amazonaws.com/nodejs-express-server:latest
Before pushing image in the ECR repository you need to login using
get-login-password command and once you login you will get Login Succeeded message.
aws ecr get-login-password --region ap-southeast-1 | docker login --username AWS --password-stdin [XXX].dkr.ecr.ap-southeast-1.amazonaws.comLogin Succeeded
Now it’s the time to actually push the docker image AWS ECR area using following
docker push [XXX].dkr.ecr.ap-southeast-1.amazonaws.com/nodejs-express-server:latest
You can verify the image in aws console under.
Create ECS Part
It’s a service that orchestrate the docker container at a very highly scalable level. ECS is similar to Kubernetes. To start with ECS type “Elastic Container Service” in the AWS console and select it. Click on the “Create Cluster” button and select the “EC2 Linux + Networking” template option and click the Next button. On the next page give the proper name to the cluster like “testing-cluster” and set other options like EC2 instance type “t2.micro” and Key Pair. When you are done click Create button on the right bottom.
In the next few minutes, you will get a new EC2 working instance with a pre-configured network setting. To verify your newly created EC2 instance open the EC2 section in the console.
In this step we are going to create a new ECS Task using aws console option, under Amazon ECS option select Task Definitions and create new Task, next select lunch type compatibility EC2, next give proper task name in our case we set it “newclustertask”.
Set task size details !
Click Add Container button and put the basic details like Container Name*, Image* and Port mappings. Once you are done with setting click on Add.