
From Docker to AWS ECS Fargate: Deploy Your First Containerized Application End-to-End
A practical, step-by-step guide to containerizing a Node.js application, publishing it to Amazon ECR, and deploying it on Amazon ECS using AWS Fargate.
AWS CB | DevOps Engineer
Introduction
Running an application inside a Docker container is only one part of the containerization journey. The next challenge is deploying that container somewhere it can run reliably without manually managing the underlying infrastructure.
This is where Amazon Elastic Container Registry (ECR), Amazon Elastic Container Service (ECS), and AWS Fargate come together.
In this hands-on tutorial, we'll take a simple Node.js application, package it into a Docker image, push that image to Amazon ECR, and deploy it using Amazon ECS with Fargate.
We'll use an Ubuntu EC2 instance as our temporary build environment and combine AWS CLI commands with the AWS Management Console to understand what's happening behind the scenes.
By the end, you'll have a running containerized application accessible through a public IP address.
What we'll build:
- A Node.js application packaged using Docker.
- A private Amazon ECR repository to store the image.
- An ECS task definition describing how the container should run.
- An ECS cluster and service using AWS Fargate.
- A publicly accessible application running on port 3000.
1. Understanding the architecture
Before deploying anything, let's understand the three main AWS services involved.
Amazon ECR
Amazon Elastic Container Registry is a managed container image registry.
It allows us to store and manage Docker images privately within AWS. Instead of pushing an image to Docker Hub, we'll push it to our own ECR repository.
Amazon ECS
Amazon Elastic Container Service is AWS's container orchestration service.
It manages container deployments and helps us define how many instances of an application should run, where they should run, and how they should be maintained.
AWS Fargate
AWS Fargate is a serverless compute engine for containers.
It allows ECS to run containers without requiring us to provision, configure, or manage the underlying container host EC2 instances.
This is the key distinction: we'll use an EC2 instance to build our Docker image, but ECS Fargate will run the actual application without us managing its host infrastructure.
Architecture
The workflow looks like this:

The responsibilities are straightforward:
Component| Responsibility
Docker| Builds the container image
Amazon ECR| Stores the container image
Amazon ECS| Orchestrates container deployment
ECS Task Definition| Describes the container configuration
ECS Service| Maintains the desired number of tasks
AWS Fargate| Runs containers without managing EC2 hosts
2. Understanding our application and Dockerfile
We'll use a simple Node.js application that displays information about the running container.
Our repository contains:
aws-ecs-demo/
│
├── .dockerignore
├── .gitignore
├── Dockerfile
├── README.md
├── app.js
├── package.json
└── package-lock.json
The application uses Express.js and listens on port 3000.
The application also displays runtime information such as the hostname, operating system, architecture, and memory statistics.
You can explore the application source here:
Understanding the Dockerfile
Here's the Dockerfile from the project:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY app.js ./
EXPOSE 3000
CMD ["node", "app.js"]
Let's understand the instructions.
- "FROM node:20-alpine": Uses a lightweight Node.js base image.
- "WORKDIR /app": Creates the working directory.
- "COPY package*.json ./": Copies package configuration files.
- "RUN npm ci --only=production": Installs production dependencies.
- "COPY app.js ./": Copies application source code.
- "EXPOSE 3000": Documents the application port.
- "CMD ["node", "app.js"]": Starts the Node.js application.
For a more current production-oriented Dockerfile, consider using "npm ci --omit=dev", running as a non-root user, and pinning the base image to a reviewed version or digest. We'll keep the original Dockerfile for consistency with the demonstration.
3. Launch an Ubuntu EC2 instance
Since I'm using a Windows machine for this demonstration, I'll use an Ubuntu EC2 instance as the build environment.
You can also perform these steps on a compatible local Linux machine.
Open the AWS Management Console and navigate to EC2.
Launch an instance with the following configuration:
Configuration| Value
Name| ecs-demo-build
AMI| Ubuntu Server
Instance type| Suitable small instance for Docker builds
Key pair| Create or select an existing key pair
Storage| Default storage is sufficient for this demo
Make sure you configure security groups appropriately.
For SSH access, restrict inbound port 22 to your own IP address. Do not expose SSH to everyone.
We don't need to expose port 3000 on this EC2 instance for the final ECS application. The application will run on Fargate.
Once the instance is running, connect to it using EC2 Instance Connect or SSH.
4. Install Docker and AWS CLI
First, update the Ubuntu package index:
sudo apt update
Install Docker and Git:
sudo apt install docker.io git unzip -y
Start and enable Docker:
sudo systemctl enable --now docker
To execute Docker commands without using sudo, add your Ubuntu user to the Docker group:
sudo usermod -aG docker $USER
Refresh the group membership:
newgrp docker
Verify Docker:
docker --version
docker ps
Note that membership in the Docker group effectively grants highly privileged access to the host. Only grant it to trusted users.
Install AWS CLI version 2
We'll use AWS CLI to interact with ECR.
Download the AWS CLI installer:
Extract the installer:
unzip awscliv2.zip
Install AWS CLI:
sudo ./aws/install
Verify the installation:
aws --version
We should now have Docker, Git, and AWS CLI available on our Ubuntu instance.
5. Configure IAM permissions for the EC2 build instance
This is one of the most important parts of the implementation.
Our EC2 instance needs permission to authenticate with Amazon ECR and push Docker images.
We should use an IAM role instead of creating long-lived access keys and storing them on the instance.
Create an IAM role

Open IAM in the AWS Management Console.
Follow these steps:
1. Navigate to Roles.
2. Select Create role.
3. Select AWS service as the trusted entity.
4. Choose EC2 as the use case.
5. Attach an appropriate ECR publishing policy.
6. Give the role a meaningful name, such as "EC2-ECR-Push-Role".
7. Create the role.
For a least-privilege implementation, the following is an example permissions policy.
Replace the account ID and region with your own values.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:CompleteLayerUpload",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart"
],
"Resource": "arn:aws:ecr:us-east-1:ACCOUNT_ID:repository/ecs-demo-app"
}
]
}
This policy allows authentication and image publishing to the specified ECR repository.
Next, attach the role to EC2.
1. Navigate to EC2 Instances.
2. Select your running instance.
3. Choose Actions.
4. Select Security.
5. Select Modify IAM role.
6. Select "EC2-ECR-Push-Role".
7. Save the changes.
Verify the identity from your EC2 terminal:
aws sts get-caller-identity
If configured correctly, AWS CLI will return the account and assumed-role identity.
We don't need to run "aws configure" with access keys.
Important: This role is for the build machine. The Fargate task will need its own execution role, which we'll configure later.
6. Clone the application repository
Now let's download our application source code.
Move into the project directory:
cd aws-ecs-demo
Check the files:
ls -la
You should see the Dockerfile, application source, and package configuration files.
7. Build and test the Docker image
Before pushing an image to AWS, we should build it and verify that the application starts correctly.
Build the Docker image:
docker build -t ecs-demo-app .
Check the generated image:
docker images
Run the application locally inside a container:
docker run -d \
-p 3000:3000 \
--name demo-app \
ecs-demo-app
Check whether the container is running:
docker ps
Test the application:
You should receive the application's HTML response.
If you're testing from another machine, make sure the appropriate network access is configured.
If the application doesn't start, inspect its logs:
docker logs demo-app
Once the local test succeeds, stop and remove the test container:
docker rm -f demo-app
At this point, our Docker image is ready to be published.
8. Create an Amazon ECR repository

Now we'll create a private repository in Amazon ECR.
First, define the environment variables:
export AWS_REGION="us-east-1"
export ACCOUNT_ID=$(aws sts get-caller-identity \
--query Account \
--output text)
export REPO_NAME="ecs-demo-app"
Verify the values:
echo $AWS_REGION
echo $ACCOUNT_ID
echo $REPO_NAME
Authenticate Docker with Amazon ECR
Amazon ECR provides a temporary authentication token that we can pass to Docker.
Run:
aws ecr get-login-password \
--region $AWS_REGION \
| docker login \
--username AWS \
--password-stdin \
$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
A successful authentication returns:
Login Succeeded
Create the repository
Run:
aws ecr create-repository \
--repository-name $REPO_NAME \
--region $AWS_REGION
AWS will return repository details, including its URI.
You can also verify the repository:
aws ecr describe-repositories \
--repository-names $REPO_NAME \
--region $AWS_REGION
The repository should now appear in the Amazon ECR console.
A practical lesson: Repository names must be unique within an AWS account and region. During my original implementation, I encountered a repository-already-exists error and resolved it by choosing a different name.
For repeatable scripts, you can check whether a repository already exists before creating it.
9. Tag and push the Docker image to ECR
We now have:
- A running Docker environment.
- A built Docker image.
- An ECR repository.
- IAM permissions to publish images.
Let's connect these components.
Define the image URI
export ECR_URI="$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO_NAME"
Tag the image
For this initial demonstration:
docker tag ecs-demo-app:latest \
$ECR_URI:latest
For better version tracking, also create a versioned tag:
export IMAGE_TAG="v1"
docker tag ecs-demo-app:latest \
$ECR_URI:$IMAGE_TAG
Push the images
docker push $ECR_URI:$IMAGE_TAG
You can also push the latest tag:
docker push $ECR_URI:latest
Verify the image:
aws ecr describe-images \
--repository-name $REPO_NAME \
--region $AWS_REGION
You should now see the image tags in ECR.
This is an important milestone: our application is no longer just a local Docker image. It is now stored in a private AWS container registry and can be pulled by ECS.
10. Create an ECS task definition

Now comes the deployment part.
A task definition is a blueprint describing how ECS should run our container.
It includes information such as:
- Container image URI.
- CPU and memory allocation.
- Port mappings.
- IAM roles.
- Logging configuration.
- Environment variables.
Open the AWS Management Console and navigate to Amazon ECS.
Select Task Definitions and choose Create new task definition.
Use the following configuration:
Setting| Value
Task definition family| ecs-demo-task
Launch infrastructure| AWS Fargate
OS| Linux
Architecture| x86_64
CPU| 0.25 vCPU
Memory| 0.5 GB
Container name| demo-container
Image URI| Your ECR image URI
Container port| 3000
Protocol| TCP
For the image URI, use:
ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/ecs-demo-app:v1
Select the appropriate task execution role.
Understanding the ECS task execution role
This is different from the EC2 IAM role we created earlier.
The task execution role gives the ECS/Fargate infrastructure permission to perform operations such as:
- Pulling images from private ECR repositories.
- Sending container logs to CloudWatch Logs when configured.
- Retrieving supported secrets during task initialization.
For a basic implementation, AWS provides the managed policy:
"AmazonECSTaskExecutionRolePolicy"
Create or select a role with the ECS task trust relationship and attach this policy.
If your application itself needs to access AWS services, configure a separate task role with only those application permissions.
Our sample Node.js application does not need AWS API access, so a separate application task role is not necessary for this demo.
You can read more about the distinction in the "AWS ECS IAM documentation" ( https://docs.aws.amazon.com/AmazonECS/latest/developerguide/security-iam-roles.html ).
Once the task definition configuration is complete, create it.
11. Create an ECS cluster
An ECS cluster is a logical grouping of tasks and services.

For our demonstration, we'll create a cluster that runs Fargate workloads.
In the ECS console:
1. Navigate to Clusters.
2. Select Create cluster.
3. Enter the name "ecs-demo-cluster".
4. Choose the Fargate-compatible cluster configuration.
5. Create the cluster.
Wait for the cluster to become available.
We now have an ECS cluster ready to run our application.
12. Create an ECS service using Fargate
An ECS service helps maintain the desired number of running tasks.
For example, if we configure one desired task and that task unexpectedly stops, ECS can attempt to launch a replacement.

Navigate to your newly created cluster.
Under Services, choose Create.
Configure the service:
Setting| Value
Compute option| Fargate
Task definition| ecs-demo-task
Service name| ecs-demo-service
Desired tasks| 1
Launch type / capacity provider| Fargate
The desired task count is one for this demonstration.

You can increase it depending on application requirements.
Configure networking
This is a particularly important part of an ECS Fargate deployment.
Fargate tasks use the "awsvpc" network mode. Each task receives its own elastic network interface.
For this demonstration, select:
- Your default VPC.
- Public subnets.
- Auto-assign public IP enabled.
- A security group that permits the required application traffic.
Create an inbound security group rule:
Setting| Value
Type| Custom TCP
Port| 3000
Source| Your public IP address
For a publicly accessible demo, you can temporarily use "0.0.0.0/0", but this exposes the application port to the internet. Restricting access to your own IP is safer.
The subnet must have a route to an internet gateway for the public-IP approach.
Why is this important?
Fargate needs network connectivity to retrieve the image from ECR. AWS supports public subnet access with a public IP, private subnet access through NAT, or appropriate VPC endpoints.
For this tutorial, we'll use a public subnet and an automatically assigned public IP.
Read the "AWS Fargate networking documentation" ( https://docs.aws.amazon.com/AmazonECS/latest/developerguide/fargate-task-networking.html ) for alternative networking architectures.
After verifying the configuration, create the service.
ECS will now schedule the task and Fargate will provision the required compute infrastructure.
13. Verify the running application
Once the service deployment begins, open the ECS cluster and navigate to its Tasks tab.
You should see the task progressing through its lifecycle.
Wait until the task reaches:
RUNNING
Check the task details.
Look for:
- Task status.
- Container status.
- Task definition revision.
- Network configuration.
- Public IP address.
Copy the task's public IP address.
Open your browser:
http://<FARGATE_PUBLIC_IP>:3000
Alternatively, test it from your terminal:
curl http://<FARGATE_PUBLIC_IP>:3000
You should see the Node.js application running.

Congratulations! We've successfully deployed our Dockerized application on AWS ECS using Fargate.
Understanding what happened behind the scenes
Let's recap the complete deployment:
1. Docker packaged our Node.js application into an image.
2. We authenticated Docker with Amazon ECR.
3. We pushed the image to a private ECR repository.
4. We created an ECS task definition referencing that image.
5. We created an ECS cluster.
6. We created an ECS service with one desired task.
7. Fargate provisioned the compute environment.
8. The task pulled the image from ECR and started the application.
9. We accessed the application through its public IP.
The most important takeaway is that we never had to provision or manage the EC2 container host for the ECS application itself.
14. Troubleshooting common ECS Fargate issues
Container deployments don't always work on the first attempt.
During my implementation, I also encountered configuration issues, including an existing ECR repository and a task that stopped during deployment.
Here are some problems worth understanding.
Problem| Possible cause| What to check
Docker permission denied| User lacks Docker group access| Group membership and Docker daemon
ECR login failure| Missing IAM permissions or incorrect region| EC2 role and AWS identity
Repository already exists| Repository name is already used| Existing ECR repositories
"exec format error"| Image architecture mismatch| Image architecture and Fargate architecture
Task stops unexpectedly| Image pull, startup, resource or configuration failure| Stopped reason and container exit code
Image pull failure| Missing execution role permissions or network connectivity| Execution role, ECR permissions and networking
Connection timeout| Incorrect security group or network route| Port 3000 and subnet routing
Application not responding| Container application isn't listening correctly| Container logs and application port
How to investigate stopped tasks
Don't assume that a task has failed because the service is taking time to stabilize.
Inspect its stopped reason in the ECS console.
You can also use:
aws ecs describe-tasks \
--cluster ecs-demo-cluster \
--tasks TASK_ARN \
--region us-east-1
Look at:
- "stopCode"
- "stoppedReason"
- Container "reason"
- Container "exitCode"
These fields help identify the actual problem.
Check ECS service events
Service events are useful when ECS cannot maintain the desired number of tasks.
aws ecs describe-services \
--cluster ecs-demo-cluster \
--services ecs-demo-service \
--region us-east-1
Review the events returned by the command.
Check application logs
If you configure the "awslogs" log driver in your task definition, you can inspect application logs through CloudWatch Logs.
Make sure the task execution role has the necessary logging permissions.
This is especially useful when a container starts and then exits because of an application-level error.
15. Security improvements before production
The implementation we've built is a learning-oriented deployment, not a complete production architecture.
Here are some changes I would make before using a similar pattern for a real application.
1. Follow least-privilege IAM
Avoid attaching "AdministratorAccess" to EC2 or application workloads.
Use separate roles for:
- EC2 image publishing.
- ECS task execution.
- Application access to AWS services.
2. Avoid unrestricted public access
For a production application, consider placing tasks in private subnets and exposing the application through an Application Load Balancer.
3. Use immutable image tags
Avoid depending entirely on the "latest" tag.
Versioned tags help identify exactly which image is deployed and simplify rollback.
4. Configure health checks
Add container health checks and appropriate service health monitoring.
5. Enable centralized logging
Use CloudWatch Logs to collect application output and make troubleshooting easier.
6. Protect secrets
Never bake credentials or sensitive configuration into Docker images.
Use AWS Secrets Manager or Systems Manager Parameter Store where appropriate.
7. Review container images
Use image scanning and maintain updated base images and dependencies.
These improvements turn a basic container deployment into a more maintainable engineering solution.
16. Understanding AWS costs
AWS Fargate removes the responsibility of managing container hosts, but it is not free compute.
You should understand the resources that can generate charges.
Resource| Cost consideration
EC2 build instance| Instance runtime and storage
ECS Fargate| Requested vCPU and memory over task runtime
Amazon ECR| Stored image data
Public IPv4| Public IPv4 address charges
NAT Gateway| Hourly and data processing charges, if used
Application Load Balancer| Hourly and capacity-based charges, if used
CloudWatch Logs| Log ingestion and retention
For this demonstration, we use one Fargate task with 0.25 vCPU and 0.5 GB memory.
Actual costs depend on region, runtime, storage, networking and other resources.
Before deploying, review the "AWS Fargate pricing page" ( https://aws.amazon.com/fargate/pricing/ ) and "Amazon ECR pricing" ( https://aws.amazon.com/ecr/pricing/ ).
Remember that deleting the ECS service alone does not automatically remove every resource created during the tutorial.
17. Clean up AWS resources
This is an important step, particularly when working in a personal AWS account.
Leaving unused resources running can result in unexpected charges.
Follow this cleanup sequence.
Step 1: Delete the ECS service
Open your ECS cluster.
Navigate to Services, select your service, and delete it.
Wait for the running tasks to stop.
Step 2: Delete the ECS cluster
Once the service is removed, delete the cluster.
Step 3: Deregister the task definition
Navigate to Task Definitions.
Find "ecs-demo-task" and deregister its revisions when they are no longer in use.
Step 4: Delete the ECR repository
If you no longer need the image, run:
aws ecr delete-repository \
--repository-name ecs-demo-app \
--force \
--region us-east-1
The "--force" option allows deletion of the repository along with its images.
Step 5: Terminate the EC2 build instance
If the EC2 instance was created only for this tutorial, terminate it.
Step 6: Review IAM and networking resources
Remove any demonstration-specific IAM role and policies after detaching them from their resources.
Review unused security groups and other resources created for the demonstration before deleting them.
Finally, check your AWS Billing dashboard to verify that you haven't left unnecessary resources behind.
18. What we learned
In this tutorial, we went through the complete process of deploying a Dockerized Node.js application on AWS ECS Fargate.
We covered:
- Preparing an Ubuntu EC2 build environment.
- Installing Docker and AWS CLI.
- Configuring IAM roles.
- Building and testing Docker images.
- Creating an Amazon ECR repository.
- Pushing images to ECR.
- Creating an ECS task definition.
- Creating an ECS cluster and service.
- Deploying containers using Fargate.
- Configuring networking and application access.
- Verifying deployment and troubleshooting issues.
- Understanding security, costs and resource cleanup.
The key learning is that containerization doesn't end with building a Docker image. We also need to understand image registries, orchestration, networking, IAM and operational considerations.
Amazon ECS and AWS Fargate make it possible to run containerized workloads without managing the underlying container host infrastructure.
19. Take it further
Once you've successfully completed this implementation, here are some ideas to extend it:
1. Put an Application Load Balancer in front of your ECS service.
2. Use private subnets for your Fargate tasks.
3. Automate image publishing with GitHub Actions.
4. Authenticate GitHub Actions to AWS using OIDC rather than long-lived access keys.
5. Manage ECS infrastructure using Terraform.
6. Configure autoscaling for your ECS service.
7. Add CloudWatch monitoring and alarms.
8. Implement automated deployments and rollback strategies.
These are natural next steps towards a more production-oriented container deployment workflow.
Conclusion
We started with a simple Node.js application and ended with a running containerized application on AWS ECS Fargate.
We used Docker to package the application, Amazon ECR to store the image, Amazon ECS to orchestrate the deployment, and AWS Fargate to run the container without managing EC2 container hosts.
The best way to understand cloud infrastructure is to build it, test it, troubleshoot it, and clean it up.
I hope this hands-on guide helps you deploy your first containerized application on AWS.
Resources
- "Complete GitHub repository and implementation" ( https://github.com/Amitabh-DevOps/aws-ecs-demo )
- "Amazon ECS documentation" ( https://docs.aws.amazon.com/ecs/ )
- "Amazon ECR documentation" ( https://docs.aws.amazon.com/ecr/ )
- "AWS Fargate documentation" ( https://docs.aws.amazon.com/AmazonECS/latest/userguide/AWS\_Fargate.html )
- "ECS IAM best practices" ( https://docs.aws.amazon.com/AmazonECS/latest/developerguide/security-iam-roles.html )
Have questions or encountered an issue? Share your experience in the comments. I'd be happy to discuss your implementation and troubleshooting approach.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article