amazon-web-services Deploy a docker container image using ECS Deploy a sample application on AWS ECS service as a proof of concept


Follow following steps to try out a sample application on AWS ECS service as a proof of concept.

  1. Login to AWS management console and go to AWS service catalog - > Compute - > Ec2
  2. Create a VM(EC2 instance) using amazon linux 64 bit OS, this we will use to configure docker, git, AWS ECS agent tool and other tools. We will also use the same VM as a node in ECS cluster to deploy container based applications. Follow below steps to create a VM.
    a) Follow usual steps to create a EC2 instance, give special embhasic on subsequent steps during EC2 instance creation.
    b) Select a IAM role with least following permissions –
    c) Make sure java is installed on the VM
    enter image description here
  3. Installing docker [execute below commands]
    first update the yum package repository
sudo yum update –y

now to install docker execute yum install

sudo yum install -y docker
  1. Start docker service
sudo service docker start
  1. Add the ec2-user to the docker group so you can execute Docker commands without using sudo.
sudo usermod -a -G docker ec2-user
  1. Log out from the EC2 and log back in again to pick up the new docker group permissions.
  2. Verify that the ec2-user can run Docker commands without sudo.
docker info
  1. Installing Git
sudo yum install -y git
  1. Clone the sample PHP application on the Ec2 instance from git. We will use this application for our POC.
git clone  

cd ecs-demo-php-simple-app  

verify that Dockerfile exists by listing the directory contents

  1. Go to AWS service catalog -> Compute -> Ec2 Container Service
  2. Click on Get Started
    enter image description here
  3. Click on cancel
    enter image description here
  4. Click repositories from Repositories menu in left
    enter image description here
  5. Click on Get Started

enter image description here

  1. Enter repository name and click next
    enter image description here
  2. Configure Ec2 tools
aws configure

provide AWS Access Key ID, Secret Access key, default region name as per your account

  1. Build, tag, and push Docker image
    a) Retrieve the docker login command that you can use to authenticate your Docker client to your registry:
aws ecr get-login --region us-east-1

b) Run the command return as output of previous step

  1. Build the Docker image from your Dockerfile. (Recall Step 9, where you downloaded a sample docker app)
docker build -t amazon-ecs-sample .

(Note the “.” stands for current directory)
b) Run docker images to verify that the image was created correctly and that the image name contains a repository that you can push your changes to the docker image

docker images

enter image description here

c) Run the newly built image. The -p 80:80 option maps the exposed port 80 on the container to port 80 on the host system(Ec2 instance in this case).

docker run -p 80:80 amazon-ecs-sample

Ignore the warning “apache2: Could not reliably determine the server's fully qualified domain name, using for ServerName”

  1. Try to access the sample application webpage on browser, make sure port 80 is open in security groups associated with the instance

enter image description here

  1. Press ctrl + c key, this will stop the docker image. The sample application should not be accessible.
  2. Now after successfully verifying our sample docker application, we will try to configure a cluster to run the sample application automatically. Also, for the demo purpose we will try to use the existing ec2 instance as a node in the cluster. This can be achieved by installing a agent program on the ec2 instance.
  3. Installing Amazon ECS Container Agent on the ec2 instance
sudo yum install -y ecs-init

b) Restart the docker daemon

sudo service docker restart

c) Start the ecs-init upstart job

sudo start ecs

d) (Optional) You can verify that the agent is running and see some information on your new container instance with the agent introspection API. Make sure the port 51678 is open in security group.

curl http://localhost:51678/v1/metadata
  1. Go to AWS service catalog -> Compute -> Ec2 Container Service -> Cluster and verify a default cluster is created
    enter image description here
  2. Now we proceed with creating a task group and adding our docker image as task to run on the cluster
    a) Examine the simple-app-task-def.json file in the ecs-demo-php-simple-app folder.
    b) Edit the simple-app-task-def.json and redue the momeory, so that it can run on free tier eligible instance(i assume one is using free tier eligible ec2 instance for this POC, otherwise no need to reduce the memory limit)
    c) Update memory=250 in all the occurrence on the simple-app-task-def.json file
    d) Register a task definition with the simple-app-task-def.json file.
aws ecs register-task-definition --cli-input-json file://simple-app-task-def.json

e) Go to task definition in ec2 container service page, you ll find the registered task definition

f) Use the following AWS CLI command to run a task with the console-sample-app task definition.

aws ecs run-task --task-definition console-sample-app

g) Open the sample web app in browser, it should be accessible(refer step 19)

Thanks for reading, do share your comments and queries for follow up discussion.