Even though there is enough material available on this topic, still it could take 1-2 days a beginner to upload their first .Net web application docker image to AWS ECR. This post lists the steps without going into details.
I used Visual Studio 2019 to create a new project ASP.NET Core Web Application.
Give a Project name in the next screen and create an MVC project. Make sure to select ‘Docker support’ check box.
Selecting ‘Docker support’ option will automatically generate a Dockerfile in the solution.
Additionally, .dockerignore file will also get generated one level up in the project location, which will use to make the build context as small as possible by ignoring unwanted files.
To build and run a Docker Image from above solution in a Windows PC, we can use Windows PowerShell. First go to the project folder and move the Dockerfile one level up to the folder where solution file and dockerignore file resides. Otherwise when you are building the docker image, an error message will appear if Docker file reside at the same level of .csproj file.
Now, open a PowerShell window and navigate to your project folder. Then use the following command to build the Docker image. CCISWebApp is the project name.
docker build -t cciswebapp .
This command will take some time to execute. It will download the base images if they are not present locally. Once command is successful, use following command to see the Docker images and your Docker image should be listed.
docker images
To run the Docker image, use the following command.
docker run -d -p 8081:80 --name myapp cciswebapp
Now you can go to localhost:8081 to access your app in a web browser. To upload this Docker image to AWS Elastic Container Registry (or ECR), we have to first install AWS CLI. Go to the https://aws.amazon.com/cli/, download and run the 64-bit Windows installer.
Open a new PowerShell window and use following command to configure AWS Command Line Interface. Old PowerShell window won’t recognize below command.
aws configure
You will need AWS Access Key ID and AWS Secret Access Key to complete the command. To get them, go to AWS Console -> IAM -> Users
Then select your user account and go to the ‘Security Credentials’ tab in the Summary page. Use ‘Create access key’ button to generate keys and complete aws configure command.
Then go to the AWS Console -> Elastic Container Registry and create a Repository for your images. Copy the Repository URI.
Then use following command in PowerShell to tag your Docker image. First argument is current image name and second argument is repository URI.
docker tag cciswebapp 6xxxxxxxxxx9.dkr.ecr.us-east-2.amazonaws.com/ccis-web-app
Now, to use Docker CLI to push the image to the repository, first need to authenticate to the ECR registry. This can be done with following command. Make sure to use correct region and account number in the command.
aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 6xxxxxxxxxx9.dkr.ecr.us-east-2.amazonaws.com
Then use following command to push the image. Again, make sure to use correct region and account number in the command.
docker push 6xxxxxxxxxx9.dkr.ecr.us-east-2.amazonaws.com/ccis-web-app
If the command is successful, image will be uploaded to the repository.