how-to-deploy-docker-containers-on-aws-using-ecs.html

How to Deploy Docker Containers on AWS Using ECS

In today’s cloud-native world, containerization has emerged as a powerful way to package and deploy applications. Docker, a leading platform for containerization, allows developers to build, ship, and run applications in isolated environments. When combined with Amazon Web Services (AWS) Elastic Container Service (ECS), you can effectively manage and scale your containerized applications. In this article, we'll explore how to deploy Docker containers on AWS using ECS, providing step-by-step instructions and code examples to help you get started.

What is AWS ECS?

Amazon Elastic Container Service (ECS) is a fully managed container orchestration service that makes it easy to run, scale, and secure Docker containers on AWS. ECS abstracts the complexity of managing the underlying infrastructure, allowing developers to focus on building applications. With ECS, you can define your application using task definitions, which specify the Docker images to use, the resources allocated, and other configuration details.

Why Use AWS ECS?

  • Scalability: Automatically scale your applications based on demand.
  • Integration: Seamlessly integrates with other AWS services like RDS, ELB, and CloudWatch.
  • Cost-Effective: Pay only for the resources you use.
  • Security: Leverage AWS’s security features to protect your applications.

Prerequisites

Before we begin, ensure you have the following:

  1. AWS Account: Sign up for an AWS account if you don’t have one.
  2. AWS CLI: Install and configure the AWS Command Line Interface (CLI) on your machine.
  3. Docker: Install Docker on your local machine to build container images.

Step 1: Create a Docker Image

First, we need to create a Docker image for the application. Here’s a simple Node.js application as an example.

Sample Application Code

Create a new directory and add the following files:

app.js

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

Dockerfile

# Use the official Node.js image
FROM node:14

# Set the working directory
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the application code
COPY . .

# Expose the port
EXPOSE 3000

# Start the application
CMD ["node", "app.js"]

Build the Docker Image

Navigate to your project directory and run the following command to build the image:

docker build -t my-node-app .

Step 2: Push the Docker Image to Amazon ECR

Before deploying to ECS, you need to store your Docker image in Amazon Elastic Container Registry (ECR).

Create an ECR Repository

  1. Open the AWS Management Console, navigate to ECR, and create a new repository named my-node-app.
  2. Note the URI of the repository for later use.

Authenticate Docker to Your ECR

Run the following command to authenticate Docker with your AWS account:

aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin <your_account_id>.dkr.ecr.us-west-2.amazonaws.com

Tag and Push the Image

Tag your image with the ECR URI and push it:

docker tag my-node-app:latest <your_account_id>.dkr.ecr.us-west-2.amazonaws.com/my-node-app:latest
docker push <your_account_id>.dkr.ecr.us-west-2.amazonaws.com/my-node-app:latest

Step 3: Create an ECS Cluster

Now that your image is in ECR, you can create an ECS cluster.

  1. Go to the ECS console and click on “Clusters.”
  2. Click “Create Cluster” and choose the “EC2 Linux + Networking” template.
  3. Configure the cluster settings and create your cluster.

Step 4: Create a Task Definition

A task definition is a blueprint for your application. Here’s how to create one:

  1. In the ECS console, navigate to “Task Definitions” and click “Create new Task Definition.”
  2. Choose the launch type as “EC2” and click “Next.”
  3. Fill out the required fields, including:
  4. Task Definition Name: my-node-app
  5. Container Definitions: Click “Add container” and fill in:

    • Container name: my-node-app
    • Image: <your_account_id>.dkr.ecr.us-west-2.amazonaws.com/my-node-app:latest
    • Memory Limits: Set as needed (e.g., 512 MiB).
    • Port Mappings: Set 3000 for both container port and host port.
  6. Click “Create” to save the task definition.

Step 5: Run the Task

To run your application:

  1. Go back to your ECS cluster.
  2. Click on the “Tasks” tab and then “Run new Task.”
  3. Select your task definition and configure the number of tasks.
  4. Click “Run Task.”

Step 6: Access Your Application

Once your task is running, you can access your application via the public IP address of the EC2 instance. Navigate to http://<ec2_public_ip>:3000 in your browser, and you should see "Hello, World!"

Troubleshooting Tips

  • Task fails to start: Check the logs in the ECS console under “Logs” to diagnose issues.
  • Application not reachable: Ensure your EC2 instance's security group allows inbound traffic on port 3000.

Conclusion

Deploying Docker containers on AWS using ECS simplifies the management and scaling of your applications. By following the steps outlined in this guide, you can quickly get your containerized applications up and running in the cloud. With AWS ECS, you can focus on building great software while AWS handles the operational overhead. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.