AWS Builder Center

How to Deploy Docker Containers on AWS: A Step-by-Step Guide

Deploying Docker containers to Amazon Web Services (AWS) is one of the most reliable ways to build, run, and scale modern applications. AWS offers several options for running containerized workloads, ranging from fully managed serverless platforms to robust, enterprise-grade orchestrators.

i'm Yogesh and i'm interested in learning cloud and devops
Deploying Docker containers to Amazon Web Services (AWS) is one of the most reliable ways to build, run, and scale modern applications. AWS offers several options for running containerized workloads, ranging from fully managed serverless platforms to robust, enterprise-grade orchestrators.

1. Key Deployment Options on AWS

Before diving into deployment steps, choose the AWS compute service that best fits your requirements:
  • AWS App Runner: A fully managed, beginner-friendly service for web applications and APIs. Point it to a container image or repository, and AWS handles provisioning, load balancing, and auto-scaling.
  • Amazon ECS (Elastic Container Service): AWS’s native container management service. It integrates seamlessly with other AWS services and can run on AWS Fargate (serverless) or managed Amazon EC2 instances.
  • Amazon EKS (Elastic Kubernetes Service): Managed Kubernetes on AWS. Ideal if you already use Kubernetes manifests, Helm, or require multi-cloud container orchestration portability.

2. Prerequisites

To follow the deployment steps, ensure you have:
  1. An active AWS Account and configured AWS CLI.
  2. Docker Desktop installed and running locally.
  3. A sample Docker application with a working Dockerfile.

3. Step-by-Step Deployment via AWS ECR & Amazon ECS (Fargate)

The standard and most versatile production setup uses Amazon ECR (Elastic Container Registry) to store images and Amazon ECS with AWS Fargate for serverless container execution.

Step 1: Create an Amazon ECR Repository

Amazon ECR stores your container images securely.
  1. Log in to the AWS Management Console and search for Elastic Container Registry.
  2. Click Create repository.
  3. Set the repository visibility to Private and enter a name (e.g., my-web-app).
  4. Click Create repository.

Step 2: Push Your Docker Image to Amazon ECR

In your local terminal, authenticate Docker to your ECR registry and push your image:
1
2
3
4
5
6
7
8
9
10
11
# Retrieve login password and authenticate Docker to your ECR registry
aws ecr get-login-password --region <YOUR_REGION> | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<YOUR_REGION>.amazonaws.com

# Build your local Docker image
docker build -t my-web-app .

# Tag your image for Amazon ECR
docker tag my-web-app:latest <AWS_ACCOUNT_ID>.dkr.ecr.<YOUR_REGION>.amazonaws.com/my-web-app:latest

# Push the image to Amazon ECR
docker push <AWS_ACCOUNT_ID>.dkr.ecr.<YOUR_REGION>.amazonaws.com/my-web-app:latest

Step 3: Create an ECS Cluster

  1. Open the Amazon ECS console and click Create cluster.
  2. Name your cluster (e.g., production-cluster).
  3. Select AWS Fargate (Serverless) as the infrastructure type.
  4. Click Create.

Step 4: Create a Task Definition

A Task Definition is the blueprint that tells ECS which container image to pull, how much CPU/memory to allocate, and which ports to expose.
  1. In the ECS console, navigate to Task Definitions and click Create new task definition.
  2. Enter a family name (e.g., my-app-task).
  3. Under Infrastructure requirements, select AWS Fargate.
  4. Set container details:
  • Container Name: app-container
  • Image URI: Paste your ECR Image URI copied from Step 1 (e.g., <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>[.amazonaws.com/my-web-app:latest](https://.amazonaws.com/my-web-app:latest)).
  • Port mappings: Expose port 80 or 8080 (matching your app's internal port).
  1. Click Create.

Step 5: Launch the Container Task or Service

  1. Return to your ECS Cluster, navigate to the Tasks tab, and click Run new task.
  2. Choose Fargate as the launch type.
  3. Select my-app-task as the Task Definition family.
  4. Under Network Configuration, choose your VPC, Subnets, and an active Security Group that allows inbound HTTP traffic on port 80.
  5. Click Create.
Once the task status changes to RUNNING, click on the task details to locate its Public IP address. Open the IP in your web browser to view your live containerized application!

4. Best Practices for Production Deployments

  • Automate with CI/CD: Set up AWS CodePipeline or GitHub Actions to automatically build and push new Docker images on every git commit.
  • Implement Least Privilege: Use AWS IAM roles for ECS tasks (ecsTaskExecutionRole) so containers only access necessary AWS resources.
  • Manage Secrets Safely: Pass environment variables and database passwords through AWS Secrets Manager or Systems Manager Parameter Store instead of hardcoding them into Dockerfiles.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article