Dockerize .NET 6 in 10 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to techno savior today in this video I am going to show you how you can run dotnet 6 in Visual Studio code directly so the best part about dotnet right now is that it is a machine independent so basically it can run on Windows it can run on Mac so this is the website I'll be that I'll be referring to set up the dotnet environment although it is relevant to dotnet core but that doesn't matter because.net 6 is kind of a upgraded version of dot net core and I'll show you how to how can you install the latest version of dotnet SDK so the prerequisites over here are you should have the vs code docker extension installed you should have a dotnet development environment installed and you should be having a c-sharp code for visual studio it's basically for the code prettifying and other stuff okay so what you can do is over here click on dot net download the latest version of.net as I said the latest version of dotnet is nothing but the dotnet 6 that is the stable version as of now so you can download this based on your Windows or Mac again Mac has different versions Windows as different versions then the other installation process are like simple you just go do a step-by-step install and it should get installed but how do you verify in order to check whether all the system is up and running for dotnet then what we can do is we'll just go into the terminal now what I'll do is if I'll type docker it should give me uh this information is basically tells that okay my Docker is up and running okay or I can just type Docker Dash V to show me what version of Docker is installed in my machine similarly for DOT net you can type.net dash dash version okay it will give you the latest version the.net version that is installed in your machine so I have Docker of Docker version of 20 and Dot net 6. so now since uh my prerequisites are installed at least the docker and the dotnet is there let me open the visual studio so I've created this ms.net 6 folder it's empty folder as you can see now the docker extension already installed over here if you don't have the docker extension you can just go over here search for Docker and you will find the docker extension by Microsoft do install it it's pretty great at the same time I have some other plugins installed okay I use it for my day-to-day activities so let's keep it aside dot net new okay so for the terminal if you don't have the terminal handy you can just click on this icon here it will open up the below taskbar and you can click on the terminal okay so that's how I opened it new terminal dot net new what do I want so I want a web API that's just no https so as you can see the scap folding happens scaffolding is nothing but like a template that.net has in its internal structure okay so as soon as you give the comment okay give me a web API project it will quickly create all the necessary controllers the API is the program.cs file some dummy file to get you started very quickly so as you can see we have this weather forecast okay it has some properties and uh you have this program dot CS file that is the starter file of this project okay and you have the controller weather forecast controller now what we'll do is we will run this program directly in docker so there are multiple ways to do it but will be since we already have this extension installed you have that luxury to Auto create all the stuff directly from the terminal point adding the next step is what we need to add the docker files to the project so that it gets executed normally if you don't have this Docker extension installed you have to go ahead and create this Docker file manually step by step and that's a hectic process so this Docker extension kind of removes all this overhead and gives you this plain interface where you can directly go and create this x Docker file you can create a Docker file you can create a Docker compose file it's up to you if you are using multiple containers Docker compose is the best way to go forward okay so let me open the command palette so I can type windows windows or command shift p okay now if you type Docker add Docker files to the workspace so what kind of application are we interested in creating so it is basically a dotnet asp.net application although it is not completely dotnet core but again dotnet6 has the base of dotnet core so it's good we can go ahead with this now what kind of system are we using since Max since I am using Mac we'll go it with Linux you can go it with Windows now what is the port that I wanted to be exposed in let me give some random Port it doesn't matter actually But whichever Port we are mentioning we have to access that Port 5010 I've given okay so here it is turning do you want to add a Docker compose file yeah let's do it so we we were able to generate this Docker file pretty quickly directly from the docker extension okay first it will what it will do it will pull the dotnet 6 base image as you see it has automatically detected which version of.net we are using or which is installed in our machine and it is downloading the respective.net version and it is setting the working directory to Dash app because by default everyone has some kind of every images will be having their own working directory if you go to the documentation of this you will be able to find what is the correct working directory and what port it is being exposed with now secondly the URLs so how do we access this web API so it's telling whatever is your localhost just add HTTP localhost and put 5010 it should do the job okay it is adding a default alt user so that it can run the basic commands Okay it's not required when you are going into the server mode this is there are two images involved here first image is is for hosting the application but the next image is for building the application so first what will happen is it will build the dotnet project it will extract the published files and it will put it to the base image how it works is it will now pull the dotnet SDK image for building the work directory is slash Source now it is what it is doing is it is copying the entire project content along with the Cs project and moving it to the directory then the second normally what we do first we do a dotnet restore so similarly the same steps are being repeated okay so first the Cs project got copied it did a.net restore so that the necessary plugins are installed then it copied the entire content of the project into the docker container okay so it is setting the work directory now now it is running dotnet build command Okay so it is building it in the release mode and the output is where app build now after building it it needs to publish it right so again so what it is doing it is running the same command in the same image itself it is running another command that is dotnet publish and the output folder will be again app slash publish now once the entire publish is completed all our files are present in the app published location the normal behavior right what do we do is we provide a published location then all the files are published over there then the files are being copied from that place and dumped into IAS or any other servers we are using so similar kind of job is being repeated over here so first we moved everything to publish then now we have all the code in our publish all the final dlls in the publish then we'll use the the image that we pulled in first right to host our dotnet application so what it's going to do is is going to pull this set this working directory again and it is going to copy from publish what does this mean is it's nothing but it's like a Alias name for the build so that we don't go ahead and forget what are these steps where it is coming from so every build or every dotnet ah image is given some kind of Alias name in the docker file okay so it is using this image okay if you see this image actually uses this one okay so internet rate is trying to pull the published location from here okay and then move it to the base base image is nothing but this one so to this image it is trying to copy all the necessary information so from the app publish it will take entire thing and copy it to the current location okay that is nothing but the app directory now we always have to start an entry point like how we'll get started so it executes this command called dotnet ms.net6dll so as soon as it starts it the app is started now there are various ways to run this project okay what you can do is you can directly right click on this build the image so as soon as it builds it it will start it will try to pull all this information from the internet it might take some time based on your internet speed where you are or have you already built it for earlier if you have already built it earlier then it will be pretty quick because all the image will be available in your Docker cache now if I go to my Docker images right you will see few new images got added that is Ms dot net the image is created it's not yet running okay so to build it so right now what we have done is we have just built this image right so if you go to this Docker extension you will find the images built but is it running that's the question okay so right now what you can do is right click run so if you run it now our image is running so how do you verify whether it is running or not so from the command line you can do Docker PS you will see there will be one image okay that is the dotnet image that is getting executed to verify it what we can do is we can again just open one more tab localhost 5010 enter yeah that's it so you see our API started responding so now we are already up and running with our Docker container so this is how you can easily set Docker container but I'll be showing you a few more steps so it's not that it is limited to this okay so first of all what I'll do is let me stop this container so as soon as I stop and I'll try to access this URL again so it cannot find this let's see the advantage of Docker compose so instead of running all this comma inside you can just write Docker compose if you write Docker compose up right it internally uses the docker file if we go into this Docker compose ml you see it is using this Docker file and exposing the same ports over here so it is making the steps little bit automated now we go over here enter it starts up now let's stop it let me go into a new terminal Docker compose down okay so now it is down so guys this is how you can easily get started with dotnet and Docker amazing
Info
Channel: Techno Saviour
Views: 1,404
Rating: undefined out of 5
Keywords: docker, .net6, asp 6, dotnet core docker, dotnet 6 docker, docker compose, vscode, .net sdk
Id: g7UXFeuD1GY
Channel Id: undefined
Length: 12min 9sec (729 seconds)
Published: Sat Oct 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.