Are you looking to create a big-scale application but are worried about how you’ll manage it? Maintaining such a big-scale application is a big task. How do you think you will tackle this undertaking? What will happen if your server goes down?
Perhaps it’s time you consider moving your Docker containers to Amazon Elastic Container Service (ECS). Don’t be scared. This AWS ECS tutorial teaches you how to deploy your Docker application and never worry about scaling and downtime again!
Prerequisites
This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:
- An active AWS account. You can register a free-tier account first if you don’t have one.
- Docker and AWS CLI must be installed on your computer. This tutorial uses Docker 20.10.20 and AWS CLI 2.8.3 on Ubuntu 20.04.
- An existing Docker image or a Dockerfile for building one. This AWS ECS tutorial will use a Docker image containing a web application serving a static website.
Creating a Repository in this AWS ECS Tutorial
To start with this AWS ECS tutorial, you first need to create a repository in Amazon Elastic Container Repository (ECR), where you’ll push your Docker image.
1. Open your favorite web browser and log in to AWS Management Console.
2. At the search bar, type in Elastic Container Service or ECS, and click Elastic Container Service from the results. Doing so will redirect you to the ECS page.
![Searching Elastic Container Service in the search bar](https://adamtheautomator.com/wp-content/uploads/2022/10/image-706.png)
3. Click on the Repositories sidebar menu.
![Opening Repositories](https://adamtheautomator.com/wp-content/uploads/2022/10/image-707.png)
4. On the Repositories page, click on the Create repository button. Doing so will redirect you to a form to fill out and create the repository.
![Initiating the repository creation.](https://adamtheautomator.com/wp-content/uploads/2022/10/image-708.png)
5. In Create Repository form, select the visibility of the new repository — Public or Private. Select Public in this example so that the repository will be accessible everywhere.
![Setting the visibility of the repository](https://adamtheautomator.com/wp-content/uploads/2022/10/image-709.png)
6. Next, enter the name of the new repository. Ensure that you follow the naming rules below.
![Naming the Repository](https://adamtheautomator.com/wp-content/uploads/2022/10/image-710.png)
7. Lastly, scroll to the bottom of the page and click the Create repository button.
![Creating Repository](https://adamtheautomator.com/wp-content/uploads/2022/10/image-711.png)
Pushing a Docker Image to ECR
Now that you’ve created the repository, the next step in this AWS ECS tutorial is to push the Docker image file to the repository.
1. Click the new repository named ecr-tutorial
to open it.
![Open the repository](https://adamtheautomator.com/wp-content/uploads/2022/10/image-712.png)
2. Click on the View push commands button to get the commands you must run on your computer to push an image to the repository.
As of this writing, AWS CLI is the only way to push Docker images to ECR.
![Viewing the push commands button](https://adamtheautomator.com/wp-content/uploads/2022/10/image-713.png)
3. Next, you should see a window similar to the one below. Copy the commands for later and click Close.
![Viewing the Docker image push commands](https://adamtheautomator.com/wp-content/uploads/2022/10/image-714.png)
4. Now that you have the commands to push the Docker image to the repository, start by running the first saved command to authenticate your Docker client to the registry.
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/n7a3i1h9
Wait to see a Login Succeeded
message to confirm a successful login.
![Authenticating your Docker client to ECR](https://adamtheautomator.com/wp-content/uploads/2022/10/image-715.png)
5. Next, execute the following command to build a new Docker image named ecr-tutorial
for this AWS ECS tutorial. Ensure that the Dockerfile file is inside the same directory.
Ensure that the Dockerfile file is inside the same directory. If you already have a Docker image with the same name as your repository in AWS ECS, skip this step.
This command will build an image with a website that displays the words “AWS ECS Tutorial @ AdamTheAutomator.com” listening on HTTP port 80.
docker build -t ecr-tutorial .
![Building the container](https://adamtheautomator.com/wp-content/uploads/2022/10/image-716.png)
6. Now that you built the Docker image, run the following command to tag the image with your repository prefix. This step enables you to push the image to your ECR repository later.
docker tag ecr-tutorial:latest public.ecr.aws/n7a3i1h9/ecr-tutorial:latest
7. Finally, run the following command to push the Docker image to the repository.
docker push public.ecr.aws/n7a3i1h9/ecr-tutorial:latest
The push process can last several minutes depending on your image size, internet speed, and system capacity. This example took less than one minute to complete.
![Pushing the Docker image to ECR](https://adamtheautomator.com/wp-content/uploads/2022/10/image-717.png)
8. Lastly, open your repository in ECR and confirm that you can see the image you pushed. Click on Copy URI and paste it into notepad or any note-taking program for later use.
![Copying the image URI](https://adamtheautomator.com/wp-content/uploads/2022/10/image-718.png)
Creating an ECS Cluster
So you’ve successfully created a repository and pushed an image. Before deploying this image in a container, you must create the ECS cluster. Creating a cluster will create the required resources in the background.
Follow the below steps to create a new cluster in this AWS ECS tutorial.
1. Navigate to the ECS page.
2. Click Clusters from the sidebar menu under the Amazon ECS heading.
![Open the Clusters page](https://adamtheautomator.com/wp-content/uploads/2022/10/image-719.png)
3. Click on Create Cluster to create a new cluster.
![Initiating the Cluster creation process](https://adamtheautomator.com/wp-content/uploads/2022/10/image-720.png)
4. Select EC2 Linux + Networking template, and click on the select button.
The EC2 Linux + Networking cluster is preferable for long-term production-level projects, while Networking only is suitable for small testing environments and short-term projects.
![Selecting the ECS cluster template](https://adamtheautomator.com/wp-content/uploads/2022/10/image-721.png)
5. Next, enter the Cluster name. This example uses learn-ecs-cluster as the cluster name.
![Specify the cluster name](https://adamtheautomator.com/wp-content/uploads/2022/10/image-722.png)
6. On the Provisioning Model, you have two options. The On-Demand Instance is the pay-as-you-go model, while the Spot model charges you a monthly fixed price. In this tutorial, choose the On-Demand Instanced model.
![Select the provisioning model](https://adamtheautomator.com/wp-content/uploads/2022/10/image-723.png)
7. On the EC2 instance type, select the EC2 size that the ECS will provision. This example chooses the t2.micro instance type.
Refer to Amazon EC2 Instance Types for the list of available EC2 instance types.
![Select the EC2 instance type](https://adamtheautomator.com/wp-content/uploads/2022/10/image-724.png)
8. Under Networking, specify the VPC, Subnets, and Security Group for this cluster’s EC2 instance. Also, select Enable next to the Auto assign public IP so that the cluster assigns a public IP address to your instance.
Ensure that the Security group you choose has rules that allow access to your application’s required port(s).
![Configure the instance networking](https://adamtheautomator.com/wp-content/uploads/2022/10/image-725.png)
9. Now you’ve filled in all the cluster configurations, click on the Create button to create the cluster.
![Creating the Cluster](https://adamtheautomator.com/wp-content/uploads/2022/10/image-726.png)
10. Once the cluster creation finishes, you will see a similar output to the one below. Click on the View Cluster button.
![ECS Cluster creation result](https://adamtheautomator.com/wp-content/uploads/2022/10/image-727.png)
Creating a Task Definition
Your next step in this AWS ECS tutorial is creating the Task definition. The task definition specifies the container information, such as the quantity of the containers running in the task and what resources the container will consume.
In essence, creating a task definition in AWS ECS is like creating a Docker Compose (
docker-compose.yml
) file on your computer.
1. Click on the Task definition from the sidebar menu and the Create new Task Definition button.
![Click Create new Task Definition](https://adamtheautomator.com/wp-content/uploads/2022/10/image-728.png)
2. Select EC2 under the Select launch type compatibility heading because the cluster template uses the EC2 template. Click Next step.
![Selecting the task launch type compatibility](https://adamtheautomator.com/wp-content/uploads/2022/10/image-729.png)
3. Enter the Task definition name. The name can be anything you want, but choose a name that is descriptive of its purpose.
![Naming the Task definition](https://adamtheautomator.com/wp-content/uploads/2022/10/image-730.png)
4. Under Task size, enter the adequate Task memory and Task CPU values based on your assessment of the task load. In this example, the values are 100 (MiB) and 1 vcpu (Unit), which are enough to run a basic web server.
![Configure the task size](https://adamtheautomator.com/wp-content/uploads/2022/10/image-731.png)
5. Now, create the container definitions, which include the image URI, port mapping, and the container name, among others. To do so, click Add container.
![Click Add container](https://adamtheautomator.com/wp-content/uploads/2022/10/image-732.png)
6. Next, enter the following information:
- Container name – This example uses the name learn-ecs-container.
- Image – This Docker image URI you copied earlier. This example will fetch the image at public.ecr.aws/n7a3i1h9/ecr-tutorial:latest.
- Port mappings – The host port and container port mapping. This example container will be accessible through the EC2 host’s port 8080, and the web server listens to port 80.
After configuring the container, click Add.
![Setup the container](https://adamtheautomator.com/wp-content/uploads/2022/10/image-733.png)
The new container definition appears on the list.
![Container definitions list](https://adamtheautomator.com/wp-content/uploads/2022/10/image-734.png)
7. Lastly, scroll down to the bottom of the page and click Create.
![Create the task definition](https://adamtheautomator.com/wp-content/uploads/2022/10/image-735.png)
You’ll see a confirmation banner indicating the task definition creation was successful.
![Task definition creation result](https://adamtheautomator.com/wp-content/uploads/2022/10/image-736.png)
Running the Docker Container in ECS
The next step in this AWS ECS tutorial is to test whether your image and container deployment works. To do so, you must run a new task using the task definition you created.
1. Click Clusters → cluster name (learn-ecs-cluster).
![Open the ECS cluster](https://adamtheautomator.com/wp-content/uploads/2022/10/image-737.png)
2. Click the Tasks tab and click on the Run New Task button.
![Running new Task](https://adamtheautomator.com/wp-content/uploads/2022/10/image-738.png)
3. On the Run Task page, select EC2 as the Launch type. Assuming that you only have one Task Definition and Cluster, those values will auto-populate. If not, make sure to select the correct ones.
![Configuring the task](https://adamtheautomator.com/wp-content/uploads/2022/10/image-739.png)
4. Scroll to the bottom of the page and click the Run Task button.
![Running the Task](https://adamtheautomator.com/wp-content/uploads/2022/10/image-740.png)
5. Once the task runs, click the ECS Instances tab and the ECS Instance link.
![Open the ECS instance](https://adamtheautomator.com/wp-content/uploads/2022/10/image-741.png)
6. Next, copy the Public IP address or Public DNS. Either of these two addresses is what you need to access the Docker web application container.
![Copy the ECS instance Public IP or DNS](https://adamtheautomator.com/wp-content/uploads/2022/10/image-742.png)
7. Open a browser window and navigate the ECS instance IP address or DNS. Remember to append the host’s port number that you specified in the container’s port mapping.
Copy the ECS instance Public IP or DNS
![Accessing the Docker container web server via the public IP address](https://adamtheautomator.com/wp-content/uploads/2022/10/image-743.png)
# Public DNS
HTTP://ec2-3-19-68-252.us-east-2.compute.amazonaws.com:8080
![Accessing the Docker container web server via the public DNS name](https://adamtheautomator.com/wp-content/uploads/2022/10/image-744.png)
Conclusion
This AWS ECS tutorial taught you how to create a repository and push a Docker image. After which, you created a new cluster and deployed the Docker container running a web server.
Did this tutorial spark your interest? Do you think you’ll use AWS ECS in your future project?