.NET 8.πŸš€πŸ”₯: What is Docker? 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 containers and the different terminology that's being utilized within a container World we're going to be seeing how all of these pieces fits together as well we're going to see how we can actually containerize our as well we're going to be seeing how we can actually containerize our web API so let's get started so first of all we're going to be defining the main keywords that currently exist within a container word so we're going to have first Docker which is I think the most popular one we're going to have a container registry we're going to have an image we're going to have a container and we going to have something called a locker file so we want to understand how this work and we're going to be delving deep into all of these different terminology and understand how do they fit then we're going to be containerizing our application so let's say I have a web API and this is my nice web API which is built on nut so this web API because it's actually built on the nut it's going to have some requirement so it's going to have first an SDK which is going to be the nut SDK it's going to have some new get packages it's going to have some configuration etc etc so let's say here this is my API and all of this information that we actually need for this API to run so let's say I want to deploy my web what are my current options in order for me to deploy my web application so the first option that I have is to actually have some kind of a virtual machine and this virtual machine can take different uh types of form so this could be an ec2 instance so this could be a web app on azer so this could be a VM on azer and there's a lot of different option that we can actually use there but how will this actually work and how we can actually deploy our code there so if we want to actually deploy to a virtual machine what we need to do first before we can actually we need to do some configuration so let's put this here and we're going to say that we need to do some configuration so we need to set up the SDK this is all except the web app so we need to set up the SDK we need to set up the configuration we need to set up the networking we need to set up the security protocols etc etc so all of this work needs to happen in order for us to be able to deploy our web API to our virtual machine so how can we do that so we have in order for me to push my code from my local development environment to my virtual machine I need need something like GitHub to be available so let's say here I have GitHub and from within GitHub what I need to do is I need to actually push my code from my web API to GitHub and then once my code is actually in GitHub I can push it to my virtual machine as a deployed version and I can actually from there run it so here what I'm doing is I'm actually taking all of these different information that I have for my web API let's put this here for my web API with all of its configuration and new get packages and SDK publishing it putting it into GitHub and then from GitHub I'm actually creating a published version I'm putting it on my virtu machine so that's one it's one way of doing it if I'm using web app what I can do if I'm using visual studio I can directly right click and click publish and basically Visual Studio will take of all of the different mechanism for me to directly publish from here but this is not really recommended if you really want to have a full automated platform and it has its own limitation but it's one way of doing it but we can see here that in order for me to get this up and running there's a lot of different configuration that I need to do that a lot of manual work on the virtual machine it's not easily replicatable because I need to do all of this work manually again on any other virtual machine etc etc so what are the other Solutions the other solution here is for us to containerize our application and what I mean by containerize is actually to create a published version of our application that we can actually move it from one place to another so for that what we can do is we can actually create a container version of our or containerize our application but how we can do that in order for us to containerize our application we're going to be needing some kind of a technology that's going to allow us to content arize our application and this technology is called one the most popular one is called Docker and Docker basically allows us take whatever we have within our code base and create an container version of it but how will that work so basically Docker is a very light virtual machine and then this is over simplification which takes the commands that we provided we point it out to the source code that we need to do and from there it will actually do all of the work and create a containerized version and we're going to be understanding how this work but basically what we need to do in order for us to convert our web API into something that Docker will understand we're going to be ending something called a Docker file and Docker file is usually directly attached to the application so it's directly attach to the application itself and basically whenever we want to create a container version of our web API a Docker file will be able to directly create or basically go into the source code check all the different configuration check the different uh requirement in order for it to be created and then once that Docker file is up and ready Docker will communicate with the docker program available in our machine or any other Docker service that we utilize again once that is ready we're going to have a container image or an image if you'd like to call it so once Docker has took that file and has executed it's going to generate an image of our application and this image of our application is going to contain the following so it's going to contain the published version of the source code it's going to contain the configuration the SDK the packages and everything else that might need it so all of that that currently exist here is going to be directly available in into our image that we have deployed but what is this image we can think about this image is like a big box which going to be containing all of these different information ready to be deployed anywhere so once we want to deploy it we don't really have to worry about the configuration the compatibility we don't have to worry about any of that because this image we are 100% sure that whatever whatever exists inside of it is going to run anywhere we need it so now that we have our image ready and basically we have we were able to containerize our application into an image how we can actually deploy it so in order for deploy it we can utilize a container service just going to put this a bit down here a container service is basically it is built in order for it to take a Docker image and run it but here we have a small problem because currently the image that has been deployed is currently available only on our machine it's not available anywhere so let's say this container server is running on azer or AWS and we need somewhere or somehow to copy this image from our local machine into this container service how can we do that how can this image move from one place to another in order for us to accomplish this we need something called a container regist and a container registry we can think about it as a GitHub for images or container images because basically within a container registry we can basically publish our container images we can have different versions of our container images we can have as many projects as we want within their own different container images and basically it's a similar concept as GitHub where we can actually have a lot of controls who can access it what version can be deoy etc etc so once we decide on that we want to actually deploy our image to a container service what we need to do is we need to push it to something called a container registry and this container registry could be on azer it could be as a Docker Hub it could be on AWS it could be anywhere as long as we have the configuration for it we are able to act so now that we have decided that we need to push our Docker image into a container registry again this is not solve the problem of how we can actually run it into a container service that as or AWS requires so the way we can actually solve this is by basically creating a container of the image that's actually going to be utilized so we're going to be basically taking an image creating a container out of it and then this container it will be connected to the to the container service so what is a container and what's the difference between a container and the image so we can think about in very simple terms when it comes to C we can think about it as a class and an object so for example when we have a class we have all of the set or all the configuration that exist within a class but when we want to use the class what do we do we initialize an object of that class and we can initialize as many objects of the class that we want and we're not limited to only one and this is the same concept here so an image we can think about it as a class and a container is basically an object of that class so we can initialize as many as many containers as we want from a single class and what we're doing here is basically once we want to deploy we're initializing that container from the image and then basically we are loading that container into our container service without actually going through the process of doing all of the different configuration doing the process of actually setting up the environment the container service will take care of all of that because everything that it needs in order for it to run is already going to be available within the container so we're elevating a lot of the heavy lifting that we need to do in order for this to be up and running so we can see here that Docker Docker file images containers they are basically a piece of puzzle that actually all fit together in order for us to have a more streamlined uper roach so if you want to really summarize this in a very simplistic terms what we can say here we can say that everything start with an application the applications then actually convert into or basically use a Docker file the docker file will basically translate the application into an image or the container image once a container image is created we store it in a registry and once it's actually stored in a registry then we can initialize a cont a container from it and then we can do whatever we want from it from there so we can see that this flow that we follow is going to allows us to have a fully containerized application that can be deployed anywhere that we want and this flow makes it really easy for us to understand if we're having any problems to actually identify what could be the problem so locally on our machine if we're actually trying to deploy this what we can do where we can install Docker to be available locally on our machine and once we install Docker locally on our machine what do we get we get all of these combined together so we don't really have to worry about them locally on our machine so within those on ow machine we can get these two together so we can get a container registry where we can actually store the images and we'll get the locker engine available so this is within a Docker desk on our local machine so right now in order for us to actually just go do a quick overview on this what we're going to be doing is I'm going to be showing you a Docker file uh that is built for a web API and we're going to be explaining the terminologies there and we're going to be seeing how that works so now let's open the visual studio code and this application that we currently see here is the application that we have worked on for so many different videos but basically it's a very simple web API that contains two controllers it contains a SQL light as a database storage and it contain different functionalities and it's all built on net 8 so if we take a look here we can see it's a net 8 we can see we have different packages automapper we have the open API we have Entity framework we have swashbuckle we have all of these available we're also have our application or data store available there and as well in order for us to save a bit of time I have already created the docker file here and what I want to do here within the docker file is I want to explain to you how this works so currently our application as you can see here is compromised of three different folders so we have our web API which is the main entry point and we have two class libraries which actually supporting it so in order for us to have a container version of our application which going to contain all of these it's always a good idea to have the locker file on the root directory of your folder rather than inside your we API because it much allows you much more easier to access the different Services that's needed so once we have created to the docker file you can see directly it has a whale icon next to it and here what we have we have the basically the recipe that we're going to be following in order for us to have a fully containerized application for this web API and you can see here within 24 lines of code we were able to completely containerize our application so let's see how this will work so first things first we can see here that we have something called a base and basically we can see what we're doing here is we're setting up the stalker by informing it of what type of SDK do we want so if we take a look back at this example here we said our application is going to require an SDK in order for it to work so what we're telling here is we're telling Docker that it needs to have some kind of an SDK available in order for it to work and this SDK is going to be the asp.net uh version 8 and basically what we're doing is we're taking once we have defined the SDK we're taking a folder or creating a folder inside this Docker file that we're that we're using and we're toning it up and inside this folder up we're basically we're going to be doing everything that we need in order for us to have our application up and running so then once we have done that we can see here we have the Expose and basically what we're doing here is we're doing the networking configuration that Docker require in order for us to have the networking available so these are the networking configuration in order for us to have this up and running so basically we're telling a Docker that this application will be exposed uh exposing port 8080 and Port 881 one will will be utilized for HTTP and the other one will be utilized for htps so once we have one through all of these four items we can see here that we're actually defining another instance of the framework or the SDK but this is called build why are we doing that so let's explain it here so in order for us to have everything that we need inside our application inside our Docker image what do we need to do we need there's two ways we can actually first publish the application outside Docker and then copy everything into an image or what we can do so this is the first approach and we're going to say this is why it's not recommended or what we can do is we can actually copy the source code into Docker into a Docker image into Docker base basically and then create a published version of that source code into another Docker image and once we create that public the published version We copy it into a new Docker image so why are we doing that why are we doing an extra step where we can actually simply just deploy it on our machine and copy paste it because basically if we deploy it on our local machine the deployment here is going to be completely dependent on all of the configuration of our OS so the deployment here will take the configuration from our OS like the SDK the networking etc etc so everything will going to be based on the operating system that we're running so currently I'm on my Mac so if I deploy it it will take the version of the SDK on my Mac and it will deploy it into a folder and then I copy paste it which is not really what we want because this we can see it will create a lot of dependency on what type of operating system I'm using it will create a lot of dependency on the configuration that I have on my machine and does not really mean that it's a completely unique or uh make sure it's going to run all the different environment but if I copy my source code into Docker and I let Docker do download all of the dependencies SDK do the configuration and then you create a final version I can 100% be assured that whatever configuration I provided is going to be working perfectly so that's why we need here another one which is called build and this build is going to be this temporary version that's going to be removed at the end and only utilize the last one so once we Define this temporary version we're telling it that this uh we're going to be utilizing a release version we're basically publishing it as a release and then what we're doing here is inside this new box we're creating a new folder and this folder we're we're calling it source and inside the source folder what we're doing is we're copying all of the different CS proos that currently exist inside our application and we're running a net restore because basically what we want here is we have already got the SDK into this temporary image then once we got the SDK we were able to do a net restore make sure everything is running perfectly and this will be completely catered For That Docker image so with no dependency on our operating system and once everything goes into place that what then what we're doing is we're copying every single piece P of source code that we have into that new container image and then we are basically publishing it and we can see here we're using that nut build and we're basically building the configuration for it at the end so once we have done that now it's going to be pretty straightforward to just completely just do a net publish you have already buildt it in a published version and then we're basically in a release version and then we're telling you that it needs to be published and once that has been published inside this temporary version what we're doing is we're taking whatever we have published in that and we're copying into the final version and we are able to run it I'm not going to be delving into all of these different commands there's a different video which I'm going to be linking here somewhere where I explain every single command in more detail but I just wanted to give you an overview about how this will work and why are and what are the steps that's needed in order for us to have a containerized web API so when within this video today we were able to explore the world of Docker and containerization we were able to understand the different terminology that currently exists and we were able to understand how everything fits together if you have any question questions please make sure you put them in the comments down below if you'd like to support me please consider supporting me on patreon or buying me a coffee with that said have a great day and thank you very much for watching
Info
Channel: Mohamad Lawand
Views: 3,576
Rating: undefined out of 5
Keywords: .net, api, beginner guide, step by step, csharp, c#, dotnet, 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, container registry
Id: ZyTzy3P_lP8
Channel Id: undefined
Length: 16min 7sec (967 seconds)
Published: Fri Feb 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.