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.
Preparation
- Prepare your AWS Account. If you never create an AWS Account, please check this link.
- Prepare your CircleCI Account. Don't have the account? Check here.
- Prepare Github Account. I will use Github as the repository, feel free to use another Git Repository Hosting.
- Install Dotnet SDK.
- Your lovely IDE.
- 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.
- We will create public repository. Feel free if you want to create private repository. Click Public Repository and after that click Create Repository.
- The required information to be filled are repository visibility and repository name. Feel free to fill other fields too.
- Check your repository.
Prepare Github Repository
- From the dashboard, click your account picture and click new repository.
- Fill some required fields and click create repository.
- After that, you will be redirected to your repository.
Prepare your projects
- In your favorite terminal, generate the projects. You can use
dotnet new webapi -o CircleCIDemo
.
- Add additional files like
.gitignore
and.sln
. Usedotnet new gitignore
anddotnet new sln
. After that connect solution with project, usedotnet sln add CircleCIDemo
.
- 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" ]
- Push your works to Github Repository
Setup IAM and Circle CI
- Go to IAM and Click Create User
- Make sure you tick option "Access key - Programmatic access" and Next
- 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.
- 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
Please use context to fill some environment variables. You can check here.
I've defined some variables like this.
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.
Repository
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
.
- 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
- You may use curl or another tool to try call the API.
Thank you
Thank you for reading. If you have questions, don't worry to ask me.
Top comments (0)