Containerizing an ASP.NET Core 3.0 Web API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome today I'll show you how to contain your eyes or doctor eyes on asp.net core to 0 Web API you'll learn how to create an asp.net cortecito Web API project how to add docker artifacts with missile studio code and how to build and run the asp.net core project as a docker container before getting started let's go through a few things that you'll need in your box to follow the steps in this video the first one is a dotnet core CLI which you can get at dot dot net and this will allow you to both build and run net core apps in your box next we're going to use missile studio code which you can get at code that we saw studio.com and this we'll be using to explore the contents of the project that we're going to create as well as to edit the docker files used to a queenly container images next you're going to need a detour engine which you can get docks that docker calm and this you're going to need in order to be able to enable the creation of docker images and the execution of the commands in your box if you scroll down to the support platform section you should be able to find a version for your OS and finally the postman tool is a very handy tool to be able to query a Web API with a very nice interaction this is an optional requirement as you may as well just use the browser for this but it's really nice to use polls and for this and it's completely free also notice that all these tools are available in multiple operating systems so you're free to pick whichever version works for you for this video we'll be using the Windows version of the tools so now let's close this so now let's go to a command prompt and here let's use the dotnet core CLI to create a brand new asp.net core 3-0 Web API project if we were going to call hello a spinet core so let's do dotnet new web api - oh hello ASP net core and just for the sake of making these things a bit simple for this video we will not be enabling HTTP yes but of course you should always enable EVPs in your production projects so let's do know yes so this quantity generate a bunch of files in a new hello a spinet call directory and create separate for us so let's switch to that directory and then let's open a business video code to see what code generated so we'll do code dot and here we are in visa to the code this clause is from here and the maintain that of our interest is the controller that gets included in this a new template which is called weather forecast controller so in this one and before we move over let me actually talk about this little prompt here these a process to add a few files that this is to the code needs to be able to both build and run the project in from business to your code itself generally a good idea to say yes here so that's going to get a new that PS code folder without going to files in there now back to the controller weather forecast controller is a very simple a controller that gets automatically added as you create this new project has just one week one API and get API and what it does is just returns a bunch of a weather cold weather forecast rejections or that base it on some date some temperature and some summary that's also based in this random list of a weather summaries ok so this is the API that we will be working with across this video so now let's actually run this before container rising and see what we get so let's go to the debug a hop and let's do start debugging this is going to do data built to builder project and then it's going to spin up a browser for us so the project is running in localhost in port 5000 the actual API is the weather forecast and route matching the controller that got created so go there and as you can see we are saying we're getting results out of this call to the weather forecast API now like I said you can totally use a browser for this usually postman is a much better tool for exploring what comes back from this API so that's what I'll be opening postman here and I'm just going to create a new request get request to that URL and then I'll click send and as you can see here now we have those five a random weather forecasts a generated for us in a very nicely formatted a way okay so now let's my since and let's close this and let's stop the debugger now it's time to actually containerize aid applications also close this so in order to do that one thing that I recommend is to add the docker extension for visual studio code and that you can do from the extension table if you don't have it already installed just look for docker it should be the first the first thing on pops out there and Yossi Starling will take a few seconds what this will allow us is to among other things it allows to automatically generate a token file for the platform that we're working with here this case a spinet core so close these go back to this folder and now let's go to view command pull it and let's dive docker a docker files to workspace so that is going to ask you because a fourth application platform that we're working with in this case is infinite core which operating system we are targeting with this container in this case is going to be Linux and which is the port that will be our app is going to be listening on and let's just leave it as port 80 and as you can see an initial docker file code generated for us right away now let's listen slow this file and see what what all this stuff means so each line here really means or creates a layer within our our application that builds on top of the previous layer so the first line here says that we're going to be creating an infinite a core 3 0 app and that we're tagging this layer or this stage as base okay and so we're going to be using the MGR magnet comm / dance / core / a spinning repository and then we will be switching to a new deal called a / app as I worked here I will be exposing a port 80 as we already said now interestingly this template uses this concept of multi stage fields which is a very nice way to optimize the size of the of the docker image what it says is that after doing that do not keep using that in that stage there and let's switch to another stage which is pretty much kind of another image kind of an oil image and that will we will be at aliasing as built and this one here is going to be base it actually in the done that core sdk-based image 3-0 image which is definitely bigger it includes all sorts of tools and compilers and stuff that you need to build it up but that you will not need when it's time to actually around there so we will be running all this section out of that that image and then will be later on will be coming back to the initial base image that we use in over there where we'll use copy the files of all talking read but idea is like I said to optimize alt so that the final image only includes the files needed to run the app but not all the other files needed to compile there so we say go from that basically mission this analysis at Build work tears going to be SRC let's copy the the project file into the root of easy victory is our scene let's restore all dependencies over there and no gate restore and all dependencies of the project copy all the other files into that same location we will remove this line here is not quite needed and then lastly let's do the net build and to build the project but in this case since we're pulling a locker image it makes more sense to do a release version as opposed to the default depopulation of the app and let's uh put all that into an app directory then we go ahead and we go from that for that stage and we come up with and get another stage called the polish stage where we're going to say just dotnet polish that same project in the same configuration and the same opportunity so that ends up finishes the actual compilation and creation of the binaries they're good to include it in this in this docker image and as a final step we go back like I said to the base image and we see we switch to an /ab directory and most importantly we copy the files that live in this Polish directory created in the previous stage and into the slash app directory that was declared as a work dear a moment ago so all the files are in their previous coffee here everything else all the all these other layers that were created here the involve launch of extra files are completely scarred not including the final image so keeping the image very small in size and finally we will say that the entry point is going to be dotnet and pointing to the hello a spinet code that the DLL and that we have here which is the same that we'll get if we just explore quickly the bin directory and over here for for the DLL does produce it by our project ok so now that we have that let's open at terminal just open a brand new terminal here in this code and let's actually build the image out of this ogre file one more thing that I mentioned that I didn't mention by the way is that dr. ignore file which is similar to if you know kit is similar to ignore file it just mentions a bunch of a potential directories and files that you may have a in your project structure that you want to exclude from the image is going to be created so for instance anything that has to do with being an obj where you may not want to include them into the final image you just want to include the stuff that God that is meant to be distributed within the container ok so now let's actually build this image and we do that using the docker build command and we need to sign an attack to these images going to reveal so the attack will be composite of a name and an actual tag so the tank is going to be the name of a image is going to be hello asp net core and let's say it will call it a big one so this is like I said is just attack so it's a way to remember a way to address this version of the image so usually you will be putting here some sort of mill number build version something that uniquely identifies at this version of the image that you're producing and finally I'll say dot to include to mention that this plot is current location is the is a context of the docker bill command that's going to be executed so I'll go ahead and say enter what it is going to do is equal to NCR micro calm and download the ethernet core image first if you need call runtime image and tag it as a space then it gets also the ACK image and both of them are going back into my into my box and of course first time this this may take a little bit because it tackling is to download all these layers into your box but after immediately after the second time you do this this is going to be blazing fast because team is already there and only the new images will be created so as you see after doing that it will go ahead and follow one by one each of the lines that we declared include the restoration and the build building of these of the project and finally copying the files into the final location and this is all done okay and then when that is done let's verify that the image correctly created so let's do docker images and as you can see the image that we created is right here hello spinet core tag we won and also you can notice that we have these other two images downloaded into your into our box for next for next version want to create now that we have the image there we may want to also run it so let's run it to verify that it's actually working as expected to do that we can use the docker run - IT - RM command so this is kind of an interactive session and that the docker container should be removed after we finish a run in the process that running inside the image we should also map a some port into the internal port of the container so for instance we will say that when we browse to the 8080 port that should be route into the into port 80 within the container okay so this is the way to kind of open a little window into the the app that's running inside a container and then we need to mention which which image we actually want to run here so that would be the hello asp net core v1 image so let's hit enter and as you can see the app is now running just as if we were running it directly from from our box and it's just that now it is not running and going back to Boltzmann it is not running in port 5000 it's actually running in port 8080 mapping inside into port 80 so if you just hit this you can see we just got a response back it's exactly the same thing as if it was running in the box but now it is running within the container and finally I had to stop the container we can use to control C and that's going to stop the container and actually discard it keeps the image but this cars the container because of the options that we specified oh there you go and thanks for watching I hope this video was very useful and I'll see you next time
Info
Channel: Julio Casal
Views: 51,726
Rating: 4.9221311 out of 5
Keywords: asp.net, asp.net core, .net, .net core, containers, docker, web api, asp.net core 3.0, .net core 3.0, visual studio code, vscode, tutorial, api, asp.net core 3, .net core 3, microservices, .net conf, beginner, c#, cloud native, julio casal, dotnetcore, .net core api, dockerize asp.net core web api, dockerize .net core application, docker asp.net core, docker asp.net, docker dotnet core, docker .net core, docker .net
Id: Po9jQS7WBDQ
Channel Id: undefined
Length: 14min 17sec (857 seconds)
Published: Mon Sep 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.