Using Containers in Azure
01 Aug 2020 - Sanjeev
Skills Measured
- Create container images for solutions by using Docker
- Publish an image to the Azure Container Registry
- Run containers by using Azure Container Instance
We will do the following to understand different concepts involved to learn the skill set being measured. Below code assumes windows host machine with docker desktop installed.
- Create and publish a .Net Core WebApi projectlink
- Package and Push the app as a docker Image to dockerhub
- Create an Azure WebApp and deploy the image
- Create and push the docker image to Azure container registry
- Deploy the image from Azure Container Registry to WebApp created earlier
- Create and deploy the image from Azure Container Registry to Azure Container Instance
Create and publish a .Net Core WebApi project
Run the below code from a command line window
mkdir webapis
Create a working directory to host the project
cd webapis
make that directory as the working directory
Run the command dotnet new webapi
to scaffold a new ASP.NET WebAPI Project
Execute dotnet run
to run the application
Application is now running on ports 5000 (http) and 5001 (https). Confirm that the application is running by browsing to url https://localhost:5001/WeatherForecast
Package and Push the app as a docker Image to dockerhub
publish the application to out directory using command dotnet publish -c Release -o out
Within the project directory, create a file with name Dockerfile
without any extension. Use the command echo FROM > Dockerfile
to create a new file. Copy the below content in that file.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
# Use the base image of ASP.Net 3.1
WORKDIR /app
# Set app as the working directory
COPY /out .
# Copy the content from the published directory
EXPOSE 80
EXPOSE 443
# Expose ports 80 (http) and 443 (https)
ENTRYPOINT ["dotnet", "webapis.dll"]
# Starting command for this image
Run the command docker build -f Dockerfile -t webapis:v1
to build the docker image of the application with tag v1
Run the command docker images
to make sure you have the container in the local repo
Run the image using the command docker run --rm -it -p 5000:80 -p 5001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="crypticpassword" -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:/https/ webapis:v1
and run command docker ps
to check if the container is running.
Navigating to the url https://localhost:5001/WeatherForecast
should give the similar out as before
Execute command docker tag webapis:v1 <docker account name>/webapis:v1
in preparationg for moving the image to dockerhub. Run command docker push <docker account name>/webapis:v1
Create an Azure WebApp and deploy the image
Navigate to Azure Shell and sign-in.
let randomNum=$RANDOM*$RANDOM
# Generate a random number to use so that we do not have to revisit the names used in the sample
resourceGroupName="lab-containers"$randomNum"-Group"
# Resource group name
az configure --defaults location=eastus
az configure --defaults group=$resourceGroupName
# We do not have to pass the location and resource group attribute once configured so the length of the command becomes small
az group create --name $resourceGroupName
#Create the resource group
appServiceName="appService"$randomeNum
az appservice plan create --name $appServiceName --sku Free --is-linux
#Create a linux free app service plan
az webapp create --name webapp$randomNum -p $appServiceName -i <docker account name>/webapis:v1
#Creates a web application using the docker image. Replace the docker account name in the above command.
az webapp show --name webapp$randomNum --query enabledHostNames[0]
# Execute to get the url to the website
Navigating to the
Create and push the docker image to Azure container registry
#Create an instance of Azure Container Registry with admin user
az acr create --name acr$randomNum --sku Basic --admin-enabled --query loginServer
#Above command returns the login server uri. Execute below command will give the password for the admin user
az acr credential show --name acr$randomNum --query passwords[0].value
From your local machine, execute below docker commands docker login <acr login server>.azurecr.io
to upload the image to acr
Tag the image for acr by executing the command docker tag webapis:v1 <acr login server>.azurecr.io/webapis:v1
and push the image to the registry by executing the command docker push <acr login server>.azurecr.io/webapis:v1
Deploy the image from Azure Container Registry to WebApp created earlier
Lets make some change to the code so that we are sure we are deploying the new image only available from ACR image registry. Update the WeatherForecast
class with new attributing ReportingTime as public DateTime ReportingTime => DateTime.Now;
. This would provide when the weather was reported.Execute below commands to build and push the image to acr.
dotnet build
dotnet publish -c Release -o out
docker build -f Dockerfile -t webapis:v2 .
docker tag webapis:v2 <acr login server>.azurecr.io/webapis:v2
docker push <acr login server>.azurecr.io/webapis:v2
az webapp config container set --name webapp$randomNum --docker-custom-image-name acr$randomNum.azurecr.io/webapis:v2 --docker-registry-server-url https://acr$randomNum.azurecr.io --docker-registry-server-user acr$randomNum --docker-registry-server-password <Password for admin user in acr >
When navigate to https://<Webapp url>/WeatherForecast
we should see the updated response
Create and deploy the image from Azure Container Registry to Azure Container Instance
Create and deploy the container with az container create --name aci$randomNum --os-type Linux --cpu 2 --image acr$randomNum.azurecr.io/webapis:v2 --ports 80 --ip-address public --dns-name-label aci$randomNum
Navigate to url http://
References
- Docker Documentation
- Host local containerized website in https
- Microsoft .Net Images in DockerHub
- Run a custom Docker image in App Service
Do let me know if you have any clarification/suggestion on this post. I will be happy to know your feedback!