Step by step guide on utilising Docker Compose with Asp.Net Core, SQL Server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends thank you for watching this video i am muhammad and i will be showing you today how to create a docker compose file for an asp.net core application and the sql server database utilizing the entity framework code first approach we will be creating a simple bookkeeping application where we'll be able to add and view books i will be utilizing docker compose as it allows us to orchestrate multiple containers in a streamlined way the two containers will be our sql server database and our asp.net coil application the four things that we will need before we start is visual studio code dot net core sdk docker as well as the docker extension for visual studio code the four things that we'll need before we start is an ide dot net core sdk docker as well as an extension for visual studio code so we're going to start by our sdk which is going to be dotnet core once we go to that link all of the links will be available in the description down below we have to click on download.net sdk we're going to be using the net 5.0 as it's the recommended one it's a very simple installation process and once we finish with that we're gonna be downloading our ide our ide is gonna be visual studio code it's a simple as well download once we download it we need to install it the installation process is very simple and minimal as i have already downloaded all of these before i will not be doing it again similarly we need to download our docker application it's a very simple installation process once we download it it will take roughly around few minutes to be installed once it's completed we can see we have the docker icon on our top right hand side and we can see once we have everything running within docker we're gonna see docker desktop is running and the last thing that we're gonna be needing is an extension for visual studio code which is going to be a docker extension which is going to give us a lot of ability to debug and view what's actually going on when we're on our local containers so after installing all of our requirement we need to make sure that our.net sdk has been installed successfully in order for us to do that what we need to do is we need to open the terminal by clicking on command space bar on windows and typing terminal and once we are in our terminal what we need to do is we need to type nut dash dash version and as we can see we see the net version number which is 5.04.102 so this mean we are on the latest version of the net sdk if you have any other number it could be 5.0.0 3.1 any version or in the future it could be 5.1 which is going to be fine if you see any other message other than the number then you have an issue with your sdk then you need to redownload the sdk and reinstall it again so before we start as well we need to make sure that our entity framework tool is available for us to use for that we're going to be using the nut tool install dash dash global because it's going to be available globally within our os and we're going to put dotnet ef i have already installed this one but if you didn't it's going to take roughly around 10 seconds to be installed once we have uh all of these requirements set and ready what we need to do is to create our application right now so for us to do that we're going to be using the new keyword we're going to put dot nut new mvc we're going to give it a name of book management and we're gonna give it the line c-sharp we don't really have to do that but i like to do it for consistency and we're not gonna use any authentication if you're interested in authentication within asp.net core i have another video which i can link within the cards on the top right hand side or the left hand side and i will leave a description the link in the description down below so once we do that we click on enter and we can see our we have an issue oh i have put an m instead of an n for the name now after we click on enter we can see our net sdk is building the application for us which is great so once we have this and ready all we need to do is just open our code editor which is going to be visual studio so now after the ide has been open all we need to do is we need to add the terminal directly so we need to click on view and click on terminal and we can see our terminal has shown up here so the first thing we need to do is we need to navigate to our folder so we need to type cd book management and then once we are here we need to build the application make sure it's building successfully and as we can see it's building and after a few seconds it will give us the build succeed so the next step is we need to add all of our packages so we have basically three packages that we'd like to add in order for us to utilize the entity framework so the first thing is we need to type not add package microsoft dot entity framework core this is going to take a few seconds to install then we need to install our second package dotnet add package microsoft dot entity framework core dot sql server since we're going to be utilizing sql server and the last one is going to be dotnet add package microsoft dot entity framework core design perfect so now we have our three packages in order for us to verify that all we need to do is we can go to the left hand side open our project and we can click on bookmanagement.css browse and we can see we have the three references available here for us which is great so the next step is we need to build the application underneath to make sure that everything is running smoothly so for us for for in order for us to do that we're going to be using dotnet build to build the application and then we're going to be using dotnet run and we can see the application is running on port 5000 for http and 5001 for https so let's go to our browser and let's navigate to http localhost 5000 and we can see our application is running as it should be which is great so right now after we see our application is running as it should be what we need to do is we need to add our docker file our docker file will be added on our root directory and for us to do that let's first navigate back to our editor let's stop the application and then within our application directory we'll add a new file we'll call it docker file and the d has to be capital when when creating this file or it will not be correct because it's case sensitive so once we create our docker file what we need to do is we are gonna add our commands i will not be delving deep into every command what does it mean but i'll give a brief explanation so the first thing that we're gonna do is we're gonna be adding the from keyword and basically this is going to tell docker where to pull in the main image that we're going to be using so the first image is going to be the microsoft.net sdk forward slash dotnet forward slash sdk column 5.0 and we're going to name this as our build environment so the second thing is we're going to be defining the work directory and we're going to name it up after that we're going to be copying our cs pros file from our local environment to our new container so we're going to call it we're going to call the copy keywords and then we're going to put star because we don't want to identify every cs browser independently and then put dot cs bros and then we'll put dot forward slash it means it's gonna copy it to the root directory of our container and then we're gonna use the wrong keyword in order for us to use dotnet restore so all of the packages will be restored after that we're gonna be copying the rest of the code so i'll put copy dot space dot so we're gonna copy everything from our root directory into the root directory of our container and then we're gonna put run dot not basically we're gonna publish this application publish dash see we're gonna release it so it's gonna be release and we're gonna specify the output after we finish from that now we're gonna finalize our production image we're gonna call it from again mcr dot microsoft dot com forward slash dot not forward slash sdk 5.0 we're going to specify a new work directory for this image we're going to call it as well up and the last thing is before sorry before last we're gonna copy everything from the build environment to our new environment so from equal build environment to our app forward slash out to our root directory and the last thing is our entry point was the command it's gonna be running once this container is up and running we'll put dotnet and we're gonna be named taking the name of tadialal which is gonna be basically book management we can check it from here okay great let's save this an extra step that we can do in order for us to ease our development process is to specify a specific port so our application will run so in order for us to do that we need to go to the program.ts and within our web builder we just type web builder dot use urls we specify which url application will run on and we'll just use http it doesn't matter the domain and we'll run it on port 80 and we'll update our docker file so we'll open port 80 directly from there so we'll just type expose our port 80 and that's it so once we have this saved what we can do is we can start building our container so how do we do that oh sorry build our image so with in the command prompt with docker build dash d and we'll give it a name book management and we space and then we put dot we have to put the dot because that tells it where to find the actual docker file in order for it to build and we click on enter it's going to take a few seconds to build as we can see double the process is running and it has built successfully because at the end it has created the image for us which is great right now in order for us to test if this docker image is running what we can do is we can type docker run let's specify which port are we gonna connect with so we'll put port 80 80 and we're going to connect to the internal port 80 and we'll put call it book management and we click on enter and now we can see the application is running on port 80. on the inside and port 8080 on the outside so now let's try to connect and now let's change the port to port 8080 and now we should see the application running which is great so in order for us to just to summarize what we did so far is uh we created the network application it's a very simple application we just updated our startup class as well as we created the docker file uh the docker file basically just took the application and published it in a environment container and then it copy everything to another container we specified which url is going to be using within that application and once we did that we tested it out and we can see that the application is running successfully which is great right now let's stop this the next step for us is to add our models so in order for us to add our models we go to the models folder within our root directory and we add a new class let's call this new class book and this book is going to be very a book class going to be very simple all is going to contain is two properties the first one is going to be an id of type and and the second one is going to be a name perfect so once we do that the next step for us is to create our application db context so what we do is within our root directory we click create a new folder we'll call it data within this data folder we create a new class we'll call it application db context once we have that class created we then directly inherit from the db context class basically once we inherit from the db context class our application db context will become a prese representation of our database with an ndd framework so let's fix the references perfect the next step for us is we need to declare our data set so public dbsat and we'll put book basically this will allow us to create a table in our database called book and we'll name this table box we'll fix the references and the last thing that we need to do is to define our constructor so the constructor is gonna be db context options we're gonna define the application db context options and then we're gonna send this to our base class we are inheriting from the dbcontext class we need to send the dbcontext option directly to the base class so once we do that we need to update right now our startup.cs in order for it to take our connection string so let's go to the startup.cs class and inside our configure services we're going to first define our configurations so we're going to be defining the server the port the user the password as well as our database so first of all let's define our server we're gonna take it from the configuration and we're gonna be calling it database server the second step is we can define the port database port and in case there's nothing within the configuration we just return an empty string the third is our user so far we'll call it user equal configuration database user similarly if it's empty will return an empty string password configuration database password and finally we need to define the database so called far database equal configuration database name and if it's empty write an empty string after we have defined our variables right now what we need to do is we need to create our db contacts and injected into our services so for us to do that we'll just type services dot add db context we'll put application db context this is the name of the class that we have created we can fix the references before we continue now we need to create the options and then we can define these options so basically within these options we're going to be telling sql server we're going to be telling our application db contacts what type of server database server we're going to be using so it's going to be use sql server and finally here we're going to have our connection string so let's fix the references it's use not user okay and now great now let's create our connection string so we'll put var connection string equal and now what we're going to do is we're going to be concatenating all of those things together so in order for us to do that first we're going to put server equal we'll put the server and then we have to define the port after that we're gonna use the initial catalog to define our database and then we're gonna give the database name after that's gonna be our user id user it's gonna be user sorry and lastly we're gonna be defining our password equal password once we have this ready we just put this inside here so this connection string will be built using the variables that we have sent here and after that it will be passed to our sql server and then it will be passed to our application db context which is great so now just to make sure that everything is still running let's put dotnet build perfect application is succeed the build succeeded the next step for us is we need to create our initial migration so how do we do that it's very simple we're going to be using the ef framework within.not so we're going to put dot nut eff data migrations and we're going to call it initial migration it's going to be the first migration that we do once it's ready we can see on the left hand side we're going to be having a new folder which is called migration and this folder basically it's going to have the new table that we have created and this code by itself it's going to be converted into sql and then it's going to be applied to our database so great right now just to have a quick summary we have built our application we have created our application db contacts we hooked up the connection string and we created our first migration but we're not going to be directly migrating these changes to our database why because we're going to be using sql server within a docker container in order to have it as our database server since that container is not running we cannot really do any migration so in order for us to facilitate the entire process of automatically migrating all of the scripts to the database what we're going to be doing is we're going to be creating a database management service which is going to be hooking up to our startup class in order for it to be responsible to apply all the migration that happens directly to our database so for us to do that what what we're going to do is we're going to create a new folder we're going to call it services inside the root directory of the application so we'll call it services and inside the services folder we create a new class we'll call it database management service and let's make this class static and then we'll have a method inside this class we'll call it public static it's gonna be void and we're gonna call it migration initialization and we're gonna pass the i application builder okay great so let's fix the references first after we do that we're gonna just basically initiate a scope from that application builder and then we're using that scope that we have created we're gonna be initiating an application db context and then use entity framework core in order for us to ensure that all of the migration has been applied so it's a very simple piece of code and let's see how we do that so we're going to be typing using var we're going to name a service scope and we're going to be using the app dot application services dot create scope the create scope here we have to fix the references because it's going to be referring to the asp.net dependency injection so once we do that it's going to be as simple as referencing to the service scope that we have created i'm gonna go to service provider we're gonna get which service we're gonna initiate and we're gonna be it's gonna be the application db contacts let's fix the references okay great and then we have to initiate it and then what we're gonna do is we're gonna put database and then we're just gonna type migrate and then we're gonna close it and this is it let's fix the references first so what does this mean so just to summarize this is a static class that we're going to inject into our startup class in order to ensure that the database entity framework will automatically update the database with all of the migration scripts that we have so the first thing that we did is we passed the i application builder was going to be sending from the startup class we created a scope the scope basically is going to be utilizing the application services and then it's going to be creating like a an object from those services for us to utilize once that service scope has been created we're going to be choosing the service providers in our case right now we're going to be using the application db context and then from here on we're going to be connecting to our database and once we have connected to our database we'll be utilizing the entity framework core in order for us to migrate all of the changes directly to the database perfect so the next step for us is to go to the startup class and inside the configure method on top all we gonna type is database management service let's fix the references and then we're going to call basically the only method that exists it's going to be migrate initialization and we're gonna be putting the app passing the app which is the iweb host environment sorry the eye application builder great so once we do that our next step basically is just to continue working on our controllers and have basically our book controller which is going to be using to create a book to view all of the books and after that we're going to be creating the views once we do that we're gonna jump into the exciting stuff of how to create a docker compose file and how to stitch everything together so the first thing that we're gonna be doing is inside the controllers folder we're going to create a new class we're going to call it books controller sorry we're gonna inherit from the controllers class perfect once we do that what we need to do is we're gonna be initializing our application db context so for us to do that we're gonna put private i'm gonna make it read only application db context and we're gonna name it context and then we're gonna define the constructor we're gonna be sending application db context context context equal context which is great now it's going to be simple as defining three actions the first one is going to be public i action results it's going to called index we're going to get all of the books so we're gonna create a new variable call it books equal underscore context dot box dot to list this simple function is gonna connect our database we're gonna go to the box table and then it's gonna create a list using the system.link and then we're just gonna return it to the view and we're gonna return the box to the view as well very simple stuff the second part is we're gonna be creating our create so we're gonna put the attribute get public i action result create and then put return view and the last action is gonna be also create but we're gonna be sending back which is gonna be a post so we're gonna put http post public it's kind of let's make it async let's put a task i'm going to return an i action result and we'll call it create and we're gonna be sending back a book call it book perfect and now let's just fix those references so the first thing is the task we're gonna be using system threading and book we're gonna be using the models perfect so what shall we do now for the first thing is once we wanna do a post uh we're gonna verify the model state that we have so it's going to be simple if model state dot is valid if it's valid we'll continue if not we just return to the view and we're gonna send back the object.cutscent and let's add a nice error message model state dot add model error gonna be string dot empty for now this is a very simple implementation we can delve more into deep into later videos if you guys want that but something went wrong and then if we all see good let's create a try catch because we're gonna be communicating with a database put exception and let's put model state dot add model error also string dot empty and it's gonna be just gonna send back the error we'll put something went wrong and we just passed the message and now let's fix the references that we have for the exception we're going to be using system and then we're just going to create the database code to order for us to save which is going to be basically just calling the context going to the box and book and then it's going to be just have to so basically once we tried underscore contacts books dot add it's going to take this object and prepare it to be saved in our database so it's going to be saving it into memory and in order for us to comment those chains we need to first await because it's going to be writing to the database context dot save changes async and that's it what we need to do is we want to redirect back to the index page once this is successful redirect to action and just redirect to the index page and that's it now we have our model sorry our controller ready and everything is there let's save everything that we're using and then let's build this application to make sure everything is still building okay perfect now we can see the application is building successfully so the next step after that is we need to create the views so within those view the view folders we're going to create a new folder we're going to name it books the first thing is gonna we're gonna create a new view so new file we'll call it index dot cs html and the view for that is gonna be very simple we're gonna be adding an ashraf to the create action in order for us to be able to create a book and we're gonna call it books and we'll call it create a book and let's give it a nice class it's gonna be class btn btn dash success for example and after that we're gonna create a simple ul unordered list and we're going to loop at the model i wouldn't define the model so let's define the model we're going to be basically this view is going to be handling uh a list of books so it's going to be as simple as add model we're going to define a list and list of books so it's going to be book management dot models dot book perfect and now we'll just loop on that model so for each we call it book and it's gonna be model okay great and then for each we can define an li and we're just gonna print the name so book dot name okay great so that's the index the next step is the create view so basically we just right click here new file we'll call it create dot cs html and it's going to be a simple first let's define the model before we forgot so it's going to be bookmanagement dot models dot book and then it's going to be a simple form asp action equal create and then asp dash controller equal books and then we're going to define the method which is going to be post because it's going to be posting the book to the controller after that we're just going to find a div class row and then within that row we're going to define a div we're just using the bootstrap default classes class call md-12 after that we're just to be creating another div which is going to be hosting our input so div class it's going to be form group because we're going to be grouping a label anna a label as well as an input field okay and then within the step we're just gonna use a label and this label we're gonna call it book name perfect and then we're gonna be using input asp4 we're gonna put it for the name itself and then it's gonna be of type text and let's give it a class it's gonna just be form control and i think that's more than enough or yeah let's go for the placeholder we'll call it please enter book name okay great so once we do that and we have it all set since we are returning error messages in case something went wrong so let's create another div or call we give it a class form group as well and it's going to be as simple as creating an inside div asp.validation summary equal all which means that we're going to return all of the issues that the controller is going to throw back and we're going to give it a class of text danger it's going to give it some nice format and some nice colors in case something goes wrong and the last thing is we're just going to have to create buttons in order for us to submit so we create a div we'll just add a button of type submit so it will submit the form and give it a class of btn6s so it will be green and we'll name it to create a book the last thing we want to do is the simple redirection if one i cancelled so a asb does action equal index so we're going to go back to the index in case we don't wanna if we're gonna cancel this so it's gonna be a book and the control is gonna be asp dash controller we're going to be referring back to the box one and we'll give it a class of btn and we'll call it cancer okay great so basically this finalize almost all of the changes that we need to do our application so to do a quick summary once we have created our application db contacts we just created our controllers and we created three action was gonna be the index to show all of the all of the books created a gut for the key for the create so it will give us an empty view and then we created a post where going to be processing all of the requ processing the request that we have made which is that we're going to be adding a book the last thing that i would like to add is just a simple uh url on the layout page so we can click directly and go to the page of books management so we just copy this and instead of home we just call it book and action is going to be index and we'll name this href books perfect this is it now we get to the exciting part which is going to be creating our docker compose file so the docker compose file is it's a yaml file which is are going to allow us to create multiple images and build them in order for multiple images and it allows us to build them and run those containers together so how do we do that in our root directory very simply we create a new file and this file we're going to be called docker all lowercase letter dash compose dot y ml the ym and it's going to define the extension of a ml file so the first thing we need to do is we need to define the version of this tml file and we're going to be using the three version so after we define our version the next step is within the cml file what is it going to do we need to define services so in order for us to do that so basically the application is a service the database server is a service if we have for example radius caching it's another service so the first thing in our email file we define the version and then we define the services then we're going to put define our database servers because has to be created before the application we're going to call this ms sql dash server and be careful in the yaml file indentation is really important so as you can see here in order to define a service within the indentation and then after that i have to do another one so it's gonna be image and we're gonna be calling the sql server image from docker hub so mcr microsoft dot com forward slash amazon sql forward slash server 2017 we're gonna be using the latest one and we're gonna be using the ubuntu version ubuntu perfect so after that this is going to be the images and in order for us to utilize this we need to pass some arguments so how do we pass the arguments in order for us to utilize it we're going to have to create an environment attributes environment and again the tab because it needs to be consistent with the spacing basically the requirement for sql server is going to be first accept the iola so we're going to put yes we accept it the second one is we're gonna define the sa password okay so it's really really bad to it's not at all recommended to uh defined as a password in a plain text within our docker compose but since this is gonna be uh only for demoing the abilities of docker compose it's fine for the scenario but usually it's it's not it's frowned upon it's really not good practice to put the password here so for now we're just gonna be using it and we're gonna put it as docker 2021 and the last thing is we're gonna be defining the which version of sql server we want so we're going to put amazon sql underscore pid and then we're going to be using the express version perfect so after we define the image after we define the environment what should we define we shouldn't define which supports are we going to be connecting for this reason uh for this demo we're going to be using the defaults one which comes with amazing sql which is gonna be the one four three three and one four four three three it's gonna be very easy so basically we're just mapping from the internal to the internal ports external to the internal ports on the same port which is 1433. so that's finalized our first service again we need to vape to pay very good attention to the tabbing the second service is going to be our box application so we're gonna call it book dash app and that's what we can do is we can define directly again the build variables here but if we just type build space dot it will automatically pick up the docker file from our root directory which is what we want instead of free repeating the code and the last thing we want is we want to define the ports of this application similarly what we'll do if we're gonna run it within the command prompt so we're just gonna be connecting let's use port 8090 and we'll connect to port 80 because we have already defined port 80 from the inside within the startup class outside the programming class if you remember okay great so once we do that let's make sure we save it and the next step for us is to use how how are we gonna run this so basically in order for us a single to run a single docker image we use docker weld we gave it a tag and then we put the we put a dot and this build the image for us and then we use docker run using that name and the ports in order for us to run but how are we going to use the docker compose it's very easy using the cli that is provided for us what we can do is we can just type not sorry docker dash compose and we're gonna put up it means gonna when we put the word up basically it's gonna initiate everything within the docker file so it's gonna make all of our services currently we have only two services it's gonna run them and then it's gonna run the container so it's gonna build the images and then it's gonna run the containers so this what does docker does compose up means so just to make sure that all of our images are downloaded and rebuilt so nothing is cache i would like always to pass the dash dash build flag this will force docker compose to rebuild the images for us and we click on enter usually the first time it could cause us some errors until everything is initiated in case it does that all we need to do is restart it again and we're still seeing the docker is running it has initiated the database it's trying to build the application and right now we have to wait and we can see it has failed let's try to stop it and run it again okay still finding y which is very good there's a very good explanation for us for it why it's failing because if you remember within our startup class we haven't defined these variables and it's all going to be empty empty empty empty so how do we define these values from within the docker compose file so we can fix this issue which is going to be very simple below the build environment we're just going to just type environment again and then we're gonna define our configuration directly from here so if we go to the startup class we know we have database server so let's take this don't forget the indentation and then we're gonna call this the same name of our service here so it is gonna be quotation ms sql dash server perfect what's the second thing which is gonna be the database port the database port is gonna be as we have configured here one for one four three three so let's copy it again sorry and let's go to the docker compose and we'll paste it and we'll put it's gonna be one four three three so the next step is gonna be the database user again as mentioned earlier it's really not good practice to log in as sa but for demo purposes we're going to be using it so it will save us a bit of time then creating all of those users and creating the scripts for them after we create the database user now we need to pass the database password and we can see from the service above we have put the password docker 2021 so we can use the same one because basically from this app is connecting to this server using those credentials and the last one that we're gonna do is we're gonna put the database that's gonna be automatically created for us so we'll take database name i will put it here and for this reason we're gonna call this database boxdb very simple so now let's try to do another first docker build dash t call it books management this is just to make sure that the image is building properly and there is nothing wrong with it so once we do that and make sure everything is running as it should be then we'll use a docker compose up okay it's building now we use docker dash compose up dash dash build and now we can see the application is building again because we define the dash dash build and just wait a few seconds it's building the sql server we can see it's running and we can see the initialization and at the end we can see here that the application has run successfully on port 80 within the container so from the outside it's going to be connecting using 80 90. so let's try that out let's go to port 8090 and now we can see we have the box link has shown which is great if we click on it we don't have any books because we haven't created any let's create a new book we'll call it book one and let's create it and we can see we have book one created let's create another book book two and book 2 i created and so on and so forth let's call it book 3 perfect so right now what we have is we have our books management running within our books management web application which is running within a container and we have our sql server which is running within another container and they are communicating with each other using the docker compose network oh one thing i forgot to mention when we create a docker compose file similar to this one the docker compose cli will create a network and this network will put those two containers under the same network so let's say for example if we want to create the run the image of the application by itself and then run the sql server by itself without docker compose these two containers will not be able to communicate with each other because one of the main benefit of docker is the isolation and because every container is going to be self isolated from the other one it will not be able to communicate so but when when we do put it inside a docker compose file the single file is considered as one network so these two containers will be able to see each other and they will be able to communicate with each other yeah okay that's what i wanted to mention sorry i forgot to mention earlier so basically this summarize and our application for today it's basically how we can utilize docker compose in order for us to create an asp.net core application with sql server please like and subscribe to our to the channel and comment for any types of video that you'd like to see or any future topics that you'd like me to cover thank you very much for watching have a great day ahead please like and subscribe thank you
Info
Channel: Mohamad Lawand
Views: 16,423
Rating: undefined out of 5
Keywords: asp net core, dotnet, docker, docker-compose, beginner guide, step by step, sql server, tutorials, efcore, entity framework core, aspnetcore
Id: zrzdRINyYdM
Channel Id: undefined
Length: 56min 10sec (3370 seconds)
Published: Fri Jan 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.