amazon-web-servicesDeploy a docker container image using ECS


Remarks

Before you can add ECS instances to a cluster you must first go to the EC2 Management Console and create ecs-optimized instances with an IAM role that has the AmazonEC2ContainerServiceforEC2Role policy attached.

  1. Go to your EC2 Dashboard, and click the Launch Instance button.
  2. Under Community AMIs, search for ecs-optimized, and select the one that best fits your project needs. Any will work. Click next.
  3. When you get to Configure Instance Details, click on the create new IAM role link and create a new role called ecsInstanceRole.
  4. Attach the AmazonEC2ContainerServiceforEC2Role policy to that role.
  5. By default, your container instance launches into your default cluster. If you want to launch into your own cluster instead of the default, choose the Advanced Details list and paste the following script into the User data field, replacing your_cluster_name with the name of your cluster.
#!/bin/bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config
  1. Then, finish configuring your ECS Instance.

NOTE: If you a creating a web server you will want to create a securityGroup to allow access to port 80.

  1. Create a Repository: aws ecr create-repository --repository-name example-repository
  2. Authenticate your Docker client to your registry: aws ecr get-login --region us-east-1 | sh
  3. Build your Docker image: docker build -t example-image .
  4. Tag your image so you can push the image to this repository: docker tag example-image:latest example-namespace/example-image:latest
  5. Push this image to your newly created AWS repository: docker push example-namespace/example-image:latest
  6. Register an ECS Task Definition: aws ecs register-task-definition --cli-input-json example-task.json
  7. Run the task: aws ecs run-task --task-definition example-task