How to Deploy .NET 6 Web API Docker Image to Amazon ECR using CircleCI?
Tags: aws, circleci, dotnet, docker
Published: 2022-03-18 18:09:20

Hello!

I'm going to share about how to deploy Docker Image to Amazon ECR using CircleCI. What is CircleCI? [CircleCI](https://circleci.com/product/) 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](https://docs.docker.com/get-started/overview/#docker-objects). So, what is Amazon ECR? [Amazon ECR](https://aws.amazon.com/ecr/) (Amazon Elastic Container Registry) is an Amazon Services that you can use to store Docker Image. Okay, let's get started.

![Deployment Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64ciyo8buge2toxt4g5v.jpg)
 

## Preparation

1. Prepare your AWS Account. If you never create an AWS Account, please check this [link](https://portal.aws.amazon.com/billing/signup?nc2=h_ct&src=header_signup&redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#/start/email).
2. Prepare your CircleCI Account. Don't have the account? Check [here](https://circleci.com/signup/).
3. Prepare Github Account. I will use Github as the repository, feel free to use another Git Repository Hosting.
4. Install [Dotnet SDK](https://dotnet.microsoft.com/en-us/download).
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](https://docs.aws.amazon.com/AmazonECR/latest/userguide/getting-started-console.html) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7gww2c6eljye4qc4j91.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jx0hjl8bdcu8oz2ypneg.png)
 
* The required information to be filled are repository visibility and repository name. Feel free to fill other fields too.

![Create Repository Form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdkz15ctpk20y2as8hdt.png)

* Check your repository.

![Create Successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/teaxpqjyymgf18h3vt3o.png)

## Prepare Github Repository

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

![Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dlogte06sndgpv95nau.png)
 
* Fill some required fields and click create repository.

![create repository form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtumm7sdndrqr212z7n0.png)

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

![repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcvk944ot54qq6euyaqf.png)

## Prepare your projects

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

![Generate Projects](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5nz5082aqx78elop44w.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ts2avpgt3yr61x6ntxo5.png)
  
* Create Dockerfile as following.

```Dockerfile
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" ]
```

* Push your works to Github Repository

## Setup IAM and Circle CI

* Go to IAM and Click Create User

![IAM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/744kv2c8gfu6v8vva4ym.png)
 

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

![Access Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rizclbv04ga00vofstsl.png)
 
* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usaww9852bcbejp6objv.png)

* 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:

```yaml
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
```

* Please use context to fill some environment variables. You can check [here](https://circleci.com/docs/2.0/contexts/).

* I've defined some variables like this.

![variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0n8r6dk9ho6ohwm9q6h.png)

* 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](https://circleci.com/developer/orbs/orb/circleci/aws-ecr).

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

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

![Success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9svppzxt2qqtr5bdkzh.png)

## Repository

{% github berviantoleo/circleci-aws-ecr-demo %}

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0e3mnky3cwa8a1mjr129.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ydgi2hamrlv96p8qhuq.png)
 
* You may use curl or another tool to try call the API.

![Use curl](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uewk8ubpd7b2sw906wpw.png)

![Use Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48zb37f5ulz2e9pl11o7.png)

## Thank you

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

![GIF Thanks](https://media.giphy.com/media/LOd8g4r5uljWf2RrnT/giphy.gif)