Microsoft has invested billions in Azure to drive technology and also hired best of the minds on this planet. From last few months, Azure has been launching new container-focused products and services on a regular basis. One of these products is Azure Container Instances. It acts as a bridge between platform as a service and infrastructure as a service architecture. Perhaps it would be okay to call this service as “Container as a Service”. Azure Container Instances (ACI) allows to rapidly create and launch containerized applications, without any overhead and with an easily scriptable set of commands. Designed to work both on its own and with tools like Kubernetes, ACI adds container-management commands to Azure, coupling them with a billing model that’s based on per-second usage, with no need to create and deploy (and pay for) container hosts. In this blog post, we’ll create an ASP.NET Core App, containerize it and deploy it as single instance on ACI.
Create a ASP.NET Core Web App
We’ll be creating this using Visual Studio 2017. Open Visual Studio and go to file -> new -> project and Select an ASP.NET Core Web App project:
Provide a name for the project and path to store files. In our example, this would be the DemoWebAppProject and then click Ok. After this, out of the templates provided by Visual Studio, select “Web Application Template”:
Select appropriate dotnet version. Also uncheck the option for “Enable Docker Support”. Click Ok and it will create a very basic web app project based on dotnet core version.
Go ahead and build the project and it should build fine.
Create Docker Image
We’ll need to go to root project path and add a basic Dockerfile to it. After this, open the file in Visual Studio and type below code:
FROM microsoft/aspnetcore:latest WORKDIR dockerdemo ADD ./DemoWebAppProject/Debug . ENTRYPOINT ["dotnet", "DemoWebAppProject.dll"] EXPOSE 8086 ENV ASPNETCORE_URLS http://0.0.0.0:8086
After this, save the Dockerfile. By default, if you do not expose a port and bind the application to it, ASP.NET image will expose port 80 and bind the application to it.
Build a Docker Image
We need to open command prompt in admin mode or PowerShell in admin mode and browse to the project root directory. After this, we can run below command to create a docker image:
docker build -t aspnetcore/demowebapp:1.0.0.0 .
Verify that image has been built fine using docker images command. We can also initiate a container to make sure that our image is working fine as expected:
docker run -d -p 8086:8086 -name=demowebapp aspnetcore/demowebapp:1.0.0.0
And browsing the application in a browser:
Push the image to the Azure Container Registry
For deploying docker image, first we need to push it to a container registry. For this post’s purposes, we’ll be using ACR or Azure Container Registry. First we need to login to ACR using below command:
docker login -u {username} -p {password} {ACR-name}
Now, we need to tag our image again so that ACR can accept it. For this, our image tag needs to be first accompanied by ACR name:
docker tag aspnetcore/demowebapp:1.0.0.0 mgoyal.azurecr.io/aspnetcore/demowebapp:1.0.0.0
After this, we can push docker image using:
docker push mgoyal.azurecr.io/aspnetcore/demowebapp:1.0.0.0
Deploy Dockerized Image as Azure Container Instance
Open Azure Portal, select ACR in reference and then go to Registries. You should be able to find the uploaded docker image here. Click on the image and then click on the ‘Run instance’ option:
Now, submit the details for the instance to be created:
It will take a few minutes to deploy the docker image to the Azure Container Instance. Once deployed, your container is up and running with either a public IP address or a private, internal address. Public addresses can host public-facing services, while private addresses work well for private applications or internal services used to support your public interfaces.
After this, we can browse the application using the URL of the container instance or the public ip address assigned to container instance:
[…] can be also be thought of as the follow-up on previous blog post where we learned steps to create Container Instances using .NET Core and Docker and deploy the same […]
LikeLike
hi,
Can an app (web api) run on Apps Services connect to an API run on ACI with private ip address?
LikeLike