Getting Started With .Net, Docker And Azure Container Instances

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there and welcome to a new exciting video on the cold wrinkles channel in this one we'll get started with net Docker and we'll see how we can use our Docker containers to run our applications locally but also how to deploy them to Azure container instances what we have here is a regular starter project with a.net 7 minimally API where we only have these hello world so nothing more than that but that's already enough for us to actually create a Docker container out from this and run it locally and deploy it to Azure container instances now the very first thing to do when we want to achieve this is to create a new file that we will call just simply a Docker file one important thing from my point of view is that we can go step by step through all the processes and through all the thought process that we need to put in when we want to create a Docker file for a.net application it also needs to be said that this is a very bare minimum of a Docker file that we can use to run an application obviously it can get more complicated than that however this is a getting started video so the thing is when we want to run.net in docker is that we will have to prepare several different steps in order for our container to run our application because we need to restore we need to build we need to publish and then finally we need to create the container itself it runs our application so we'll take it step by step in this multi-process or multi-step process of creating the docker file now first of all what we want to do is to using Docker this from and get the image for asp.net 7 because my application is running on.net server and this is we can use this Alias we can use this as and we call this base and we'll see a little bit later why this type of aliases are important because in some steps meanwhile we want to reference things that we have done previously and then this is an easy way to achieve that the next step is always to specify a working directory now when we create this type of container we need to specify this working directory so the docker knows exactly where we want to run the different comments that we want to run however right now we don't want to run anything in this container we just want to expose Paul a port 80 and this port will be the one through which we can get to talk to our.net application and the first step in this multi-step Docker configuration is actually done the next step is obviously to restore the nuget packages of our application and to build the application itself for that we have this new step in this process and for to achieve this we need the.net SDK and since we use version 7 we will also use version 7 here and we'll call this entire step build as previously we need to specify a working directory and in this case we'll call this working directory for this container Source SRC and now comes the fun part because what we need to do here is we need to copy the Cs profile that we have in our local file system in our destination working directory in a folder called docker.net which is the same name as our project now this copy comment is usually the most misunderstood because it is a little bit tricky because the first parameter that we have to specify here is usually or Target our local file system on our computer but then obviously what also we need to do here is the destination is usually a folder in the container itself and once we have copied the Cs profile we can simply run the.net CLI commands to restore the nuget packages for this CS Pros file after the nuget packages are restored we need to build everything but to build everything we'll need also the other files so here comes another tricky part where we'll still use the copy but in this case we'll use the dot and the dot now the dot usually refers to the source or or to the root of our Docker file or to our project on the file system which would be everything what we have in this folder and then the second dot refers to the root of the working directory which in this case is our source so we basically say that we you want to copy everything from here to our working directory in Docker once we do this we want to switch the working directory installed instead of source it should be stored docker.net and now what we can do here is simply run the dotnet build we say that we want to build a docker.net CS profile we want the configuration to be released and an important thing here we need to specify the output path where the build artifacts will be placed and this will be in the folder app.build and with this our second step in this process is completed and we can go to the third step which is publishing a.net application now to publish the.net application we will use this from but instead of specifying specifying a container you see that we use this build which means that from build which is this one that we have done earlier and we will call this publish and the only thing that we will need to do here is to simply just run.net publish we say that we want to publish docker.net cs plus configuration release and in this case the output directory should be app.publish now the final step in our entire process is to make everything run and to do this we will use this front base which is the initial step that we have provided here and in this case we will call this final here we need to specify once again the working directory so that we are clear that we are in the app directory of this specific container and then we use this copy once again but this time we see that we use this from and we specify that we want to copy the files from publish which is actually this one or what results from this step and from the publish we want to copy everything that it is in app.publish and you see that here app.publish is exactly the same folder as we specify here so it means that what we want to achieve is copy all the files that we have in this app.publish in the app directory basically of our base and final step here now the last thing that we need to do is to specify an entry point for our container which would be this docker.net dll which will be the result or this file will be part of the result in this app.publish that we have just copied over and that's basically it that's the entire dock Docker configuration that we need for this application now the next thing that we want to achieve is to run this container locally on our machine it goes without saying that in order to achieve this you need to have installed Docker desktop so you might want to go to the docker documentation and check what you need to install and how you need to install it depending on the operating systems that you are working on so once everything is done we can just simply open the terminal here and I'll make this bigger because we don't we are not interested in the docker 5 anymore and let's clear everything up so now what we want to achieve is we want to build a dockered image from this Docker file that we have just created in order to do this we can use this Docker and we can say build and we specify a tag and we want to call this image hello world and then we need to specify the file or where we can find exactly this Docker file and then we have to also add this dot at the end so that the docker context works correctly and we don't have any errors so now when we run this you will see that it will actually start to download and install everything to build a container according to the steps that we have described previously in the docker file so now this seems to be done let's clear the screen again and what we can do here is simply run Docker images and this will list all the images that we have right now and you can see that we have this hello world image that was created by me just now the next step is obviously we need to run a container based on this image and then we can check our web application to achieve this we will need to copy this image ID for the hello world now the next thing that we want to do is we want to run our image in a container do this we use this Docker run but there is one important thing here and we specify this Dash p which means or which reverse Port bindings so let me know about our container is that it listens on Port 80 but this is the port 80 of our container so if I want to run this image locally on my computer and be able to access it from a browser I need to configure a port binding from one of the ports of my machine to the port 80 of the docker container and to achieve this we just simply specify here a port from my machine to the port of the docker container and then we can simply use the image ID of the image that resulted from this Docker build so now we can hit enter and the container will start and as you can see we have this logging here which already shows us that the container is working and the application runs in a container so you can see that we can successfully connect to our application that runs in a Docker container from a browser and it means that everything just works fine when we develop it locally obviously the question is now how can we get our application or this specific container to run for instance in Azure container instances so for this next step we obviously go over to the Azure portal where I already have a resource Group where I want to create exactly the resource that I need for this goal there are actually two things that we need to consider here in order to be able to run my dockered image on an Azure container instances we need a way to publish the image somewhere now if you think about Docker Docker has something like a Docker registry which is the docker Hub which is a public registry so everybody can publish something to the docker Hub and then use it in for instance Azure container instances however usually what we do when we develop in a corporate environment is that we don't want to publish everything to the docker Hub we want to have a private container Registries so one service that we have enable for this is azure container registry and I will create this Azure container registry here and I will just click here your own create now we need some of your details that we need to go through here first of all we have this Resource Group which is okay then the registry name let's call it the code wrinkles hopefully it works yep it seems to be okay location is West Europe and I don't want to use availability zones and right now I will just use the really very basic Tire which doesn't allow me to for instance configure fqdns and things like that but for the scope or this video I think this should be okay now here if we go to next networking here there's nothing that we can do here there's also nothing that we can do so we can simply just review and create and simply click the create button and create this container registry now that's that it is created let me go to this resource and this is right now our registry now there is one thing that I want to do right now and by the way if you look in the official Azure documentation you will see that this step is actually not mentioned which will make you run into errors if you want to do this for the first time so the thing that you need to do here is when you want to publish and upload an image to this registry you need to have enabled this admin user so what we need to do here is we'll just have to click here admin user and then this would be successfully saved so this is a very important step if you want to publish something to this Azure container registry next there are a few other steps that we need to do and we will need to use the Azure CLI for that so once again if you don't have the Azure CLI installed go over to the documentation and see exactly how you can install the Azure CLI on your operating system I already have it installed so the first thing that we need to do is we need to use this Azure CLI to log into our Azure subscription and I will run this AZ login and this theoretically should prompt me with a window where I should log in and I will pick this account because this is where I have the subscription and now you see that login is actually successful and and I can simply close this coming back here we see that we have this output here with some information about our subscription let's clear this and now comes to the next thing that the official documentation actually lacks because it doesn't mention anything about this when it comes to how to provide or how to publish a Docker image to an Azure container instance but the thing is that besides logging into your Azure subscription you would also need to log in to your Azure container registry and for this use this AZ ACR which is a acronym for Azure container registry login then you specify the registry for which you want to actually log in so I will press enter and this will perform a login to my code wrinkles Azure ACR i o obviously the suffix is not needed and I see this result with login succeeded and now I have everything that I need to be able to actually publish my image that I have already created to my Azure container registry to achieve this I also need to create a tag first and I need this image ID once again so it would be kind of like this one so to achieve this I will run this Docker tag I will use the image ID specify these call wrinkles Azure ECR dot IO and then hello world this is the name of the application that I want to appear in my Azure container registry so let's just run this command the last step in this process is to use this Docker push command and it will push my tag to this or this image to the Azure container registry as you can see there's a lot going on here and when it is ready we'll switch over to the Aether Portal and check if my image is there now that the push has completed we can simply go here in the container registry to repositories and we can already see that we have this hello world image that is published so now that we have this Azure container registered with this image published it means that we can go to the next step and create an azure container instance that will run actually this specific image so here I am back to my Resource Group and I will click here create I will choose here Asian container instances and this container instances is really the first entry and here we'll just want to click on create so now that we have everything set up this is the resource Group and then we need a container name the container name let's call it hello world it will run in West Europe availability songs I don't want to have any and here when it comes to image stores you can simply use Azure container registry and since we have only one image we see that we already have this code green class registry and it already chose for us this image for hello world and the image tag latest and here we have the size for this specific container I'll leave it exactly how it is right now let's go here to the networking step here obviously what you can already see is that we have here a port binding which is the port 80 TCP so that we can access our container from outside or from a web browser or from any web browser anywhere around the world now the thing is that if you haven't configured your container to use port 80 but to use another Port you obviously need to configure that other Port here in order for you to be able to access your container let's just click on review and create and simply create the resource it will take a few minutes but we'll be right back and we will then check if everything is still working okay our hello world container instance is now created and you can see that you have this public IP address and theoretically right now based on this public IP address we can use a browser to actually access our application so let's open a new browser window and let's use this IP address and you can see that hello world so it means that we are now able to actually connect and make requests to our application that runs in an Azure container instance so that's basically it in this video we have learned how we can actually create a Docker file for a.net application and from the docker file create a Docker image and then we have seen how we can both run it locally that is very useful when we develop something using Docker we want to run everything then we can just simply run that specific image and then we have moved one step further because okay we don't only want to develop locally we want also to deploy our application and we have looked into the basic steps that we need to configure and to publish our Docker image to an Azure container registry and from there how we can use Azure container instances to actually run a container that runs our image and how we can then connect to our container and our application from the outside in our case via web browser but radically if you have an API right now you could even make requests via Postman and you would receive responses so everything would be working fine if you enjoyed this video don't forget to hit the Thumbs Up Button And subscribe to this channel if you didn't do this already and if you have any type of question would just want to get in touch with me just feel free to head over to the comment section and leave me a comment and I would be more than happy to get in touch with you this being said thank you very much for watching and until the next time I wish you the very best
Info
Channel: Codewrinkles
Views: 5,056
Rating: undefined out of 5
Keywords:
Id: oCCguBcLyIU
Channel Id: undefined
Length: 17min 13sec (1033 seconds)
Published: Sun Sep 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.