DEV Community

Cover image for How to Deploy .NET 6 Web API Docker Image to Amazon ECR using CircleCI?
Bervianto Leo Pratama
Bervianto Leo Pratama

Posted on • Edited on

How to Deploy .NET 6 Web API Docker Image to Amazon ECR using CircleCI?

Hello!

I'm going to share about how to deploy Docker Image to Amazon ECR using CircleCI. What is CircleCI? CircleCI is a CI/CD (Continuous Integration/Continuous Deployment) platform. We will learn together to deploy Docker Image to Amazon ECR. If you want to know more about Docker or Docker Image, please visit their official documentation. So, what is Amazon ECR? Amazon ECR (Amazon Elastic Container Registry) is an Amazon Services that you can use to store Docker Image. Okay, let's get started.

Deployment Flow

Preparation

  1. Prepare your AWS Account. If you never create an AWS Account, please check this link.
  2. Prepare your CircleCI Account. Don't have the account? Check here.
  3. Prepare Github Account. I will use Github as the repository, feel free to use another Git Repository Hosting.
  4. Install Dotnet SDK.
  5. Your lovely IDE.
  6. Congrats! You are ready to advance.

Create ECR Repository

You will need to create ECR Repository. You also can check here to understand how to create ECR Repository. I will give some screen captures.

  • From the dashboard, you can search ECR in the search box and click Elastic Container Registry.

ECR From Dashboard

  • We will create public repository. Feel free if you want to create private repository. Click Public Repository and after that click Create Repository.

Create Repository

  • The required information to be filled are repository visibility and repository name. Feel free to fill other fields too.

Create Repository Form

  • Check your repository.

Create Successful

Prepare Github Repository

  • From the dashboard, click your account picture and click new repository.

Dashboard

  • Fill some required fields and click create repository.

create repository form

  • After that, you will be redirected to your repository.

repository

Prepare your projects

  • In your favorite terminal, generate the projects. You can use dotnet new webapi -o CircleCIDemo.

Generate Projects

  • Add additional files like .gitignore and .sln. Use dotnet new gitignore and dotnet new sln. After that connect solution with project, use dotnet sln add CircleCIDemo.

Additional Files

  • Create Dockerfile as following.


FROM mcr.microsoft.com/dotnet/sdk:6.0 as build
WORKDIR /app
COPY . .
RUN dotnet restore
RUN dotnet build -c Release --no-restore
RUN dotnet publish -c Release --no-restore --no-build -o publish

FROM mcr.microsoft.com/dotnet/aspnet:6.0 as runtime
WORKDIR /app
COPY --from=build /app/publish /app
CMD [ "dotnet CircleCIDemo.dll" ]


Enter fullscreen mode Exit fullscreen mode
  • Push your works to Github Repository

Setup IAM and Circle CI

  • Go to IAM and Click Create User

IAM

  • Make sure you tick option "Access key - Programmatic access" and Next

Access Key

  • You may click "Attach existing policies directly" and tick to "PowerUser". Note: Just choose the appropriate registry. If you are using private, please choose "AmazonEC2ContainerRegistryPowerUser" otherwise use "AmazonElasticContainerRegistryPublicPowerUser" for public.

PowerUser

  • You may next and fill other fields until you have created the user. Don't forget to download or copy the Access Key Id and Access Key Secret. Don't share it.

Setup CircleCI

  • You may use this config:


version: 2.1
orbs: 
  aws-ecr: circleci/aws-ecr@8.0.0
workflows:
  build_and_push_image:
    jobs:
      - aws-ecr/build-and-push-image:
          context: dev # update with your CircleCI Context
          public-registry: true # use this if you use public repository
          repo: <your repo name> # update with your repo name


Enter fullscreen mode Exit fullscreen mode
  • Please use context to fill some environment variables. You can check here.

  • I've defined some variables like this.

variables

  • You need to define some variables like AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ECR_REGISTRY_ID, and AWS_REGION.

  • More information about the orbs, you can check here.

  • Done. You can push the config and try build from CircleCI.

  • You will see this screenshot if you successfully configure the repository.

Success

Repository

GitHub logo berviantoleo / circleci-aws-ecr-demo

Circle CI + AWS ECR Demo

Circle CI + AWS ECR Demo

Deploy your Docker Image to AWS ECR use CircleCI. :)

License

MIT

Blog Post

Dev.to






Testing

You may try the ECR. If you are using the public repository, you can pull it without Authentication. Different with private repository, you should have the Authentication before pull the Docker image.

  • Pull use docker pull <your ecr name>. As example, docker pull public.ecr.aws/f2t8g3i6/dotnet-demo-berv.

Pull success

  • Create Docker Container from your Docker Image. docker run -p 8080:80 --name dotnet-demo -d <your ecr name>. Example: docker run -p 8080:80 --name dotnet-demo -d public.ecr.aws/f2t8g3i6/dotnet-demo-berv

Run Container

  • You may use curl or another tool to try call the API.

Use curl

Use Postman

Thank you

Thank you for reading. If you have questions, don't worry to ask me.

GIF Thanks

Top comments (0)