Dockerize Your .NET Application in 5 Minutes!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we'll dockerize a donet application we'll see how simple it is and how we can have it done in really just a few minutes if you're new to the channel then welcome my name is amihai and in this channel I talk about software architecture design patterns best practices things that you want to be familiar with if you're a software engineer this video is part seven in a series in which we're building a rest API completely from scratch using asp.net and following best practices Etc so if you want to stay up to date with the videos as they come out then make sure to smash the Subscribe button in any case this video can be watched Standalone from the overall Series so let's go ahead and dockerize our application now we aren't going to change anything other than creating a Docker file and in the docker file we're going to have three stages stage number one is going to be the build stage stage number two is going to be the publish stage and stage number three is going to be the Run stage in which we're actually going to run our application so the very first thing that we're going to do is we're going to use as the base image the microsoft.net 8 SDK so let's say over here MCR and then let's say microsoft.com sl. net SL SDK and the version that we want is8 and the name that we're giving this entire stage is build now what we want to do are two things number one we want to go ahead and restore the project and number two we want to build the project now we have only one project meaning that we need to only restore the packages listed in our CS project So currently we don't have anything but let's set it up anyway so what we want to do is we want to copy from the source and then one review and then we want to choose the Cs project and we want to copy this entire thing into the one review folder inside source now that we have copied the Cs Pro then we can go ahead and run net restore so let's say run.net restore and let's specify the path which is one review slash this thing over here and once we are done with this then we can go ahead and build our application and the reason why we're separating it is because now we won't need to run the net restore unless we change one of the packages in our CS project so as long as the Cs project stays the same we won't run net restore again build on the other hand we want to run when any file changes not just the Cs project so let's go ahead and copy now all the rest of the files into the one review folder then we can go ahead and actually build the product so we can say net build and we can specify the path to the Cs Pro which is still the same thing so we're in source so we're going to say one review1 review. CSR we also want to say that the configuration is released and that the output directory is slapp SLB build now real quick before we continue I want to remind you that if you want to join our amazing amazing Discord Community then you can do it for the price of a cup of coffee instead of buying your the next cup of coffee you should invest in my patreon and then not only will you join our Discord server but also you'll get access to the source code of this video and every other video on this channel enough self-promotion now back to the video okay moving on to publish so over here let's say from build as publish and this will be simply let's move actually to to Source SL1 riew so let say Source One riew and we can actually move this entire thing over here and then simply say one review over here and now that we're in the one review folder then we can simply say run. netp publish and let's actually copy this entire thing this is going to be net publish and we're going to be publishing one review to app SLP publish that's it for publishing so we have our artifact in the publish folder and now all we need to do is go ahead and run it so what we want to do is we actually want to take only the asp.net runtime over here we don't need the actual SDK so we can go ahead and say SP net and basically what we want to do is we want to run the artifacts that we have inside the publish folder so let's go ahead and say over here that the work deer is now SLA and also let's go ahead and copy from the publish stage we want to copy SLA SL publish to here so now overall we have inside the app folder we have the publish artifact finally we want to go ahead and say that the entry point isnet and then our dll which is one review. dll the last thing we still want to do is we want to expose our application to listen on a specific Port so the port that we want is going to be 5,000 And1 but of course choose whatever you want and we also need to tell asp.net that our application is going to be listening on the sport to do this what we want to do is to set the environment variable which is called spet core HTTP ports we want to set this to be 50001 and now our application will listen on this port we're exposing this port and then all we need to do is to send requests to this port and hopefully it'll arrive at our application so if we got this right then we should be able to say simply net build and let's give it the do tag let's say one review YouTube and the docker file is where we're at so I'm simply going to say dot and let's run this and see if everything works as we expect and of course I have here a small typo so this needs to be a DOT and also I copy pasted so this also needs to be a DOT so let's try this again right so now if we look at our images and we search for we said something YouTube then we can see that here we have our image so let's go ahead and now say Docker run and on error machine then we want to access port 50001 and we want this to be mapped to the port in the container which is 51 let's also give this the name let's say one review API YouTube and the image that we're using is the one review YouTube now I'm purposely not running this in detached mode because if there are errors then I want to see them over here so if this works successfully then we'll run it in detached mode so it seems like everything is running successfully let's go ahead and try making a create product request so we now have the host a bit different than what we had it before so let's go ahead and update this to be 5,1 and make sure that we're using the correct host great and now let's make a request and seems like it was created successfully using the location header of the response so I'm reminding you again let me make things a bit smaller remind you again that over here we have a request based on the location header which gives us the location of the newly created resource so let's go ahead and make a get request and see if this works successfully and seems like everything is working into end as we expect so as you can see this was really simple we don't have a lot going on over here and of course the application is still pretty small but as you can see adding Docker support is extremely simple now in the next video then we're going to be adding a Docker compost file because I want us to be able to say Docker compose up and have both our application and a postgress database both start and that way we'll have seamless integration between our application and the SQL server and then we can start talking to a real database so if that sounds interesting make sure to smash the Subscribe button and I'll see you there
Info
Channel: Amichai Mantinband
Views: 8,886
Rating: undefined out of 5
Keywords: c#, software engineering, .net, c# tutorial, asp.net, aspnetcore, aspnet, asp.net 8, aspnet 8, asp.net core 8, .net 8, dotnet 8, asp dotnet 8, dotnet tutorial, .net tutorial, docker, dockerizing, docker support, docker .net app, dockerizing .net, docker run, docker tutorial, docker asp.net, asp.net docker support, dockerizing tutorial, dockerfile, docker stage, mcr.microsoft.com/dotnet/sdk
Id: yQtgY4VG3kM
Channel Id: undefined
Length: 7min 41sec (461 seconds)
Published: Mon Jun 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.