Deploy a .NET Core API with Docker (Step-by-Step)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
wouldn't it be great to live in a world for you you could just run any piece of software on any machine and it just worked just ran without getting error messages like that well do you know what that world maybe isn't as far away as you think and maybe if you stick around you can find out a bit more about how to run software on anything [Music] well hello wherever you are whenever you are PMI I'm in Melbourne Australia as usual and for EMI it's October what's not it's August 2013 losing track of time now after my last video which was a colossal three and a half I was long I vowed never to make anything that long again and so this time I'm they're going to be really short on to the point and so in this video we're going to learn about docker and how you can deploy specifically dotnet applications using docker so what we're waiting for so but yes we're going to deploy a dotnet core API with docker now just a bit of housekeeping the ingredients if you want to follow along with this video you'll need a text editor I recommend the escort which is free the.net core sdk also free docker desktop if you're running Windows or Mac and if you're running Linux then docker Community Edition again which is free now in terms of those downloads there's a companion blog article for this video I put flash up on screen with all the download links in the description below you'll also need an account on docker hub now I can't remember because it's been a while since I've done it you may need to sign up for a docker hub account before you can download the software I can't quite remember that may not be right but it's worth the saving up for anyway and it's completely free optional if you want to be run another local machine just to test how containers work you can do that I'm going to show you that as part of this video it's very much optional and also optional to sign up for them as your account because we'll be running our darker app in Azure as well just to show you how ubiquitous how ubiquitous it is and how easy it is to deploy across different platforms I've put in inverted commas free there are three tiers and feed Leo's available but of course do do be in mind as you're as paid for service and you me depending on what you did Inc of charges if you follow along with this tutorial the way I'm going to do it I doubt you'll incur any significant charges again totally optional so what is docker well as it says that on the screen docker as a containerization platform meaning that it enables you to package up your applications into something called images and then we run those images as containers on any platform that is running docker or that can run docker so images and containers are something you'll hear a lot about it'll become clearer as we move through the module just exactly what they are but I start to thinking about it in terms of an object-oriented programming paradigm because I was very familiar with that I think of an image almost like a class and a container almost like an object instance of that class where you can have an image and spin up multiple times and scale out as required so just before we move on to building our app I just wanted to go through the differences between physical machines virtual machines and containers or darker physical machine fairly straightforward you have your opening system and then you have apps running on top of that cool virtual machines you'll have your operating system great and you'll then have something that VMware virtual box which generically I guess or asperities hypervisors and they then allow you to create a mumble a number of virtual machines that run full os's and then you have apps running on top of that so in short virtual machine is operating system virtualization and that has a lot of uses and it's widely used today fantastic doc has a bit different it's lighter weight so again you'll have a operating system you'll then have docker running on top of that and then what docker does is it virtualizes an application environment for you so your containers effectively so it's not virtualizing an entire operating system but really part of an operating system and that allows you just to run small containers running your applications so it's really a application virtualization rather than the full operating system virtualization so as a result Dockers you know I can not really then virtual machines quicker they scale better there's a lot of use cases we are you good to choose doc or something like a full march on machine so I think with that explanation let's start our app so let's jump into the S chord to create our API project and I will just open up a terminal I'll change into my F Drive that's what I'm going to place it so we'll just type a.net new command we'll choose our template type which is web api and we'll give it a name doctor API so just as an FYI for this video we're not deep diving into how how to build dotnet core API is I've already done a video on that and I'll put a link to know on the other video you can go and have a look at that if you're interested in that in this video we're just building a template API app we're going to use docker to deploy it that's the mean thrust of the video so not going to do too much with this chord so yes don't let new web api is the template name of the project's docker api wait for that to get created if you do a directory listing you can see here it's created this folder with all our project collateral and to open it in chord from the command line if you just take chord our docket API which is the name of the folder and that should recursively open up project in the same window wonderful now the only thing I'm going to do is say yes to that is I'm just going to take out this little bit of these few lines of code form our configure our method in our startup class and then all we really need to do a mid should I save that let's bring up another terminal and we should be in the project listing though in the project directory should I see so if you don't mate build I would be very surprised if it didn't build a name of the a.net run to run it and that should run up on our localhost port 5000 and fight those and one if we click on that it will put the open edge I think control click there you go so we need to end in API values to hit our values there we go I mean get value one two and I'll just show you where that's coming from if we jump over to our controllers I mean look at our values controller here's our action methods that respond to different request types or be hit this action result method here I me just got back with time value one and two we can take this one as well by changing the signature that we call the API on so if we go back here and just pass in a random number you'll get a single value back which corresponds to what's being returned but now that's all we're going to do with our Web API again if you want more detail on how to build way the API is in botnet core I've got a video for you on that but with no we're going to move on to the real exciting part of this video which is to add a docker file so this is the last slide of the tutorial I promise as is the absolutely last slide probably doesn't need that much explaining but as mentioned we're now going to add a docker file to our Web API project we then run that through the docker engine and it parses the file which is basically a list of them you'll see what it is in a minute it's a list of commands and steps that we need to perform an order to build a docker image so docker file goes through the docker engine it uses our Web API project as well and then at the end of it we have a image that has everything we need to spin up and run a container instance of our web api app and we can then take that image and spin up multiple instances across multiple platforms so let's start with our docker file so in our API project we just simply click new file and we will call it docker file there's no exceptions there and don't give it an expansion capitalize the D now you'll see that it puts a little real icon next to it so it knows as a dockerfile the first time you create a docker file and BS code that may prompt you to install this extension I strongly recommend that you do it because I'll be using it as we move throughout the tutorial just to be clear on what it is it's this one here just search an extension marketplace and install it if it doesn't want you I highly recommend it so as with most of my tutorials I'm now going to type in the docker file step by step it's a bit boring but I'll speed up the typing I'll play some music over it and then I'm done and I'll come back and I'll take you through exactly for each of the bits [Music] [Music] okay something that wasn't too painful to watch I did put some commentary in there that kind of alluded to what's going on now just before I take you through the file in a bit more detail and you'll understand what's happening while I remember I just want you to create one more file in your project again in the root of the project so new file and this time we're going to call it dot darker ignore now all this file does and it's very simple is it just tells the docker engine to exclude certain things when it comes to building the final image because one of the principles of darker and the images and the containers that are spawned from them is that you want to make them as small and as efficient as possible so well you don't have to do this step I strongly recommend that you do do it because it's just gonna make your image your resulting images much much smaller so then it all be gay that's it we'll save that file so that's done I just wanted to remind you to do that and you can you can ignore the the doc and ignore file note but it's worth doing so coming back over to our doc our file let's go through it now before we go through it I just want to explain how the docker engine builds an image it takes the docker file and then actually uses an empty container to start layering up and building your resulting image so you can remember I talked about images and I've talked about containers the image is the the blueprint that is spawned to create a running container that's true but also is part of the build process what docker does is it will take an empty container and build inside that at each step and then ultimately you'll end up with an image if that makes sense so each step in the docker file a container will be used to perform that state and what that means and when we actually build the dockerfile you'll see what's happening what that means is if you then change step 7 and your docker file docker is intelligent enough that it won't rebuild from scratch if you issue the build command again it will just go back to the point at which it changed because it's got this kind of log tape idea or going on hopefully that made sense anyway back to our dockerfile sort line - you've got an empty container there's nothing inside a pot more very basic in this case Linux file system so when when you installed on windows anyway when you installed a docker desktop actually gave you an option to use windows containers or Linux containers now I've left - Linux containers and if you're running Linux it'll be Linux containers but what the container will be and if you are using Linux containers is a representation of a Linux file system so the build process starts we get an empty Linux container or basically an empty then it container and we work within that so the first thing we need to do at line two is pull down a copy of the dotnet core SDK excuse me because we need that to build a project at line three we then see within that docker container we want to create a working directory where we do all our stuff line six were then saying copy the CS proj file from our actual PC into our working container and then we run dotnet store to resolve any dependencies line ten we then copy the rest of the files into our container into the working directory and a line a line yeah at line eleven we run dotnet publish with the configuration flag of release I make place the output of that which is basically our compiled project into a folder called out at line fourteen and this is an important point going back to the the concept that containers are to be small and efficient and lean we don't want to use the full dotnet core SDK and within our container because it's only required for build activities you can one cord on top of Israel of course but there's this extra fluff that we don't need at runtime so what we're doing here at line 14 is saying we're going to just use the dotnet core runtime inside a container going forward that's all we need because we're just running an app Lang 15 were specifying working directory again line 16 is exposing a port so we're seeing we've gotten we're going to explore 84 use and you'll see how that works and I bet 917 is basically what's called that multi-part building I'm just basically combining parts from the previous build steps and putting it into our working directory and then finally we're specifying our entry point which is basically seeing how do we want our container to start off in a container starts what is it going to do in our case all it's going to do is use the dotnet command to run our DLL which is effectively our app so with that explanation let's go on to actually using the docker engine to build our image alright so just a bit of a pre-flight check just make sure if you're using Windows or Mac that you're a docker desktop instance is running as it is here on my desktop and maybe just another check if you just take a darker - - Belgium it's installed and it and it looks like it's running so that's cool so clear the screen now to build our docker image it's very straightforward all you type is doc our build and then you specify what's called the working directory I'm just going to use a single dot didn't or you just want to build it here you can do that and that will work that's fine but I'm going to use a slightly different more verbose syntax and tag our image with a name and that becomes very useful later on when we push up to the docker hub for distribution later on now the naming convention of a docker image if I just pull over the blog article here I will show you really piss it yeah here we are so this is the standard naming convention that your dog or hub ID forward slash project name or any name you want together cool on the ocean so you will follow that naming convention so doc our build - t - tag it my daugher ID is binary this'll so please don't use that you can but what do you any good that's my unique docker ID use your own one we'll call it anything we like dr. API and I believe it has to be lowercase and we could optionally put innovation I'm not going to so call on the original number I'm not going to do that the darker engine will just put in a default version which will be latest and then don't forget to put the trailing period and to tell the engine where we want to build it so let's see what happens cool so you can see here that it ran through all the steps and I advise you when you run it yourself to look at the steps there's a bit of a explanation of what's going on what you will see here is it successfully built our image and this is this random number I was talking about earlier on if you don't tag it and so it still does that anyway and then you can see here this last step as we successfully tagged as binary thistle /dr API and then you can see here that's appended this latest item on here as the version so that is a darker image built fantastic now clear the screen just to make it look a bit cleaner so you can list the images that you have on your system now I've got quite a few because I've been playing around with us so if you take darker images it will list the images that you have on your system and we should be able to find binary thistle there is darker API cool and it was created about a minute ago fantastic also on D s chord if you just go over here you can see here the in the darker extension it will have all the images that we have on our system and you can actually see here that has the image that we've just created fine and us pay no mind to this forget that for the moment I will come on to that a bit so that sounds basically built are taking our docker file built it and we've created our image docker dr. API image so now we want to run it as a container right sort of run our image it's fairly straight forward and just as an FYI just keep keep your eye on this section of the docker plugin for BS code just just though then trace see what happens here so a docker to run docker can you guess what the command is going to be yes little surprises docker run no what we want to do is we want to specify a port mapping so we do that with the dash PFLAG now if you can remember well you don't actually have to remember you can see on screen here we exposed port 80 internally within our API what we want to do is we want to specify the port on our actual local machine that we want to map to internal container port so we can call it you can do anything you like but 8080 is the one that's commonly used I'm not too sure why but port 8080 I'm not going to map it to our port 80 here of our container so we will actually access this container using this port here okay the 8080 port so that's all the P flag is doing and then other than that it's just binary thistle in my case basically the name of the image binary first saw doctor API and yours will of course be called something slightly different so hit enter and you can see here if you're paying attention that we get a little container line in our containers here box up unds cord with a little green arrow no surprises what that means but we can also see down on here and that we are listening on port 80 fantastic so all we simply do is we just go back over to our web browser just pull off for my other screen yeah the little bar should I say not pill it off that's that's something entirely different I'm say no more and people just anything that bit bigger so HTTP localhost remember some stuff from our available and this time we'll do it 8080 API values yes if I die again API values there we go so again you know it's not really any different from it running natively and locally on our machine but this time it's running on our machine in a container now you may go okay that's great but my like wire that I do that no if you were just a one developer in your own little world you probably wouldn't be running it in a container as opposed to on your local desktop the real power of containers is when you come today and going I'm going to use it somewhere so let's come on and look at how we can do that now just before we do that I probably should have told you how you can stop stop difficulty enough so as it says here you can press control-c to stop now that kind of brings us out of the but looks like it's brought as out of the application but what you can see certainly on Windows and I've noticed some windows as opposed to Linux you can see it looks like our word container is actually still running now we can check this by going docker PS at the command line and what that will actually do this shows a list of running containers and in fact we do actually have a running container as it is reflected up here now in order to stop that container you'll take docker stop and then we don't use the image name we use this container ID which is unique so it's pasted on here and then you can see that built its reflected up here that's it's stopped and if we do the same docker PS command you can see that we don't have anything that is cool okay great now of course you could have one up here and this is one of the nice cities of this little plugin you could have right click this and click stop in this case we can click start that we want and it started so again on the docker PS and it started and then we can go up here and you can stop it wonderful okay so a no-no we can actually go on to taking our image and pushing it up to docker hub and then deploying it to lots of other endpoints so let's do that now so I back over in a web browser let's just go to docker hub sores google at first because I'm not sure what the URL is docker hub okay hub docker calm there you go and it may have remembered me don't think it has so I'll sign in as a remember those IDs cool so you can see they are the binary thistle ID is the idea I've been using and over in my repositories you can see how high I have a few different repositories that I've just been playing around with but we do not have the one we've just created which is docker API so we want to basically get our docker image doctor API image up to docker hub no relatively straightforward to how you do that docker push very fissile doctor API and no surprises pushes you up now what you might get if you've never used this before it might actually ask you to authenticate no big deal if it does use your doctor ID and your doctor password let me use the r2 to login to docker hub so we'll wait for that to finish and then we'll go and check to see whether it actually pushed up which I'm sure they would have done so looks like it's finished let's go back here let's do a refresh and indeed there we go we've got our API oh yeah no just just a little looking side note you remember that we put this docker ignore file in our project now I actually forgot to put that in one time and I did the the docker push and it took a really long time to push up because my upload connections not that great here at home and because I hadn't excluded all this other stuff it actually took ages to ward up but you can see how quickly that hold it up it's really really small it's really really light so what that means is we now have a our docker image in our docker repository ready to be pulled by other other hosts just another quick sidebar before we move on to doing that and rounding off the video if you actually click on my apologies probably looking at Explorer you'll see a lot of other images that I've been provided by other vendors you can use and you can pull them excuse me I've just double clicked on that vehicle so you can see all these other images that are available to use so you could have been to Lennox you get poor squares and this is easiest taping docker run and then the name of the image and it will just come down and vomit like that so we're going to do that now with our little image to see how easy it is to do okay so what we've got here is just an empty Linux box with nothing other than docker running on it so it doesn't have the dotnet core framework or anything like that that's a really important point to know all it has is Darko so we can just check the Dockers on it by doing in this case we have to put sudo in front of it Belgian gonna ask me for my password bigger darker so in order to run our docker API image all we have to do it's exactly the same actually it's the command you'd previously issued which is forget don't forget to the Siddle docker run again we'll specify the port mapping will keep it the same as before eight zero eight zero eighty and then we will specify the name of our image now this is fine let me just type this one cuz I can't multitask doctor API that's what happens if I start to put two things at once oh my goodness okay beneath this little darker API that's correct so when I said tagging your images is important this is why it's important because clearly my docker API is unique so what will happen in this instance is the docker CLI knows to go to the default docker repository which is docker hub in this case and using the uniqueness of my image name because it's got my doc ID in it it will attempt to Bill in this case pull it down and run it because it doesn't have a local copy and that's actually a good point let's just do that first and I'll tell you what I mean by that last comment so you can see they're unable to find image by their official docker API latest locally so what that means is every time you run this darker command if it's got our local or recent local copy it will use that it won't always go to the internet and try and pull one down for darker hump if it's got a local copy in this case because it's a completely clean box and I've never run that command on it before it didn't have a local copy so it's gone off to darker hub and it's a it's pulled it down and you can see here it's no listening so you can do a couple of things here we can do a new terminal just to have a look at the fact that's running so dark our PS again I've always forget to the sudo command then so let's just do that now PS reason why I like Windows and you can see here yes that we have a local instance orbit running and even it has a nice mapping but identification there which is great so we can again we can we can test this if you don't believe me that it's working but the whole point of this is how easy was that to run that bit of software on Linux very easy I don't know about you but I had an instance many years ago it might be a bit different now where I was a struggling developer and I needed to install a copy of article 1 of the next box cuz I needed to test something and I kid you not the documentation to install Oracle on a Linux box in that time it was literally that thick and I just gave up couldn't do it with docker all you would need to do is do doc not run and put over the image name for Oracle is and it will just run it for you it as a developer Dockers an invaluable tool to stand things up quickly simply and easily so let's just go onto our localhost type in some of these clashing Accardo very my house and see if it's running and indeed is so we get exactly the same result as we did on our local Windows box by just calling effectively the same the same API endpoint this time you just have to include the 8000 port mapping but the takeaway here is that it's super quick and super simple to get stuff down to something the Linux we're going to wrap off this section or this video actually by deploying our docker image in the cloud on as your so let's just do that then okay so this is the azure portal for those of you who have not seen it it's basically the landing page for 0 and we'll have all your resources that you've created a resource and Azure is just something that you want to run so we're going to create let me get this right a container instance resource which as the name would suggest allows us to run docker container very quickly and very simply so create a resource you can just type what you're looking for container instances there we go so we'll click create my P is you go subscription resource scripts in Azure on basically just groupings of a source if you don't if you've never used the salt as year before it just create a new one we'll call it anything the light we'll call it dark our API retainer it's just the name that as your we'll use for it the region that you want to place it in I mean and then under as you know so I just need to find Australia East there you go nope the image is public on docker hub that's fine if it was private you'd probably have to supply some credentials to the repository that you were going to but case in point we pushed our image up to docker hub its public ok I'm just oh it's public and an image named simple as typing binary that's all basically it's the name of our image and again this is why the naming convention is so important docker API the way of type Linux and then we can just have the size of this container instance we'll just keep defaults are fine move on to networking people just leave the defaults we don't need to change anything they are we don't need to change anything there I think that's fine and don't need to do anything there and then review and create so let's create our actually I think in networking I needed to provide something we just moved back yes this DNS name I don't need to provide something there so let's call it doc our API gets it you know okay cool so basically this is the URL that will go to to use our endpoint and it will be publicly available on the interweb so review and create I think that's all good and then quickly eight and they with anything as you'll it will take a couple minutes just to alway and stand up course or deployment is complete that didn't take long at all I did cut a few seconds out but literally took didn't even take a minute so we can go to the resource and have a look at it and it just gives you some various telemetry readings and all that kind of stuff but the thing that we're Morstan interested in is the domain name or the URL that we're going to go to so let's just copy that let's move over to Firefox and instance and you'll have to put in API values and there you go how easy was that to deploy to as you know I did that manually you can automate that of course but I just wanted to show you how easy it was to do it in and automate it anyway that brings me to the end of this video I hope you enjoyed it hope you found it informative have you if you get any feedback on how I can improve please drop it in the section below if you liked the video please give me a like and if you haven't done so already maybe you'd be nice if you could subscribe but I won't hold that against you if you decide not to that's okay too anyway until then thank you stay well stay happy and I'll see you all next time
Info
Channel: Les Jackson
Views: 219,080
Rating: undefined out of 5
Keywords: dotnetplaybook, binarythistle, docker, docker image, docker container, docker build, dockerfile, les jackson, .net, .net core, api, .net core api, c#, step by step, tutorial, azure, azure container instance, container instance
Id: f0lMGPB10bM
Channel Id: undefined
Length: 36min 17sec (2177 seconds)
Published: Sun Aug 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.