Create a .Net Core Docker Container and Deploy it to Azure
Articles,  Blog

Create a .Net Core Docker Container and Deploy it to Azure


Bonjour, Hi, welcome in another video of cloud in five minutes let me introduce myself I’m Frank Boucher Microsoft Azure MVP based in Montreal in Canada and today I will show you how to create a .Net Core application package it in a Docker container and deploy it in Azure in today’s demo I don’t want to use the big Visual Studio the Visual Studio IDE I want to use cross-platform tools or mostly terminals, Azure CLI and maybe Visual Studio code the first thing we need is to create our application so let’s do it so for that we’ll use the command dotnet so that net new MVC to use the MVC template so we’ll create a website and the output will be in Frank demo perfect everything is good so now we could run will if we check a new folder is here so we’ll go in it and now if we run that net run you I can spell it correctly we can see we have the template that is available to perfect stop that clear the screen okay so now what we need to do is pack add your application to be able to deploy it so the command to use is done net publish we’ll use the release and the output will be in a folder called we’ll be using that folder later on excellent so if we check we have a new folder here where everything should be perfect okay so let’s clear the screen again voila okay so now we have our application so step two is to put it in a container so to do that we need a docker file so let’s create one so a docker file animal will paste that so this is some kind of the recipe so from the microsoft.net 2.0 2.1 a spinet core runtime that’s the image that will be download from docker hub to get started so that’s our base image we’ve specified the working directory app and then we’ll copy the local like from our machine app folder into the app folder inside your container and then we specify our entry points for us it will be done at and the name of our dll so we’ll save that voila so in docker you have the same idea like in get with the gig get ignore in docker it’s called docker ignore and it’s for the same purpose so it’s to skip some folders or files so in our case we’ll skip the folder bin and object you could be of course more explicit or you could even skip this step for a demo purposes perfect so let’s clear the screen what okay so next it’s time to build our container package our image so let’s do that as we can see on step 1 docker don’t download from docker hub the image instead it used the local one because I already run the script before the image is now on my local machine so now our container is created and we can test it with this comments or docker run and I specify the port 81 81 just to map the port 80 from the container effect now if I go check that remove the HTTP and voila work cool excellent okay let’s kill that and clear the screen so now we have a wrap like we have it in a container so now it’s time to create a registry in Azure and upload push our image over there so today I’m using as your CLI those action will also be possible using PowerShell so as AZ group create Frank demo and East us perfect so let’s clear the screen so the next come out I want to create my registry so I will do a Z and a sure container registry create I specify the resource group the name of my registry the SKU I want to use so for business for a demo purposes basic is plenty and I also want to enable the admin so I could login and push ok so now it’s done let’s clear the screen again so now we want to get our credential so we’ll do again a sure and registry credential showing the credential perfect so I use that but we could also go on Azure so let’s go in the portal and now we’ll go in my source group should have a frank demo resource group at a little follow so we’ll go here then we’ll go into registry voila so container registry and now if I click here in these settings if I go for the access keys I have my registry name the server will use that in if in next step so we see that the admin is enabled and I see the user name will use that and the password so we’ll use those information to connect to our registry so before doing that let’s clear the screen so we need to tag or container so here we’ll use docker tag we’ll specify the name of our container and the registry name with the version 1 perfect now it’s done ok excellent now we want connect to our registry so darker login by default we’ll try to connect to docker hub but that’s not what we want right now we want to push our image inside adjure so we need to specify and the HTTP part is important in Azure so specify the full URL remember it wasn’t in the portal and then the username and the password perfect so of course the next step is to push your image so darker push when we specify our tag so depending on the size of your container it will take more or less time obviously so we’ll get back as soon as that’s done perfect now it’s done so we have our application in a container the image of that container is now in our registry in Azure what’s left is to create a web app that will be using that container as source code okay so step 4 create that web app so if we say web app we need a plan so let’s easy app service plan create and we’ll call that plan demo plan and we’ll specify again the resource group Frank demo with basics Q and a Linux plan for now perfect the demo plan is created let’s make some space so now what we need to do is to create the web app so we’ll create an empty shell so here I’m creating easy without create I specify again the resource rule the plan and I specify here that the the runtime we don’t really care about that just because I won’t create a quick empty shell perfect it’s not all done so let’s make some room I will explain the next command so the next command here is to change the setting so we’ll use AZ web app config and then I will specify the information for the container registry the user and the password to be using if I’m not specifying those we’ll try to get my image from the docker hub that’s not what we want I want to use my own and since it’s private I need to pass the user and password so let’s try that so now what will happen is the azure will create my website and try to download my container from my registry that could take a little bit of time depending on the size of my container so while that’s happening let’s have a look to what we have created so first let’s go on Azure so if i refresh the resource group we’ll see now we have a demo plan and we’ll have like a container and our web app ok so the web app so we’ll open that and we’ll browse here so I have the information and now if I check here in the container setting this is the information I just upload so let’s have a look perfect so here I see that the source of the images using an azure container registry I see the name of my registry the tag and I even see the log and if I go now I see that on Azure my application is now is deployed great so in for little step so we can create an application then put it in a container package or container upload it in a registry and use that image to create our application all steps are pretty simple but you have a lot of opportunities to make mistakes I will put the link in the description the post that I try to package in fact that’s all the problem I had while preparing this video so we’ll share it to you have a look it will be in the description I hope you like this video and I hope it was useful don’t hesitate to subscribe put a thumbs up share it it’s nice it helped me a lot if you want to know more about container in Azure have a look to my other video here or here another video of cloud in five minutes thank you for watching see you next time you

14 Comments

  • Chenna Vinaykumar

    Just a question here, if it all there is any change in .net code for example I modified Contact.cshtml and published the modified dll to app folder from localhost the changes reflected, how do i push this changes to azure website which is already running in linux app service plan. Please explain me the steps to achieve this

  • Amer Čehić

    Thank you very much Frank. Can you create a video to explain how to deploy .NET Core WebAPI with Entity Framework Core migrations to Azure Portal using CI/CD Azure DevOps. Thanks in advance!

  • Lorenz Michels

    Hi, I followed your tutorial, but have a difference and an error. The difference is that the field "Image" in the screen shown at 9:45 is filled with "frankdemo". When starting the application

    🙁 Application Error

    If you are the application administrator, you can access the diagnostic resources.

    When investigating the diagnostics, I see:

    Your container failed immediately

    Observation

    Your container failed to start almost immediately.

    Possible reason

    Your Docker image may not exist.

    Error message

    Failed Docker pull with parameters [pull frankContainerDemo.azurecr.io/frankdemo:v1]. Error: Error response from daemon: Get https://frankContainerDemo.azurecr.io/v2/frankdemo/manifests/v1: unauthorized: authentication required

    Suggestion

    Please check the repository name, image name, and container definitions.

    In the logs I see:

    2019_03_22_RD0003FF308CE2_docker.log:

    2019-03-22 19:53:03.442 INFO – Issuing docker pull frankContainerDemo.azurecr.io/frankdemo:v1

    2019-03-22 19:53:03.665 ERROR – docker pull returned STDERR>> Error response from daemon: Get https://frankContainerDemo.azurecr.io/v2/frankdemo/manifests/v1: unauthorized: authentication required

    Could this be caused by the fact that I had to run docker in windows mode on my windows desktop and that on Azure it is running in linux mode?

  • Valli Grandhi

    I am running .net core application which uses openxml nuget package to read docx images in linux environment. It is working fine locally after installing lbgdiplus package but i try to dockerize and deploy it using azure function app i am getting ' gdiplus threw an exception' eventhough i specified 'run apt-get install lbgdiplus' in docker file.please help me how to solve this error.Thank you

  • Jose Luis Mantilla G.

    Frank, why don't the complete example with pipeline pulling the artifact, that is so basic and is not the best practice in the real world.

Leave a Reply

Your email address will not be published. Required fields are marked *