Dockerize .NET 6 in 10 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this episode we're going to look at how easy it is to containerize a.net 6 web api [Music] hey everyone wes here with productivedev i hope you're doing well in this episode we're going to pick up where we left off in episode 1 when we created a new.net 6 web api from scratch using test driven development and we're going to take this project a step further by containerizing it using docker so we're going to look step by step at creating a new docker file i'll show you everything that you need to know to containerize your application and what each line of the docker file that we'll write means then we'll build our image and we'll look at tagging our image and then we'll run it from our development machine so containerizing our.net 6 web api is going to make it much easier to orchestrate various web api workloads when we talk about deploying this api to the cloud so if you'd like to see how we built this.net 6 web api from scratch be sure to check out episode 1 otherwise if you'd like access to the source code you can support the channel all patrons of the channel via patreon get full access to all of the source code that's developed in every productive dev episode so with that let's get started containerizing our application okay so the application that we're going to be containerizing in this episode is a simple.net web api built in.net 6 that we built in episode 1. so the main idea behind this particular project is that we have an api with a single endpoint here where we make a get request to essentially return us a list of users and so this comes back to our clients as json and the way it kind of works here is that we reach into the service layer which has an http client on it and this client is used to make a request out to a third party service with a configurable endpoint so we make a get request out to that service which returns to us a list of users that we serialize into our own class so we have a user object here or user class and we serialize the list of these objects from the json that we get back and effectively we just proxy that back to our own client so the controller gets this list of users and if any are found we return a 200 ok response with that json list of users otherwise we return a 404 so if you'd like to build this application from scratch before you get started be sure to check out episode 1 productive dev episode 1 and we built this using tdd so you'll also see how we built out all the unit tests for this simple project otherwise if you'd just like access to the source code then as a patron of the channel you can get access to the source code for this project and anything that we build on this channel so your support would be much appreciated if you're interested in becoming a patron there's a link in the description for that okay so to containerize this application what we need to do is to create a docker file and so i'm going to go to the folder view here in visual studio and we're going to create a docker file in the root of our project so i'm going to right click and we're going to add a new file called docker file so we're going to use a multi-stage build here meaning that we're going to have a build stage in our case and we're going to have what we'll call a serve stage so this is going to keep our build pretty efficient we're going to try to keep the container or the image as small as possible and so we're going to have an initial base image that uses the.net sdk to build our binaries and then we will use a base image with the.net runtime which would be much lighter weight to just serve up those built artifacts so for a base image here we're going to use a microsoft base image so this is the.net sdk version 6 focal and focal is just ubuntu 20.04 and we're going to call this the build stage next we need to create a working directory here so we can do that with the workdir directive and we're going to set that to be forward slash source next we're going to copy everything from our current project directory into that working directory with the command copy dot dot so copy everything from this directory into our current working directory and now we need to run two.net commands basically we need a command to restore all of our project dependencies so we can run dot net restore and we'll point this to the relative path cloudcustomers.api forward slash cloudcustomers.api.cspraj and we're going to use the disable parallel flag here finally for this stage we need to actually publish our artifacts to an output directory and so the way that we do that is with the net publish command again pointing to the relative path of our cs praj file we're setting the configuration to release and we're setting the output directory to be this forward slash app which will get created and we'll use the no restore flag here so all of the binaries or the artifacts for this will end up in an app directory so that's really all the work we need to do with the sdk so what we're going to do for the serv stage is to use the.net runtime and we can again use a microsoft base image here asp.net 6 focal so this is the net 6 runtime running on ubuntu 20.04 we're going to create a new working directory for this part of the build called app and now we just need to copy all of the artifacts that landed in the output directory from the build stage into our current working directory and so we can do that with the command copy and we can use this flag from build and so this is referring to what we named any particular stage in our docker file and so we're copying from the build stage from this app directory everything into our current working directory which is forward slash app for this stage next we're going to expose port 5000 by default that's where we'll handle requests and now we're going to set an entry point for the application which will be this command.net and then cloudcustomers.api.dll which is the core artifact that we're concerned with this will be the built file the binary that basically just runs our dotnet api so in the container when dot net cloud customers.api.dll is run our application will start up okay so that's really all we need to do in terms of a docker file for this particular project let's go ahead and see how we can actually build this file now okay so i've got windows terminal open here we're in our project directory and as usual i'm using git bash as a terminal emulator here we need to run a docker build command to actually read that docker file that we just created and build it so we can do that using the command docker build we're going to use the flag dash rm to remove intermediate images and we're going to tag our image in my case i'm going to call it productivedev and we're going to call the app cloud customers latest and then we need to say we're building in this directory this is the directory that contains our docker file so this will take some time if you don't have the base images what's going to happen is docker is going to go out and download those base images the microsoft.net sdk and the runtime and it's going to go step by step through our docker file to actually create an image for us first of all you can check that you have it so what i'll do is i'll run the command docker image ls and we'll pipe that to grep and look for the string cloud customers you can look for this you know any substring that is part of you know the tag the name of the image that you just tagged you can see that just a few seconds ago we created this image and it's currently about 213 megs in size okay so we're going to go ahead and run our container now for this we're going to use the docker run command and we're going to basically map ports 5000 from our host into 5000 on the container and we're going to do the same thing for ports 5001. you could map to any open port that you prefer here we're going to set a couple of environment variables and one way we can do that when we want to set environment variables at runtime like this is with dash e and i'm going to set asp.net core http port to https four slash forward slash plus colon 5001. so the base image that we're using is actually setting this by default to port 443 and port 80 i believe so we're going to control this by overriding it and setting the asp.net core http port to this https 5001 and we're going to set a second variable here asp.net core urls to hd http forward slash forward slash plus colon 5000 and we're going to run our image so that will be productive dev forward slash cloud customers okay so you can see that this is logs that we're getting out here they're not pretty but we can see them they're kind of in json format here and we can see that it's listening on port 5000 so let's open up our browser and look at port 5000 okay so when we visit localhost 5000 forward slash users we get our data out to the screen here and in fact if we put the browser open with the terminal we can actually see when we make requests here to this endpoint we're getting logs out from our docker container so things seem to be working nicely i'd like to add a feature here for a health check mainly because it's really useful to have health checks when you're containerizing applications as an endpoint that you can you know automate requests to to ensure that a particular service is running as expected it's also super easy to set up a bare bones health check in a.net api so let's look at how to do that now i'm going to stop the container and we're going to head back into visual studio okay what we'll do now is we'll head into our api project and we're going to open up program.cs here as we talked about in episode 1 with net 6 we have a new convention we can utilize where we have just a single program.cs as opposed to having a startup.cs and program.cs things get kind of consolidated here and if we want to add a health check it's actually just a two line two line change for us here to add a very simple health check so what we want to do is just before we build the app let's call on our builder here and call services and we're going to add health checks okay so that's really all we need to do to add health checks but let's map them to a specific endpoint let's call app dot map health checks and we're going to map this to forward slash health okay that is literally all we have to do to set up a basic health check now since we have a change to our code we need to rebuild our image for this and we can check it by restarting our container so let's head back to the terminal all right let's see if our container is still running and it is let's go ahead and stop it so we'll say docker container stop 665 and now if we refresh our page here by the way 665 was just coming from the container id so you can use just the first few sort of the prefix of this if you if you'd like to stop it if we refresh now basically there's nothing running on port 5000 anymore and so this is just going to timeout or be unable to connect so since we made a change to our code all we really need to do is to run our docker build command again so let's go ahead and run it again and we'll tag you with latest okay so now let's run our image again all right so things are running let's refresh the page we can see that looks good and if we want to hit our health check endpoint it's just going to be forward slash health and we can see we get a healthy response here so this will be a nice convenience and we'll look at how we can use this when we're load balancing different services or orchestrating services with various orchestration tools in other videos on productive div okay so that's about all there is to doing a very basic containerization of a.net 6 web api thanks for watching and if you got anything out of this video i'd really appreciate it if you liked and subscribed and if you have any questions leave a comment below i'll see you next time
Info
Channel: Wes Doyle
Views: 48,793
Rating: undefined out of 5
Keywords: dotnet 6, dotnet 6 web api, docker, devops, dockerize .net, dockerize api, .net tdd, containerize .net, containerization, cicd, ci/cd
Id: 3s-RfwvijpY
Channel Id: undefined
Length: 13min 33sec (813 seconds)
Published: Sun Feb 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.