How to build a Docker image out of the ASP.NET Core application and host its container on AWS EC2 instance

Table of contents:

  1. Create .NET Core application
  2. Building the Docker image
  3. Host the container on AWS EC2
  • .NET Core SDK (I am using version .NET 5.0)
  • Docker Desktop
  • Docker Hub account
  • AWS account
Photo by Jerry Zhang on Unsplash

Create .NET Core application

First things first open your VS Code editor. Toggle your terminal with ‘Ctrl+`'. Check if you have .NET Core SDK and Docker on your computer by running the following commands:

dotnet --versiondocker --version
version check
version check
.NET SDK and Docker is present
dotnet new webapi -n BasicAPI
code .\BasicAPI\
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
Configure method
Configure method
Simplified Configure method
dotnet run
The app is running on localhost
The app is running on localhost
Application is successfully running on localhost’s 5000 port
http://localhost:5000/WeatherForecast
Weather API data
WeatherForecastController - Controller = WeatherForecast
Weather controller
Weather controller
Weather API controller

Building the Docker image

In the main directory of our application let’s create a new file called Dockerfile. The file does not have an extension. Add the following code to the Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-image
WORKDIR /src
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
EXPOSE 80
COPY --from=build-image /src/out ./
ENTRYPOINT ["dotnet", "BasicAPI.dll"]
Dockerfile commands

Dockerfile explanation:

Line 1: sets the Base image to our build process and names it ‘build-image’. Using this image we will build our ASP.NET Core project.

.dockerignore

To keep the image as light as possible we are not going to copy /bin and /obj folders while building the project (line 7 and line 8 in Dockerfile). These folders are going to be created anyway because of “dotnet restore” and “dotnet publish” commands.

bin\
obj\
.dockerignore file
.dockerignore file
.dockerignore file

Build the image

Let’s build the image out of our ASP.NET Core project. Run the following command:

docker build -t <your_docker_hub_ID>/basicapi .
<your docker hub ID>/<project name>:<version>
fedora/httpd:version1.0
docker images
docker run -p 80:80 <your_docker_hub_ID>/basicapi
Weather data
Weather data
Weather data from our container

Push the image to the Docker Hub

First, log in to your Docker Hub account via the terminal in VS Code. Run the command:

docker login --username=<yourhubusername>
docker push <your_docker_hub_ID>/basicapi

Host the container on AWS EC2

First of all, login into your AWS account console. In the search field type “EC2”. Click the “EC2” selection.

EC2 search in AWS
EC2 search in AWS
Searching for EC2
Linux 2 instance
Linux 2 instance
Amazon Linux 2 AMI
Pick the “Free” instance
Pick the “Free” instance
Free tier eligible instance
Edit Security Group
Edit Security Group
Add security rule
Add security rule
Add new security rule
Connect to the instance
Connect to the instance
The instance is running, let’s connect.
sudo yum update -y
Update command
Update command
Update command
sudo amazon-linux-extras install docker
Accept
Accept
Type “y” and click Enter
sudo service docker start
sudo docker run -p 80:80 <your docker hub ID>/basicapi
Successfully running container
Successfully running container
Successfully running container
http://ec2-54-174-163-85.compute-1.amazonaws.com/WeatherForecast
Instance Public Domain Name
Instance Public Domain Name
Locate your instance Public DNS (IPv4)
Container running on AWS
Container running on AWS
Our ASP.NET Core app is running in the container on AWS
Terminate the instance
Terminate the instance
Terminate the instance

Wrapping up

Congrats 👏👏👏 on this tutorial:

  • We built the Docker image out of our ASP.NET Core project.
  • We run the container with our image locally.
  • We deployed the container with our image on the AWS EC2 instance.

I am passionate about software development :)