.NET 8 .🚀🔥: Integrating Docker with a .NET Web API - A Step-by-Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends thank you for watching this video I am Muhammad and today we're going to be discussing how we can create a Docker image of our net web API we're going to be going through step by step of what needs to be done we're going to be explaining why do we need to add all of the information inside our Docker file so we can understand the full process of it before we get started into the actual coding let us understand the fundamentals of Docker and why do we need it so and what I want to do is I want to create first of all my application so this is let's consider this as my application and I'm going to call this my API so my API usually it does not run by itself so usually it has requirement for a lot of different services so it might have a requirement for an SDK so this is would be my net SDK this I'm going to put here net SDK it will have requirements for class libraries so it's going to have requirement for class libraries it's going to have a requirement on specific configuration and for example this class libraries it could have also different requirements so this one has its own different configurations as well so all of these will basically play a major role into uh facilitating how our application will work this will actually allows us to run our application so whenever my application is running in order for it to actually start executing properly what it does it need to do it actually needs to get all of this information and once this information is available for my application then my application will boot up and then what I'm able to see let's see here a browser and then once I have all of that then I will able to see my application inside a browser where I'm actually able to see the Swagger endpoint for this so I'm having a browser and here I can for example see all of my Swagger information and my endpoints so we can see here that our API does not live by itself although this is going to be a net API it's going to require a lot of different configuration for it to work and this configuration basically it's going to allow allows us to make sure that our application is up and running and have everything that we need and this is what we currently have within uh any of our examples that we have covered the previously we usually have a main application we have some dependencies on different librar spe some specific configuration and our. net SDK available for us so usually my net SDK is installed on the operating system which is on my machine so this is installed on my Dove machine the class libraries and the configurations for those class libraries are either available through my source code or on my devv machine so it could be one on the other similarly to the configuration that the application that itself needs it could be available on my machine or on my uh in my source code so these are going to be the main building blocks so now let's say I finish developing my application and everything is running smoothly what's going to happen now if I want to deploy this so whenever I want to deploy this what do I need to do I need to basically publish my application and once I publish my application I need to I need to take the published version and put it on a server so here so let's say all of this is going to be currently on my local Dev machine so all of this simply is going to be running on my local Dove machine so let's take this let's take the drawing card and we can draw it like this local Dove machine let's make this a bit bigger okay so this is actually what's happening inside my application all of these are actually running inside my local D machine so if I want to move this to a server what do I need to do so let's say now I want to move everything to my server I need first of all to install my SDK so that's going to be the first step install my SDK then what I need to do is I need to get my configuration and configure my application again on my server then what I need to do is I need to take the output of my API and basically get publish it into my server so now that I have done this here I'm going to have a published version of API and I'm going to have this available there so once I have done that I'm going to link my SDK to my apis and my configuration as well they're going to be linked to my API so once I have done all of that this couldn't work so for example this can directly out of the box this can directly out of the box be able to work for me but in some scenarios this might not work because basically what I have here is I have a lot of dependency so I'm going to S This Server so sometimes this might not work because sometimes the configuration rely on uh some OS configuration level so it could be I have a different networking configuration that's needed networking configuration that's going to be required for me to be able to run my application and this is not I did not do that it was already configured on my local Dev Machine by default but on my server it didn't do that the SDK version the SDK that I have might installed here it could have been a different version of the SDK so here we need to make sure that I'm stalling the right version of the SDK so that could be the first layer of problem that I could be facing if I'm trying to to deploy my application another potential problem that it could be from the networking point of view so I'm going to create another box here and in order for my application to be running I have to have my networking in place and my networking will basically specify what are the input and the output that I'm going to be needing and basically make sure I'm configuring the right permissions uh to uh for my inut inbound traffic and outbound traffic to access my application so here we can see that this also needs to be configured directly on a two-way and lastly whenever I actually publish my file I need to give it a permissions to the actual directory that it's going to be running on so I'm going to put directory permission specifically if we're trying to deploy it on IIs I need to have read write permission on that directory in order for it to actually utilize it as a web server uh as a website inside it web server in order for it to run so we can see here that all of this is actually happening in the background whenever I need to deploy so we can see we have 1 2 3 4 five six dependencies that actually needs to happen whenever I'm trying to create my application and this is fine to a certain extent but this require a lot of money where work in order for me to do so if I'm trying to scale up my application and to make sure that I'm not repeating the same mistake over and over again and I want to have a more consistent deployment process than having to try to debug all of these different configuration what can I do I can actually utilize Docker so Docker basically in essence what what it does is it will take all of these different information that I currently have here and store it in a box so basically Docker in a few simple commands what it what it does it's going to take all of this configuration it's going to store it in a box and basically whenever I need to deploy it will take this box copy paste it to whatever it needs and this box will already contain all of the different configuration all of the different permissions all of the different uh sdks that it needs it it's going to contain everything so it's going to contain the configuration it's going to contain the SDK it's going to contain the permissions etc etc so this box that I'm going to be utilizing here is going to allow me to move everything that I need for my application it's going to allow me to move my deployed application directly from my local web environment to the any server I want to deploy it could be do server test QA server the prepro production as long as I have my Docker image built I can basically um move it and here I utilize the uh the keyword box because we put everything inside that box so but in essence that what Docker does it takes all of the right configuration that my application used to run and in order for it to uh uh to execute and put it inside this box we can call this box a Docker image and once install it I can move this image to wherever I want and then I can actually run my application through there so this is an very simple term why do we need to have Docker and how does it work now to actually containerize and basically create a Docker image of our application it's going to be quite simple so I'm going to go to my visual studio code and here basically this is the application that we have already worked on before where I have my Formula 1 API and this is a simple API which has a few controllers in it has a driver's controller an achievement controller it has a real dependencies on a class libraries and this class libraries is going to be one to handling data uh database items and the other one is going to for handling all of the different entities so what I want to do here is I want to basically containerize this application in order for it to run inside Docker so how can I do that it's pretty straightforward all I'm going to do is I'm going to add a file and this file I'm going to call it Docker file because as soon as I put Docker file we can see the icon Chang to a whale which means that now I'm able to containerize so once I have done that the first step that I need to do is I need to utilize something called a uh base image so I'm going to put from MCR microsoft.com slash.net for slas nut 8.0 as base so what I'm doing here is basically I'm telling Docker or basically this box that I'm going to be utilizing that you're going to be of type.net version 8 and I'm going to be utilizing you to do some work before I actually create the last version of this box so basically I'm I'm getting two boxes the first box I'm going to utilize it to basically experiment and do all of the work inside of it and the second box is once that uh once I do my experimentation the first one I'm going to be utilizing it to store it inside the final one so it's going to be something like this so I'm going to have my first box which is going to be for the for um a b as a base so basically for testing purposes and I'm going to have my production one so basically I'm going to do all of my work here when to copying my code making sure it's configured correctly inside the box and once it's actually configured I'm going to be moving this from my uh first box to the second box and I'm going to be only utilizing so we can think about this box as a staging box for all of the work that I need to do and basically prod here will be the final one will contain all of the right information so once I specified this the next step is I need to specify a folder inside this box that's going to allow me to do the work and I'm just going to call it forward slash up then I need to expose what are the ports that I want to utilize and I'm going to say I'm going to utilize port 8080 and expose also another Port which is going to be 8081 so one could be for HTTP and other one for htps so now this is the first part I told Docker that I need my box to be ready and as a flavor of asp.net version 8 and I'm told it that I need to have a folder and these are the ports then what I'm going to do now I'm going to take the same thing uh which is going to be from mc. microsoft.com slash.net SL SDK and this is the SDK that I'm going to be utilizing and this SDK is going to be allow me to build my application do all of the work that I need to do and I'm going to say this as my build so here basically I'm going to do all of the building for my application inside this box so now I'm all I'm going to say is going to say that I need to provide it an argument so if we know withinn I need to specify build configuration argument so I'm going to say build underscore configuration and inside there I'm going to say it's going to be of type release so this application whenever it's going to be building it's going to be built as release after that inside this box I need specify my work directory and then it's going to be also a folder called SRC once I have done that now it's going to be the fun part where I actually need to copy my dependencies because if we take a look at my application API here if I open my CS Pro we can see it's actually utilizing the data service um class library and if I go to my data service class library and if I open my CS approach we can see it's actually referring to the entities so we can see that we have the dependency upon dependencies so that's why what I need to do is I need to copy the source code of my data data service and my entities uh CS Pro into my Docker image in order for me to have them available for me inside that image so to do that again quite simple it's going to be Formula 1 do API we're going to start by the API and I'm going to put formula 1. api. p. CS Pro and I'm going to move it to formula 1. API so that's going to be the first one I'm going to remove this I don't need it and I'm going to just copy this to make sure to copy the other two as well rather than trying to retype everything so this one is going to be let's close this for my data service and I'm going to put it inside my data service and this is going to be for my entities and here your entities so now basically that I have copied these and I have I'm told Docker that it needs to be stored inside my machine or my Docker image all I'm going to say right now is runnet restore and I'm going to tell it that it needs to restore my formula 1. API SL formula 1. api. CS Pro so now what I'm doing is once I copied everything I'm telling not to do a full restore on this because once it actually tries to restore all of this if there is any missing dependencies if there's any problem with my with my configuration or all of the new package that I'm going to be needing this will show up before it actually build so I can know exactly that once this is run successfully that all of my application dependencies are there and I can actually proceed to creating and building this so once I have done that and it passed now I'm going to copy the rest of the source code from my local directory to the uh to the actual Docker image so that do to do this I could copy everything from my local directory to my uh Docker image and that's why I put dot dot dot here means everything inside the first one to the to everything inside the second one and then once I have done that all I need to do is go to my uh API so I'm going to put SRC so this is the folder that we have created and it's going to be the formula one. API and because this is going to be my main entry point for my application it's not going to be the data service or the entities I need to publish this so I'm going to runnet build formula 1. api. cspr and I'm going to give it my configuration so I'm going to say this is as build configuration let's remove these brackets we don't need them and then I specify where is it going to be built so I'm going to say forward slash the output it's going to for slash app forward slash build so now that I have done that what I have done here basically is I published my application I specified uh what type of it and basically I specified the output folder for it quite straightforward so once I have done this the next step is I need to actually now copy it from this box that we said here to the production one and to do this I'm going to put from build and build as referring to this item here so this box that I currently have here as publish so what I'm telling it is whatever I have taken from this build just give it a different label as published because we have published my application and I'm just providing my argument for it which is going to be my build configuration again and this is going to be released so I'm just going to copy paste this rather than typing it again and then what I'm going to say here Run net publish formula 1. API do CS Pro and then I'm going to provide the same thing- C and this is going to be the dollar sign build configuration let's copy and remove this better again I'm going to specify my output and this is going to be app for slash app SLP publish and lastly I need to specify the app host to not to not use it so I'm going to use up host I'm going to say equal to false so once we have done that finally I need to actually uh finalize my deployment which is going to be the last stuff that we do which is going to be from base and this is the base that we have created all the way here which is contained the asp.net which is a much more smaller version than the SDK which makes our image much more smaller I'm going to say as the final image and I'm going to specify my work directory that I have specified before which is app and I'm going to say that copy and here I'm going to say from publish to forward SL and to forward SL app forward slash publish copy everything and lastly I need to specify my entry point and this entry point is going to take basically my net run command for it to run and it's going to be formula 1. api. DL and this is it so within 24 lines of code what did I do I was able to containerize my full Docker application and this was able to work and this will allow me to actually containerize my application quickly so let's open my terminal and just one thing before you start you need to make sure that you have Docker installed on your machine so as you can see here I have Docker running if I open my where's my dashboard if I open my dashboard we can see here that I have Docker running inside my dashboard we can see both of them are green which mean they are running so let's put this on the side and now I'm what I'm going to do is I'm going to open my terminal and inside my terminal I want to make sure I'm in the right directory okay perfect and the command that I need to do in order for this to run is going to be quite simple so because we're utilizing Docker I need to put Docker and then I need to put my command which is going to be I want to build this image then I need to specify the the tax I want to give it a name for this image so I'm just going to call it F1 API and then I put dot because it needs to find my Docker file and when I put a dot it means that it's in the same directory where I'm executing this command so if I click on enter now basically what it does it will get Docker Image First and we can see I have already some error in my code so let's see what is this error it's telling me that something is not found so let's check this right now maybe I have a typo so for Formula 1 API CS Pro okay here I have it oh that's my problem so I put here data service and this needs to be entities and this one API is not Capital case so let's save this and run this again let's do a clear and let's run this again and now we can see it's actually what it's doing is actually downloading my based images that I have which is my asp.net and my sdks for me to utilize this will take usually a few seconds because you can see here the site uh the size is Big so we'll continue once they finish so now that they are actually doing we can see here that they copied the file they're executing the net restore once the net restore is executed successfully we'll resume so we can see here it executed successfully it copied the director it now run the not build inside my folder that I have specified now it's publishing it and now that it has published it it's telling me that I can access this so I'm going to clear this and I'm going to go back to my Docker dashboard and I'm going to click on images and here I'm going to just refresh and as you can see here I have my F1 API which is has been uh created 34 seconds ago and if I click on it we can see here that I have all of my information and we can see that I'm utilizing notet 8 is having all of the commands that I have specified we can see the work directory that exist we can see the endpoint that currently exists as well we can see here that the entry point is going to be executed when it runs and basically we can see all of the different layers that has happen in order for my application to run if I click here on images we can see that it's actually built on a net image that I have created previously or basically I have utilized previously and we can see here some of the packages that it's utilizing because it's n it's relying it's a Linux version of net so it's relying on dbn and this is all of the packages currently exist we can explore them all if we want we can see the different version of them and here we can see that because my API is utilizing automapper we can see that my packages is already installed so if we take for example at automapper this is version 12.0.1 point0 so if I go here to my API version sorry my formula 1. API if I click on the Cs Pro and I go to automap we can see it's the same version exist here as here so we can see automapper 12.0.1 12.0.1 if I go more even like to the these are all Microsoft components so let's take the uh open API which is 8.0 so let's see if we can find this where is it there's a lot search for it open API so we can see here that the open API that we are utilizing is also going to be version 8.0 which is going to be the one that that we're going to be referring to so all of that is going to be automatically available for us because we have just build our image through Docker now if I want to run it it's going to be quite simple if simp again it's it's going to be Docker the command and then it's going to be the keyword run I need to specify the port that I want to connect to so I want to connect from my machine on port 8080 to the port that I have specified inside my Docker image which is going to be all support 8080 and then once I have done that I can basically say that which image I want to run and in my case it's going to be called the F1 API so just's going to copy this and I'm going to put this one here and I'm going to run it now we can see that my application is running on Port 88 and I can access it because I have deployed it as a production I will not have access to Swagger so but this will show you that it's actually can work I'm able to access my application and it's running through Docker I hope this video was helpful it's just just a quick introduction about Docker andet if you have any questions please make sure you put them in the comments down below uh have a great day
Info
Channel: Mohamad Lawand
Views: 27,176
Rating: undefined out of 5
Keywords: .net, api, beginner guide, step by step, csharp, c#, dotnet, dependency injection, code with me, ef core, entity framework core, rest api, crud, minimal api dotnet 7, dotnet data, dotnet services, .net core, asp.net core, docker compose, docker explained, dotnet docker, dotnet dockerfile, dotnet docker tutorial, postgresql docker, dotnet 8, .net 8, Containerization, Docker Tutorial, Docker Integration, Web API Development, Docker Containers, DevOps Tools, Coding Tutorial
Id: _wp2zJHs9l0
Channel Id: undefined
Length: 22min 4sec (1324 seconds)
Published: Fri Jan 05 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.