Deploy Your Own Custom Docker Image on Amazon ECS

Ankita Kumari
Underscore
Published in
7 min readSep 21, 2018

--

image source: http://www.idexcel.com/blog/tag/ecs-docker/

This tutorial requires prior knowledge of using Docker

Introduction

If you have been in the world of IT for even a year or two, I am pretty sure you would have heard about Docker. For those who have just heard of it and never used it, docker helps solve the problem for the teams working on different platforms by providing an isolated environment for the application so that it runs on all platforms alike. For a more detailed read about what is docker and why is it so popular refer to this.

Docker is good but then why do we need Amazon ECS? This was the first question that came to my mind when I was suggested by someone to use ECS for deploying my own custom docker images.

I have always been deploying my docker images on my own server by managing configurations and custom automation, this is where ECS helped me. It gives me these features right out of the box saving my time and additionally it is fast, simple, cost-efficient and secure.

So let us begin by pushing our image to AWS. If you use some other registry for pushing your Image you can directly move to the deployment part.

Create a Repository

This step can be skipped if you are using some other registry for your image

When we build an image for our application, we decide to either make the image available to the whole world or keep it a secret. If we want the world to use it, we can push the image to Docker Hub for free and if we want to keep our image private we will need to pay to Docker Hub and get a premium account. I personally use Amazon ECR to push your image as I have an AWS Free Tier Account. You can skip this part if you want to use Docker Hub.

To create a repository on AWS

  • Select Elastic Container Service from AWS service list and proceed on to select Repositories option from the menu
  • You will be prompted to create a repository by entering a name and you will get the URL for your repository
Configuring the repository

Clicking on next step, you will get a set of commands that need to be followed to push your image to this repository. In case you are using MacOS or Linux, you need AWS CLI to run this command and if not installed you can install it as explained here.

  • Once AWS CLI is installed, go to your terminal and start executing the commands

Paste the output of this step to login to your AWS account login.
The next step builds our Docker image and in case you have already built your Docker image proceed with the next step and tag your image.

Then push the image to your AWS repository

Once your image is pushed, you can move to your AWS console and view the list of your repositories. You will find your image listed and on clicking on the image name, you will be routed to the detail page of your image. You will need your repository URI for next steps.

Deploying to Amazon ECS

You can read this article that explains the complete architecture of Amazon ECS to get a better understanding and will also help you understand the steps

AWS provides you with two options to run your containers.

  1. Fargate (Network Only): A serverless infrastructure maintained by Amazon ECS i.e. not exactly server less but you do not need to take the pain of maintaining and scaling the server.
  2. EC2: We can use Elastic Compute Cloud with ECS to get more control over the server on which your container would be running.

Using Fargate

  • To deploy the image you will need to create a cluster, select Elastic Container Service from AWS service list, select Clusters and create
  • As we are using Fargate in this section the obvious choice is Network Only and proceed with creating a cluster by entering the name of the cluster . and networking details.
  • Once the cluster is created, you will be creating a task that will be running on the cluster we just created. To create a task move to Task Definitions option on the ECS menu and create a task definition.

Configure the task definition

Next, add a container to the task definition. You can use Docker hub’s URL if your image is deployed on docker hub. You can also configure other parameters like entry point, working directory, environment variables and volumes for a container. This image does not require any of those.

After you add a container you also get an option to check the configurations as JSON and the same can be edit for changing the configurations. Now we can create the task definition. As the task definition is created, we head over to the cluster.

  • On the cluster details, under the tab Tasks run a new task by clicking on Run new Task and the following screen appears
  • Submit the details by clicking on Run. You will be able to view your task listed under the Tasks tab on the cluster with a RUNNING status.
  • You can click on the task and get the details of your running service. In the network section, you can see your public IP where you can see your service running. This is not a static IP and it changes if you restart your task. You can use Amazon ALB to assign an Elastic IP.

And finally, you have your dockerized application on Amazon ECS using Fargate. Now it’s time to ditch Fargate and try EC2

Using EC2

In order to deploy an image on ECS using EC2, we follow similar steps like we did when using Fargate. The difference would be mainly while creating the cluster where we need to attach an EC2 instance to the cluster.

  • Create a new cluster and choose EC2 Linux + Networking and move to the next step
  • Enter a cluster name, provisioning model, instance type, number of instances, EBS storage and key pair.
    For instance type, there is a variety of options available with AWS for a detailed view you can refer here.
    Key pairs are the private keys generated and stored with the user to access the instance. If there is no existing key pair for the region you are working on you can refer this to create a new key pair.
  • In the Networking section, open the ports that your application would be using. Though it is not secure to open ports for the complete world you can edit it anytime in security groups.
  • Click on Create and your cluster will be created. When we view the cluster, the ECS Instances tab has an ec2 instance listed.
  • Now that we have created a cluster, the next step would be to create a task definition and we need to follow the same steps as we did while using Fargate. The only difference is choosing the compatibility of our task will be EC2 instead of Fargate.
  • We follow the same steps to create a task definition, configuring it, adding a container to it and starting it just as we did above using Fargate.
  • After creating task we will be running it on our cluster. Just one change needs to be made for the launch type which will now be EC2 instead of Fargate
  • Once the task status changes to RUNNING. Click on the EC2 instance that is listed under the tab of ECS Instances and we will be redirected to the details of the EC2 instance like below. The instance has a public DNS which you can use to access your service.

With this, our dockerized application is up and running on Amazon ECS.

Thanks for reading, I hope it will be helpful for anyone who is trying to use Amazon ECS. Kindly leave your feedback in the comments and help me grow.

--

--