Getting Started with AWS

Deploy a .NET Web Application on Amazon ECS powered by Graviton2

Module 3: Create and Publish Docker Image

In this module, you will build and publish Docker image of a sample application

Overview

In previous modules, you launched an AWS CloudFormation stack, which created a new AWS VPC, Public and Private subnet pairs in 2 Availability Zones, 1 AWS RDS Aurora instance in a Private subnet, and also initialized an AWS Cloud9 environment to issue AWS Command Line Interface(CLI) commands via command line.

You also cloned a sample .NET application from Github on the Cloud9 Bash Terminal, and built a .NET package. 

This module gives step-by-step instructions for building and publishing Sample Application.

What You Will Learn

  • You will learn how to write a Dockerfile, with docker commands to build a container image, specifically for ARM64 based instances
  • You will be using AWS Cloud9 - command line interface, to run AWS CLI commands.

Key Concepts

  • Dockerfile - A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Docker can build images automatically by reading the instructions from a Dockerfile.
  • AWS CLI - The AWS Command Line Interface is a tool to configure and manage AWS Services via command line.
  • Amazon ECR - Amazon Elastic Container Registry (ECR) is a fully managed container registry that makes it easy to store, manage, share, and deploy your container images and artifacts anywhere. 
  • AWS Cloud9 - AWS Cloud9 is a cloud-based integrated development environment (IDE) that lets you write, run, and debug your code with just a browser. It includes a code editor, debugger, and terminal. 

 Time to Complete

10 minutes

 Services Used

Implementation

  • Set up Environment Variables

    1. Open the new AWS Cloud9 terminal window as per the instructions listed in Module 2.

    2. Configure the AWS CLI with your current region as default (you can retrieve the region you chose from instance metadata).

    export AWS_REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/region)
    aws configure set default.region ${AWS_REGION}
    aws configure get default.region

    3. Set up environment variable for AWS account id, for later reuse.

    export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
    echo $AWS_ACCOUNT_ID

    In subsequent steps you will be referring these values, so storing them in the environment variables takes away the need of copying and pasting the values, or writing long command line instructions.

  • Create ECR Repository

    1. Create an Amazon ECR repository to store movie-app docker image, set up environment variable for the Repo URI, and note the Repo URI in the output. You can choose any name for your repository, this guide suggests using name as movie-app-repo.

    export IMAGE_REPO_URI=$( \
    aws ecr create-repository \
    --repository-name movie-app-repo \
    --query repository.repositoryUri \
    --output text \
    )
    echo Repo URI: $IMAGE_REPO_URI

    The result should look something like this:

    ECR repo creation output URI

    Keep this information on a separate notepad as you’ll need this image repository URI when you specify container definition in your Amazon ECS Task Definition.

  • Create Docker Image

    In this step you’ll create a Dockerfile and then build a docker image for your sample application.

    1. Move to the source code directory.

    cd ~/environment/amazon-ecs-dotnet-app-graviton/app/

    2. Create Dockerfile in the root of the project.

    touch Dockerfile

    3. Open Dockerfile, either in Cloud9 IDE or via command-line text editor such as nano or vi, then copy the commands listed below and paste them into the file, and then save the file. Each command has an inline documentation to explain the purpose of the command.

    FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
    WORKDIR /src
    
    # These lines copy MvcMovie.csproj project files in the current directory 
    # and then restore for the specified runtime, using -r.
    COPY ["MvcMovie.csproj", "./"]
    RUN dotnet restore "MvcMovie.csproj" -r linux-musl-arm64
    
    # Build dotnet project and publishes the application and its dependencies to a folder for deployment to a hosting system.
    COPY . .
    WORKDIR "/src/."
    RUN dotnet publish "MvcMovie.csproj" -c Release -o /app/publish -r linux-musl-arm64 --self-contained false --no-restore
    
    # Docker build command
    # You need to use an architecture-specific tag if you want to target ARM based instances. 
    # Note that .NET is only supported on Alpine on ARM64 and x64, and not ARM32.
    FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine-arm64v8
    WORKDIR /app
    EXPOSE 80
    COPY --from=build /app/publish .
    ENTRYPOINT ["dotnet", "MvcMovie.dll"]

    4. Build and publish docker image with tag movie-app. Tag your image with the Amazon ECR registry, repository, and optional image tag name combination to use. For detail, you can refer here later. The commands below can take a few minutes.

    docker build -t movie-app .
    docker tag movie-app $IMAGE_REPO_URI

    5. Confirm the image is created successfully.

    docker images movie-app

    The command line outputs should look something like below:

    Docker images command output
  • Publish Docker Image

    In this step you’ll login to your Amazon ECR Repository, and then publish the new image you built, in the last step.

    1. To be able to push the docker image to the Amazon ECR, you’ll login into the default private Amazon Elastic Container Registry. The URL for your default private registry (replacing the values from environment variables you created earlier) is: https://$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com.

    aws ecr get-login-password | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com

    2. Finally, you’ll push the docker image you created, to the repository, via following command.

    docker push $IMAGE_REPO_URI
    aws ecr describe-images --repository-name movie-app-repo

    Output should look like this:

    ECR describe images command output

Application Architecture

Here is what your architecture looks like right now:

Architecture module 3

Conclusion

You now have a docker image for .NET application in Amazon Elastic Container Registry - repository, named movie-app-repo. Next module is about creating an Amazon Elastic Container Service with EC2 Launch type, with graviton2 instance type to deploy your docker image.

Was this module helpful?

Thank you
Please let us know what you liked.
Close
Sorry to disappoint you
Is something out-of-date, confusing or inaccurate? Please help us improve this tutorial by providing feedback.
Close

Create ECS Cluster