Containerize Your C# Application Easily with the .NET CLI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
containers are a great tool but putting your C application into a container might be intimidating setting up a Docker file can be complicated but now with the net command line you can create a Docker container without a Docker file I'll show you how in this 10-minute training video now for most of my training I work to give an in-depth perspective on technology including best practices and implementation details however sometimes you just need to get the quick introduction to the topic that's why I created this 10-minute training series and for this we're going to start with an application we built recently which is our background demo it's a uh a web API minimal API um setup that just has one endpoint and its messages let's just run it real quick so you can see it not in a container we're not doing container yet we go to messages try it out execute and you can see that it's been executing for about eight seconds and if I execute again there can see additional executions so that's the the setup that's the application we're going to start with now I do want to show you that Docker is installed and I have Docker desktop I have no images and I have no containers so just you know we'll we'll see some images pop up and we'll create a container for our application but for now there's nothing installed all right so that's our application that's what's set up now let's come over to the command line and this is in our project directory so in the same directory as our CSR which is right here um we have this okay so what I do is I'm GNA say net publish now it's already you know kind of filling all the stuff I need but I want to show you what to do so I'm on windows so because I'm on Windows I want to publish to a Linux container not a Windows container the windows containers are just there as a kind of a bridge to allow you get things into containers but really you want to use a lens container whenever possible so really the windows containers are basically for containerizing NET Framework applications so okay I want to say D- OS is Linux and then I want Das Dash Arc for architecture I want to say x64 and I'm going to say- P colon publish profile equals default container okay that's what set telling this uh system when you publish you want to publish to a default to the default container setup all right and I do want to say- C release meaning publish in release mode because when you're publishing you probably want the release bit which is the smaller version of your application doesn't have all the debugging things in it so that's the command to publish our container there is no Docker file in here notice on the right hand side here you don't see anything special about my API it's just a normal API we're going to publish it to a Linux container a 64bit l container and we're going to use a default container profile and we're going publish in release mode so that's going to figure out how to do that and it's going to publish our application and once it's done I know we're building top of the microsoft.com Donnet asp.net 8 version of container for base image so once it's done we will have an image in our our Docker desktop and there we go we have our background demo notice it's 221 megabytes you might say Tim that's a huge container don't worry we'll shrink it down a little bit later but for now that's just the basic container with everything you need the entire runtime for running our application which means we can say run here this is a kind of a shortcut notice again no containers yet but we hit run and it says optional settings yes we want optional settings we want to change the port to either a port we want to assign or zero for a randomly generated port number so that will map the whatever randomly generated Port you want to port 8080 okay so now it's going to start our application notice it mapped 32768 to port 8080 I'm going to open this up and when I do I see page can be found notice the URL is just the root but if I do slash messages then there's my data so now because the the way the uh container set up it's I believe it's UTC time that we're getting here not um a local time for my time zone but if I hit refresh here notice it goes a lot further because that background job is running so we've got our background jobs running in this container now why don't we have Swagger well because we published in release mode so release mode doesn't get Swagger we've turned that you know that's part of the the normal settings so we could change that but for now we're going to leave it alone now let's delete this container okay we still have the image back over here and we refresh that and notice 22121 megabytes now right now we're using the default for our um for our container but what we can do is come in here into the program.cs file we can add the container family and we can say Jammy chiseled and this is a different type of Base container it's much smaller which means much smaller images then you come back over here to the command line and we just hit that same command again we let it run it will build off of our our different base image notice the dash Jammy chiseled which is a smaller base image now I come over here to our Docker and we look at the background demo and it's 11322 megabytes it's about 100 megabytes smaller but yeah it's the same thing so if we were to launch this and come down here and say zero and run and launch this web app and let's bring it over here and say slash messages we get the messages and we refresh it it keeps updating a list so it still works and yet we've saved 100 megabytes because we changed the base image and we did that let's delete this we did that by changing our container family to this Jammy chisel so that's a really quick way to create containers you can run them locally or you can even then take those containers and publish them up to the a or container registry or to the docker container registry or to another container registry somewhere else and use them as the basis for a web app or something else there's a lot more configuration you could do one of the things you may see that yes would make this smaller is if you publish trimmed publish trimmed will cut out another 60 megabytes the problem is for my particular application because the fact that we are doing some um this is Json serialization even though it's implicit we're doing Json serialization and you've got to set some things up with Json serialization in order for the trimmed to work properly I don't want to go into that in this demo so we're not going to do trimmed but just note that if you set your app up properly for working well with trimmed then it will cut down on the amount of the runtime you have to publish with your application and so be a much smaller um potentially much smaller image if that's a really a concern for you okay but when it comes to publishing your image even if your image is larger you know so our image right now is 113 megabytes but the actual image that is our part of the application is much smaller than that which means that when we make changes to it it's just pushing that little change not the entire image again and so it really isn't nearly as bad as it might sound if you have 10 versions of this image so just note that there are a lot of benefits with Docker when it comes to image size so with that that's how to use Docker containers and put containerize yournet application especially a web application without having a Docker file you can have a Docker file if you want but this command line allows you to do that without having I let me clear the screen here and show it again this this command line allows you to publish your container very easily without a Docker file you can continue to tweak it and make it work for you but this is a whole lot easier than Ray a whole Docker file for situations where you don't need it okay that's it thanks for watching and as always I am Tim [Music] [Applause] [Music] [Applause] Cory
Info
Channel: IAmTimCorey
Views: 36,223
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6
Id: bg0QVTS4Q0c
Channel Id: undefined
Length: 9min 57sec (597 seconds)
Published: Mon Mar 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.