Introduction
Containerization has become one of the most popular approaches for deploying modern applications. Docker allows developers to package an application along with its dependencies into a portable container image, making it easier to deploy across different environments.
Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service that makes it easy to run, manage, and scale containerized applications on AWS.
In this article, we will learn how to deploy a .NET application to AWS ECS using AWS Fargate and a Docker Hub image, without pushing the image to Amazon Elastic Container Registry (ECR).
Typically, AWS ECS deployments use Amazon ECR to store Docker images. However, ECS also supports pulling container images directly from public and private Docker Hub repositories.
We will use Docker Hub as our container image repository and AWS Fargate to run the application without managing EC2 instances.
What Are We Going to Build?
- Create a Docker image of a .NET application.
- Push the Docker image to Docker Hub.
- Create an Amazon ECS cluster.
- Create an ECS task definition using the Docker Hub image URL.
- Configure AWS Fargate as the launch type.
- Create an ECS service.
- Deploy and run the .NET application on AWS.
- Verify that the application is running successfully.
Step 1: Create a Docker Image of the .NET Application
First, we need to containerize our .NET application using Docker.
Navigate to your .NET application directory and create a file named Dockerfile.
For this example, we will use a multi-stage Docker build.
Docker File
# Build stage
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
# Copy project files
COPY . .
# Restore dependencies
RUN dotnet restore
# Publish application
RUN dotnet publish -c Release -o /app/publish
# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final
WORKDIR /app
COPY --from=build /app/publish .
EXPOSE 8080
ENTRYPOINT ["dotnet", "YourApplication.dll"]
Note: Replace YourApplication.dll with the actual DLL name of your .NET application. The Docker SDK and runtime versions should match your application's target framework. Adjust the build commands if your solution contains multiple projects.
Build the Docker Image
docker build -t sunnys-dotnet-app:latest . Step 2: Push the Docker Image to Docker Hub
Your Docker image URL will look like this:
docker.io/YOUR_DOCKERHUB_USERNAME/sunnys-dotnet-app:latest
Important: For production deployments, prefer a versioned image tag instead of relying only on latest. Also ensure that the image architecture is compatible with your selected Fargate CPU architecture.
Step 3: Create an Amazon ECS Cluster
Now let's create an ECS cluster where our .NET application will run.
- Log in to the AWS Management Console.
- Search for Amazon Elastic Container Service (ECS).
- Navigate to the Clusters section.
- Click Create cluster.
- Enter a cluster name.
For this example, we will use:
sunnys-cluster
- Select the appropriate infrastructure configuration for AWS Fargate.
- Configure any additional monitoring or settings required.
- Click Create.
What is an ECS Cluster?
An ECS cluster is a logical grouping of tasks and services. It provides an environment in which containerized applications can run.
Since we are using AWS Fargate, we do not need to provision or manage EC2 instances.
Step 4: Create an ECS Task Definition Using the Docker Hub Image
An ECS task definition is a blueprint that describes how our container should run.
It contains information such as:
- Docker image URL.
- CPU and memory requirements.
- Container port mappings.
- Environment variables.
- IAM roles.
- Logging configuration.
- Network requirements.
Let's create a task definition.
4.1 Navigate to Task Definitions
- Open Amazon ECS.
- Select Task definitions from the left navigation.
- Click Create new task definition.
- Enter the task definition family name.
For example:
sunnys-task-definition 4.2 Configure Infrastructure Requirements
Select:
Launch type: AWS Fargate
Choose the required CPU and memory configuration.
For this example, we can start with:
CPU: 0.25 vCPU
Memory: 0.5 GB
These are example values and should be adjusted according to your application's resource requirements.
4.3 Configure the Container
Under the container configuration, provide the following details:
Configuration Value Container name sunnys-dotnet-container Image URI YOUR_DOCKERHUB_USERNAME/sunnys-dotnet-app:latest Container port 8080 Protocol TCP Essential container Yes
The most important configuration is the Image URI.
Instead of using an Amazon ECR image URL, we will directly provide the Docker Hub image URL.
For example:
docker.io/YOUR_DOCKERHUB_USERNAME/sunnys-dotnet-app:latest
This allows ECS to pull the image directly from Docker Hub.
4.4 Create the Task Definition
Review all the configurations and click Create.
Our ECS task definition is now ready.
Step 5: Configure AWS Fargate as the Launch Type
AWS Fargate is a serverless compute engine for containers that works with Amazon ECS.
It eliminates the need to provision, configure, patch, and maintain EC2 instances.
For our deployment, we will use Fargate as the compute infrastructure.
The important configurations are:
Launch type: FARGATE
Network mode: awsvpc
Operating system: Linux
CPU architecture: X86_64The selected CPU architecture must match the architecture of the Docker image.
With Fargate, AWS manages the underlying infrastructure, while we focus on deploying and managing our application.
Step 6: Create an ECS Service
An ECS service helps maintain the desired number of running tasks in an ECS cluster.
For example, if we configure a service with two desired tasks, ECS attempts to maintain two running instances of our application.
Let's create the service.
6.1 Navigate to the ECS Cluster
- Open Amazon ECS.
- Select Clusters.
- Open the cluster we created earlier.
sunnys-cluster - Navigate to the Services tab.
- Click Create.
6.2 Configure Service Details
Provide the following configuration:
| Configuration | Value |
|---|---|
| Compute options | Launch type |
| Launch type | FARGATE |
| Application type | Service |
| Task definition family | sunnys-task-definition |
| Service name | sunnys-task-definition-service |
| Desired tasks | 2 |
Select the latest revision of your task definition.
Step 7: Deploy and Run the .NET Application on AWS
Once the ECS service is created, AWS Fargate starts provisioning the required infrastructure and launching the containers.
The deployment process includes:
- ECS schedules the required tasks.
- Fargate provisions the compute resources.
- The container image is pulled from Docker Hub.
- The container is started.
- The .NET application starts running inside the container.
- ECS monitors the task health and maintains the desired number of tasks.
Step 8: Verify the Deployment
After the service deployment is complete, verify that the .NET application is running successfully.
8.1 Check ECS Cluster Status
Open your ECS cluster and check the cluster overview.
You should be able to see:
- Cluster status.
- Active services.
- Running tasks.
- Pending tasks.
8.2 Check ECS Service Status
Navigate to the Services tab.
Verify that the service status is Active
8.3 Check Running Tasks
Open the Tasks tab and verify that the desired tasks are running.
You can also inspect the container details and application logs.
8.4 Access the Application
If you configured a public IP or Application Load Balancer, access the application using the corresponding public endpoint.
Make sure the endpoint, listener, target group, and security group configurations match your application.
Step 9: Verify the Deployment in the AWS Console
After completing the deployment, we can verify the ECS cluster and service from the AWS Management Console.
In our deployment, the ECS cluster is named:
sunnys-cluster
The cluster overview shows:
- Cluster status: Active
- Services: 1 active, 0 draining
- Tasks: 2 running, 0 pending
- Infrastructure: AWS Fargate
The Services tab also displays our deployed ECS service with an Active status.
This confirms that the ECS service has been created and the two desired tasks are running.




No comments:
Post a Comment