Docker Compose with .NET Core & SQL Server (Step by Step)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so docker is an absolutely fantastic application when you need to spin up individual instances of something individual containers but what about when you need to stand up multiple containers and a more holistic way quickly and easily and get them to talk to each other without much fuss well if you stick around when they give you a solution [Music] well hello wherever you are whenever you are freedom I I'm in Melbourne Australia and whereas it's 10 bot 2019 now today we're going to be covering something called docker compose I've had a number of requests to do a video on this so I'm hopefully it will be something that you will find useful and informative so what is docker compose well as I alluded to in the introduction it's something that allows you to I guess orchestrate or compose multiple docker instances or multiple docker containers should I see in a more holistic way so rather than having to enter multiple command line prompts multiple things you can do it in a more synchronized easier fashion so I don't think we should waste any more time let's go on with it so yes you are in the right place this is a video on how to use docker compose with a dotnet core API and sequel server so just quickly what you'll need to follow along if you want to do that you'll need a text editor I'm using BS code you'll need either dock or desktop or Community Edition depending on whether you're running Windows Linux or Mac you'll need the dotnet koresh TK installed I'm using 2.2 yes I am aware that 3.0 was on the cusp of being released but to be honest with you taking a deliberate decision to let that they're actually bed and a little bit more before I decide to use it in my videos so for now I'm still using 2.2 so you should you should if you're using three yourself then you should still be able to follow along and about 40 minutes of your time I don't usually specify prerequisites for my videos I do cover everything on this video absolutely step-by-step but just for some of the non core subjects I did not detail ruins as much detail as I will usually like and I just moved through them relatively quickly if you want some more detailed overview of some of the concepts that come up in this video then I would suggest following Madhu's first one is the one I did on the.net core a REST API we're going to cover that quite a bit in this video I'm going to again move through it quite quickly if you want a detailed discussion watch that video the next one is on how to deploy a dotnet core API with darker that's mainly around how you construct a docker file so you can package up a dot in their application into our docker image don't really cover that in this video we need to build upon that work so if you want to you know get more detailed conversation on that then watch that video and then the last one is how to connect to sequel server running and docker that one possibly don't even need to watch I cover most of the same content in this video so I think the fact I think we've covered all the necessary introduction stuff so I think need to get going and start using docker compose ok so before we actually start using docker compose I want to basically build up our development environment and get it to stage real we're ready to use docker compose and we understand why we're using docker compose now as a basis for our environment I'm going to take the API example I created in the last dollar or the second last talker video which was about taking a darker API taking a dotnet core API and running in a docker container I know that API was effectively really just the standard Web API template out of dotnet core you didn't really add anything to it from memory at all apart from the docker file so I'm just going to I've put that up on my github repository I'm gonna pull that down and we're gonna start from the rather than doing all those steps from scratch and then I'm gonna build upon that in order to allow it to connect to a database and then at that point we're gonna start introducing docker it can pause a bit more so let's go over to my github site so it's just github calm forward slash biting a nice thistle and then I've clicked on the repositories tab the API I'm talking about is this color API I've just called it that because we're just going to store colors in a database one table with one or two columns an ID column and a column of colors just really simple but we're going to use a day two days so click on the color API go to clone or download and you'll get the URL here and just copy that to your clipboard so then I'm going to go into a command prompt and I'm gonna get to my working directory it's going to take my watch off so it doesn't clunk on the desk and with a change into my onedrive I'm going to change into Docs and we're tuned into movie escort fuller and that's my working folder and then all I do is get clone and we'll just paste in the URL of the color API and hit carriage return and that will go away and it should bring it down and put it in a folder called color API so apologies to my American viewers but I am spamming color the correct way with are you a probably really annoying you so we'll just change into that folder color API there we go and we do a listing will have that project they are so we can then open that in vs chords and just type chord I think it's open it in a window there we go and we have it loaded now probably pop up with some prompts to rebuild there we go so there are some unresolved dependencies yes and required assets to build and debug are missing from color API click yes and that will go off and get them no we'll probably get a few warnings but I think they've been resolved that all looks great so I'm not going to go through this in any detail is literally the bog standard where the API template it's doesn't come with any database connectivity called or anything like that but what it does come with is a docker file again watch my previous video on docker if you want to get step-by-step detail of how that works a high-level summary basically the stalker files going to take the project we've got your and package it into a docker image the we come on later so just before we go any further I do want to check that the record is working and it's still running okay and so forget doc I forget anything that I'm just going to do a.net run just to run it and make sure it's it's working as we expect looks okay let me just check it in in a browser so we'll just do HTTP localhost port 5000 and API values cool that's working so basically all it's doing is hitting our controller values controller and hitting this action here I'm coming back to Thomas that's all cool that's it just working and running locally on our machine great so the next thing I want to do is to make sure that the docker file component of it if you actually go back here if you look and look in that and want to make sure that that is building our image appropriately now what I'm going to do is I'm just going to move over to our dock our and I really advise you install this plugin if you're using vs code and it gives us a list of all the images we've got on our system and containers I cleared my system down so there's nothing there so what I'm going to do is we're going to build this API as an image so darker build and I'm gonna tag it give it a name and that is my username on docker hub and then we'll call it color API you've got to put all this in lower case if you actually capitalize any of it I believe you get an error and then our working directory is just the directory we're in but all our more thorough docker file exists and that will go off now and we'll run through that docker file that you can see on screen and of all basically build up our image based on the steps in the docker file and once that's finished downloading it will then run and we'll have a look and see make sure that that works and we can then move on to adding some new database called in to this API that allows us to connect into a sequence alpha cool so that's finished and one of the things you can see here that's actually created a number of images for as you can see it's brought down the Microsoft core SDK M inch which is up here it's brought down the runtime image which is being used here and then finally it's created our API image so all that's left to do is actually run our image as an active container and so in order to do that we just take da car run and we're going to specify our internal external port mapping so we're gonna map X day only to port 8080 I'm gonna map that internally to the port 80 that we're exposing in our container and then we just specify the tag name which is binary vessel this tle color api a color or a lure api and that looks like that's running so again and you can see here in our plugin extension that this is running and it's running on port 80 and terminal e so if you go back to pre we tested it here the port that we need to call as 8080 that so external port mapping and internally to the port 80 and it should give us exactly the same result and in fact it does so that's cool so we've got to get hub we've done our get clone to pull the source code down and I suggest you do that and actually wanted to do it from scratch we've checked that the cord actually ran locally and natively on our desktop and then we've created an image using the existing docker file that's in there which was cool and then would just run that image as a container so we now have our API running as a container but we're going to stop that now and we're going to make some code changes to enable it to access a database all right so let's jump over to our chord again we'll stop our container the EPI container just right click and stop and mail and move back to our chord no the first thing want to do is add a folder for our model we're going to add a model into our solution just to store the list of colors it's fairly simple setup so we'll call it color dot CS for my American friends out there if you want to call it color without the you have more than welcome to do that so this is a very simple model class so we'll just do a namespace and its color API models and then our class itself public class color don't know if you can hear that there was some buds chopping outside some of some of those on its way here and in Melbourne that's very nice the public int ID as our first attribute and cost property and set get set and then public string what we call it come up and I love and that's the thing to think of all over that because the classes are soon it will suit color color name there we go give it a stand simple bonds are gone crazy oh they are and then I want that's none of the fighting okay so we'll save that save that that's cool now the next thing we don't want to do is add a DB context class into our project I'm not going to go into the massive detail about it in my last video last video one of the videos I did on dotnet core API for development we'll go into what the DB context does is basically a very presentation of our database using entity framework or so glad that note so the first thing just to check is if you just type dotnet yes just to make sure that you have the tools installed if you get this called unicorn kind of thing popping up then that's all good to go if you don't get that if you get some kind of error message again check out my video or blog article on building a document called epi I tell you how you can resolve that if it's not if you're not getting that so again within our models fall that we're just going to create a new class and we're gonna call it color context both CS and same namespace call our API models and the class name itself is a public class color and color context on the X actually forgetting something up here be neither using directive to use microsoft entity framework or cool so back to our class public class we're gonna call it color context and it's going to edit for all more surprises in context and then we'll just create our class constructor public color context dbcontext options come on context options you see that empty for now and then we want to add what we call a data set which is effectively a representation was taking our model our colors model and it's representing it as a table really in our database that's all it is around DB cert what type of DB set is its color well and I'm just thinking out loud and may be wrong but since I'm spelling it the English way or the Australian rebirth are you it's not a reserved keyword I'm wondering if my us planes if you use color spell though you actually the further you if you're allowed to do that maybe a reserved keyboard so you might be was you might have to use the the English spelling if you cool make keep it simple for you and then we're good the DB sets could be called color tightness so get set on that cool okay very simple but there's still a bit disconnected so we need to do a couple of other things to get this working fully end-to-end so that it can actually talk the database right so we've just thought of dbcontext we have a model but at the moment the DB context which is a representation of a database doesn't really know where to go in terms of that physical database now if you don't you haven't spun one up yeah we're going to spin mop the spend one up in that container in a minute but that being said you want to lay the groundwork for where does the DB context go so in order to do that we need to provide some kind of connection details firstly to enable it to connect now ordinarily you would put that in your app settings JSON file as a JSON string that would go in there but because we're going to containerize our api I'm not going to do that and instead I'm going to use environment variables to basically build up our connection string now the reason for that will become more obvious later but in short it means that you can inject new environment variables into the container application when it starts up if you stored your connection string and app settings Jason as a file and you wanted to change some of those settings such as the password that you're using to connect or the database name you would potentially have to rebuild your whole container but you don't want to do so you basically won't have a kind of almost static like using that term static container that you can inject definitely environment variables into that's at runtime so that's the approach we're gonna take so in order to do that we're going to jump over to our startup class and we're going to go into our configure services method and we're going to start setting up the environment variables that we are going to construct our nation string from so we're going to provide four things we're going to provide a salvo name we're going to provide a user ID that we want we're going to provide a port that we want I'm not going to provide a password that we want so let's begin so just do var Salva get configuration and this is where we put in the name of our environment variable and we'll call it something DB server and if you don't have an environment variable then we'll just pass in a default value so I'm going to pass on local host the next one one is the port exactly the same path on if we have an environment variable it will use that and I'll call it the UB port we don't have one then we'll just pass in a default port of 1 4 4 3 which is the default sequel server port user ID or user now just a word of warning here I'm going to use C DV user I'm gonna do a very bad thing here red red flashing lights do not do this in production do not ever do this I'm just doing this to save a bit of title but I'm gonna use the SA account which is the system administrator account for sequel server do not do this in production or even taste really you should create another user I'm just doing this for quickness and then password [Applause] and again you shouldn't ever ever ever ever ever store your store your passwords in plain text and code especially shot to get hub it's all published and again the wrong ways you can get around all of this and you can store it locally I just love time to do that I have done other videos that take you through how that works specifically the one deploying an API to is your date with it that there's your DevOps that explains how to not do this I'm just doing this book quickness I'm doing a password in here I'm putting a user ID that don't really exist yet so we will do that in a minute it will all make sense eventually so basically what we're saying is if we have an environment variable called DB server then we'll use it otherwise will revert it to localhost same for all of these attributes now what we want to do is actually build up our add our DB context as a service to our application and make sure that it uses those connection string attributes to connect to the database we're going to spin up now you need to make sure that you add these two additional using statements so you need to add a reference to your models here and you also need to make reference to entity framework or so make sure you add those and at the top there which I just did there they are I'm just making sure that you've done that as well so you then just type services and dbcontext what DB context where I want to add color context there's some options and those options are you want to use sequel server and then really all you want to do in here is pass in your connection string so if you're using your connection string from app settings Jason you would reference that we're actually going to build up our connection string using these parameters here so if anything is going to go wrong if any typing mistakes going to happen is going to be here some I have to circle back if I make a mistake but let's see if we can do it without any mistakes so first thing is server and we're just making that equal to our selves our variable and am I putting in a comma and we're going to reference our port semicolon so that's our server component server name port semicolon and end in it initial cap that long make sure spell initial correct because I always get that wrong this is just going to be the name of our database now we could put that up here as well I've not done that I guess we could in fact let's do that let's do our database make the rules may as well keep it consistent configuration what the hell's going on figuration and we'll call that database [Applause] otherwise we'll just call it something like it covers okay so initial catalog curly brackets database I'm going to pass in our user ID and that will be equal to user and then finally our password which will be equal to no surprises that's what cool so that's basically as told are adding up dbcontext to our services pool and then we are using sequel server as the database type and are passing in our connection string there so hopefully that is correct okay so what we need to do now because we're using entity framework entity framework works in a number of ways two main ways are code first and - vase washed so the database first you create your database first and you basically import the schema effectively into your code application your cool project and it will entity framework or will create classes and things of that nature the references to your database as you've designed it that way we're not doing it that way you're doing a quote for support so we've created our colors model and our DB context and we're going to use this migration strategy to migrate those things into a database that way so the first thing we need to do is create a migrations file that we will then use to create the collateral in our database when we finally spin up so it's pretty simple to do that all you need to do is just type dotnet and make sure you're in your obviously in your project that we actually don't know yes the framework migrations add and then you just give the migration a name now usually the name you give it makes some kind of reference to the new additions that you've added so we've Muir adding our color model so call that had come up model enter and if you keep an eye on the folder structure here you'll see we have a migrations folder with a number of files in it and that those files basically just tail or database how to create the tables that match our called classes they call now order Natalie what you would do now or what it can do is you would then at the command line execute that migration file so all you've done is generated the migration file we would then actually run the migration file against our database now our database doesn't exist yet because we're going to use a sequel server or container so it's a bit awkward so this is where we start to explore the concepts with with docker compose so just parking that for a minute instead of running on my migrations manually we're going to build it into our cord so when our application starts up it will check to see if the migrations have been run and if not it will then run them against the database no I'm not suggesting that you do this in a production environment you wouldn't really want your application in production every time it started up checking to see if the migrations have been and then run it against the production database you wouldn't want to take that strategy there but I gained for the purposes of just getting to darker compose and talking about docker compose that's what I'm gonna do so we'll do that now okay so in our modern school we're gonna create our new class well if I selected the models fall but now I call it trap DB that's yes don't forget the CS extension we're gonna have a few using statements microsoft asp net core builder using Microsoft entity framework or using Microsoft extensions pens injection excuse me I think I'm losing my voice using system link set up a name space color API models not migrations so must be painful to watch models there we go and then we will set up our static class what's a public static class we'll call it of course right D and again this is just going to this is just going to set up our database and might call our migrations file and migrate across stop if it's not already there and we're all so why not with a better data into a database as well so yeah that's why this class exists so before we do anything we're going to do explores our first method public static void methods and we'll call it mmm prep population and the passing and I application builder call it app and basically the point of this is what actually going to get what's called I'm the score we're gonna get scope of our DB context or DV context is running this is basically just getting almost like our who can to that dbcontext so we can use it to actually run the migration that's all we're doing so using connect a variable and say they're using statements we'll call it service school and we'll reference our app builder object the car application services or fractured dbcontext one and is one and we'll get the called the create score so then with that well then going to do something with that service score before we do that though I'm going to create another method factor in the right place that would be useful so we're going to we're going to do something in here we're just not going to do it yet I'm going to create another method and I'm gonna call it public static void data and into that we're going to pass our DB context which we'll do in a second to saw our context is color context what context we're going to pass that into this method so that's what we're going to do in here so going to call C datum we're going to reference our service scope that we've just defined above service provider get service she picked about one lot services and then we're going to supply what take the service do it wanna get we want to get our color context so that should get as a reference talk of DD context class that we're passing into the seed data data method will then do the magic that needs to happen so let's just do a system console.writeline just so we can keep a track of what's happening I'll say applying migrations standby and then I'm going to context which is here obviously database migrate and so what that does is it takes this will take some latest migration file and applies it to the database and we will have our database schema in place okay so we could leave it at that but I'm gonna actually add some data into the database as well no so creating the schema is one thing that's useful but pulling but more useful to have some data in there rather than having to add it manually so the first thing I'm gonna do is check to see if we do already have data in the database so if you do then we just leave it if we don't then we'll add something so context color items any so if you don't have any items in the database then I'll add them otherwise they'll just write something out to the console just an console.writeline already have data not seeding otherwise we'll add some data and so you'll do another system where you are not just copy that's actually rather than typing it again just just so you can see that it's working when we test the application so in order to do that we'll make use of a context again context and this time again I'll make use of our colors color items collection and we're going to do something called add range not a strange add range and then we simply add a couple of colors to our database it's not terribly complicated that me just put a semicolon now to stop editing so we just do new color and then we'll just give it color name equals right and it's basically that simple add I've got a few min just going to copy this actually do orange yellow green blue yellow green and blue cool so if you don't any color items and I'm database them or add them if not just make a message out so all we need to do now is make sure that we call this method for our start up class otherwise nothing will happen so if we pop over to our start up class we'll just add it in so I can figure out method after use MVC and as it's a static class is quite straightforward click DV type population and then we pass over a pelvic context here and that will just go in and it should work so we've got one last thing to do we need to update our controller to actually make use of the DV context and pull data back and then we're ready to start testing it against our sequel server instance running in doc so let's just go over to our values controller and again this was the standard controller that we just had oh the web api template and what we're going to do is we're going to update this method action he off to actually make use of a database now what I'm also going to do just clarity I'm just going to get rid of these because I'm not really gonna make use of them so the first thing we need to do is either using directive up here to make sure that we have access to our models folder [Applause] our models namespace should I say not formal and then within our controller class we're going to create a private instance of a context object that will make use of later so private read-only color context to call it context underscore which is standard convention now we're going to create our we're going to use the pendency injection to inject in our actual colors controller I'm going to use constructor dependency injection so I need to create a constructor in our controller so public values controller I'm gonna pass on a color context called context note there's no underscore and then all we simply do is say apply the color context to the context that's injected in via dependency injection and again I'm not going to cover the pins injection here that's for another time in place and that basically means we just have access to use our context object throughout the rest of our code and then the rest is relatively easy I'm going to comment this out so you can just see the differences and there's not really that many very soon well so again we'll do HTTP GET public action results i enumerable and we're gonna agree rend color and then just give it a name get color items correctly and then it's really quite simple you just return for my context or color items that's it really so we use on dbcontext hopefully it should be connected correctly to our database and then instead of returning a static string as we've got previously doing just returning yeah the color items that we have in our database so that is all the database called correct so now all we have to do is spin up an instance of sequel sale form and connect into it from our api so what we're going to do is we're going to spin up a sequel server instance in docker and we're going to then connect into it from our api running natively on our local desktop so we're not gonna put our API and docker just yet but it's gonna run it locally and I'm gonna connect into our sequel instance running in darker and it should work and it will take take it from there okay so just before we sped up our sequel server instance I just want to really draw your attention back to our startup class and the fact that we're trying to set up some environment variables which basically build that connection string and I'm going to use those same default values when I spin up a sequel server so that well so that we can connect now one thing I'm just noticing here it's not really an arrow but it's just I wanted to use port 14 33 which is the default sequel server port you can use any port in your like but I just wanted that to be 14 33 so I'm going to change that to 14 33 now I also did a video on spending up a sequel server instance in docker that was the last video I did put a link to it somewhere and you can take a look at that I'm not going to go into too much detail here I think what everything does but it's fairly self-explanatory so let's face it just issuing a darker one command so docker run and then we're going to pass in some environment flag so the first one is accept EU le equals yes system that's just accepting the licensing agreement the next environment flag we're gonna pass on is the SE passwords and I'm just going to set that equal to this and then the final environment readable in a set is the version of sequel server that we wants ms SQL underscore hid Dwarfs Express and but I'm going to do our port mapping that's why I wanted to change this just to make sure we're mapping the port correctly so we're gonna map externally external port to our internal port so 1433 214 30 so you know you can you know you can configure that as you please but I've just kept seeing both internally and externally that as 1433 so make sure that's correct I think I'm slightly numerically dyslexic I keep getting this wrong I keep having to come back and we be filming it so that's fine that's all check so and then the last thing you do not port as we just then select the image that we want to download and run the D flag just means we're running it in a detached mode which means when the image downloads and once we get out of my pot we're talking back to us that's all that means so MCR microsoft.com ms SQL server and the ocean was 2017 latest I didn't I don't know if you can hear the sirens and the background there there's a hospital quite nearby when I live so often your ambulances it's speeding past so I think that looks correct and does double-checking the poor allocation is correct yeah that looks okay so her enter you can see the other it can't find unable to find a copy of the image luckily that's okay and so it just pulls down an image form the bit in the relevant repository what you're thinking this case is just docker hub as you'd expect now what you'll see here well that's done loading is we don't actually have upset already we don't have a sequel server image you will see that eventually pop up in human that's downloaded and then once it's downloaded and it runs you'll then see the container running here and then what we'll do is we'll connect them to our instance of sequel server using something like management studio just to make sure it's working and then we'll test against the API okay so that has finally downloaded and it started to run you can see as I seeing the images now appeared in our list of available local images so he ran that command again you wouldn't have to wait a few minutes to download it we just would be there and then you can also see that we now have a running sequel server container up here as well so just to check that it's all working correctly I'm going to fire up management studio and let's just started on my other unis just drag it across and I click on the connect icon I've already had this prefilled but just use localhost to calm up 14:33 we use sequel server authentication change this to s a and then we'll just type in our password I get that correct okay cool so it connects and so no problems connecting into the container from from here there are some system databases which I just didn't care one sequel server or users to manage itself there are no other databases there or anything else completely completely clean now what you would usually do here is you would start to create user accounts specifically for your API and all that kind of stuff I'm not gonna do that that is not best practice we're just going to use the AC account but again in interest of time that's not the focus of this video so what I want to do now is run our run our API and what we should start to see is when the startup class runs this will run and you should start to see some messaging around database migrations being run and data being seeded and then we can go back in here and we should see that the database has been created with data in it and then we can also run the API itself to pull that data back so this is kind of the moment of truth if I've made any coding mistakes at any point in time this will get flushed out so everything's saved and music back to my file view here everything's saved so what we simply did is dark net run and wait with bated breath so seeing applying migrations which is a good sign looks like that looks okay adding data seeding okay and then seeing a hosting environments development that's giving us our usual listening stuff so that look but not actually what I some supplies there shouldn't be but it's very easy to make at least one small mistake and you have to go back and find where that is but that seems to what okay so you can see here I've just done a refresh on our management studio session and you can see that we have indeed we have a colors database now with all the standard folders and we should have a color items table now and we should also have data in there so if we do is select everything color items we don't actually get our colors wonder why that is okay I think we've probably left something else did he was one of the coding mistakes that I was talking about so let's go back to our threat DB I think I know I've done wrong yes I know what I've done wrong I've forgotten one very very important command so this was actually probably quite a good mistake to me I've simply forgot to put in context save Save Changes okay so we kind of said we're going to add all this data but didn't actually save it so it didn't actually go into the database so cool so that's actually a good use case so let's let's stop an application let's rerun it again it's checking to see the migrations have been run it should have said seeding data and here we go adding seed data so let's go back over here mister refresh over here to make sure episode database is still they've still got the same table if you select Furcal items now there we go our colors are novel it so that's cool and then the last thing to check before we really move on to docker compose you'll be pleased to hear is - if I just bring over a web browser if I do local host port 5,000 API values we go we're just checking that the actual API itself works so again we are actually hitting our values controller and we're calling this this action method to you I'm going to get my colors return back and there you go you can see that so working quite nicely so just to reiterate what we've done we have bring this back up we have started a sequencer for instance in darker and we have run our API locally on our machine it's still not in a container yeah so an hour what well so what I'm going to do next is repackage our API as a container and start that up so basically we build this this one here restart it as a container and see what happens okay so because we've made code changes to our darker API we're going to have to rebuild the docker image we remember we did that at the start of the video we're gonna have to rebuild it because there's been changes and then when it starts up we'll see what happens now it should again tend to connect into the sequel server instance try and apply the migrations try and see the data and again if the migrations have been run and there's already data they are you should see the appropriate messages see don't need to do that but by but it should theoretically should that still connected into the sequel server but let's let's test back CD so first thing we need to do is we build an image so it's just talk up build I think I make sure you're in the right working directory so it can find the docker file and we're just going to tag it I need to spell it correctly little [Applause] tight my typing you should be quite good but it's definitely degraded I don't know tonight because my fingers are cool okay so darker build - tea by kneading thistle for Asajj color API and then don't forget to end the training period to say this is our common working directly an actually go off and build or rebuild our rebuild our image yeah so that was incredibly quick before we got the images that it needs looks like it's done that okay it was incredibly quick there wasn't it okay it looks like it was okay it doesn't look that there's any issues there okay fine and then we will just run it now so we've got it rebuilt image let's just actually I'm going to delete this container because I wasn't not sure that actually did do I wanted it to do I'm going to delete this image as well I'm not convinced to actually then do a rebuild so let's just uh should I again okay yeah it was very very quick Fiona okay I'm just double-checking want to make sure that the changes are taking place so then we just through a docker run and then again in a similar way as we did with a sequel imagery map our ports are gonna map externally to port 8080 I'm gonna map to the internal port 80 that we're exposing here and then we just basically give the name of the image that you want to run so binary that's all or slash color API and you should see you're running a container in there they go starting to run and you can see here it's trying to apply the migrations and we get a whole host of errors and if we actually have a look at the arrows if you just scroll up and see what it's complaining about it's saying basically I met what related arrow meant that we couldn't find the sequel server the server was not phoned or was not accessible so what's going on there we didn't change anything we still have our instance of sequel server running it's using the same credentials would not change any of that all the done is packaged up our API is an image and run it what tool caiva me ran it locally we can still connect them to the sequel server from the management studio but as soon as we packaged up our API into our docker image we have problems that is not at all that it's not a bug that is by design so if you go back to the central concepts of what docker is it's about container izing applications and part of that is keeping things separate and so by default the networked path between two containers actually is kind of prohibited so you actually need to ensure you have to actually explicitly you should I say you have to explicitly set up those networking permissions now this is for your docker compose really comes into play so just before we jump into the specifics of docker compose and a networking within docker compose that is another way of networking between containers I just want to mention I'm not going to cover it here and that is software-defined network so you can use this concept if you are interested that would Google that the other one is using docker composed a low networking between containers which is what we've got to do here now just quickly stepping back to what is docker compose thinks probably a good time to revisit that it reduces the reliance on and simplifies the use of the docker command line no you can see that we've set up our sequel server instance using the command line we've set up our API using the command line can you imagine if you had other components that you needed to spin up and you were just typing away the command line and you can see the command line is quite long it's very error-prone and you can actually for using things that volumes and although other bits and pieces actually gets really really really long so it's difficult enough and laborious enough when you just get two containers that you need to run but you may be running many many more than that so the command line is fighting for hacking about it and just spinning things up here and there an ad hoc and I know but if you're actually wanting to run things as a whole harmonious and to end ecosystem it's not really not really suitable so one of the main things about docker composes you can kind of coordinate the spinning up of your containers and actually you can have say it dependencies between them and all that kind of stuff as well so you you can start one before the other one starts all that kind of thing it allows us to sloth up multiple containers quickly and the last point here is allows a set to set up connections between containers so you'll be pleased to hear finally become on to using docker composing at all centers around a docker can pause file that we basically set up services and a leper allows us to start containers quickly and network them together so we're going to do that now okay so we're going to build up our docker can pause file no we just we're going to add that to our project just a bit of housekeeping just to make you aware of what I've done here I've removed the two containers that we had previously created our command line just to keep it a bit cleaner and more straightforward so you can see what's going on when we come to use docker compose the other thing I want to just change briefly because I've had some problems with it is I'm going to change the default password that we're going to use for a sequel server and I'm not going to use dollar signs the main reason for that is when we come to use the docker compose file we need to specify that password India and I've had some issues with dollar signs you can apparently escape the use of the dollar sign and actually couldn't get it to work it just caused me a lot problems so if you've got an answer to that I'd be really interested to hear that below if I find the answer I'll post below as well but just to keep it straightforward we're going to just change the use of dollar signs and our default password for sequel server so make sure we save that and again just coming back to the fact we have no containers here they were gone you're blown away so it when we run this up and the migration should be applied again and all that kind of stuff and the sequel server instance will be started with well you'll see in a minute with this password no let's do it now so back over into our project we're going to create the docker compose file at the root and it's just called docker I think compose dot yml and you get maize pink icon there and and we just begin by specifying the Belgian which point famous three way for you and this is a llamo file so it works a lot of white space you've got be quite careful with how you indent things Visual Studio chords pretty good that usually does a good job for you but just be weird of it now as I said docker compose what so in this concept of services of it it's probably easiest to think of a service almost like a running cantina or how you want to take an image and run it as a container so it really is a combination of both against the image that you want to use along with some config this is almost exactly like then what you're running at the command line but just scripted in a yeah profile so we're going to create two services and you can see their visual studio quarters indented nicely for us and we're going to call and call this anything you're late I'm just going to call it in this SQL server that can be anything it's really just a tag and I mean and only want to specify the image so this should be fairly familiar to you MCR microsoft.com /ms SQL forward slash and the value in 2017 latest so it's exactly what we take to let the command-line exactly the same image that we're using and then we're going to pass on some environment variables I'm sure I spell it correctly I'm again familiar it's just the same environment the evils at the pass thing out of command line so you can start to see by using docker can pause it and you'll see when we actually come to running it and you don't have to type in these lengthy command-line arguments again so that's a bonus number one except the you have a equals it's not equals to use the call on and in the value in quotes s a pass once again this is what I'm talking about I have issues with using dollar signs in this yam will fail so we're just going to go back over here and we're going to use this one instead and then the last one is the flavour of SQL Server we want to use ms SQL did not piss and expect and then we want to set up the port so we just 1433 map to 1430 so again very very similar to the command line stuff that we used do they want to set up a second server so this this whole block here that's the service configuration for a sequel server it's protectively exactly what we had previously taped at the command line so we're going to set one know for our color API again you can call it anything you like and this time we're not going to specify an image exactly this specific thing we're just going to say we want to build whatever is in the docker file so the dot per file specify so we want to build an API it's just going to be so they use that and hook into that so it's quite simple and then like with a command but normally they need to do is specify the ports that we want our call on in there and the ports that we want r800 fix they are mapped to our port 80 internally that's it so this is kind of almost scripted up the starting up of our services or containers you may ask well you've not really explicitly done any networking stuff other than the port allocation which we had already done at the command line so how is this going to make any difference to the problem that we had with our two containers well this is one of the beauty no another may subdued beautiful things but docker compose simply by the fact that you've defined your services within a docker compose file basically put someone to the same network now there are more complicated networking configurations you can do within the docker compose I'm not going to cover them here today just going to do the kind of basics that you you need to get things up and running so just checking with that that all looks kind of all clear have saved that though the way that you run the docker compose file is dr. - and paws-up and before I run that and just going to move back over to our docker plugins so you can see what's happening deal so let's run that it will go away and it will spin up our images and you'll see up here a number of things happening so you can see now that we've got a Microsoft sequel server image running and I color API instance running no something's gone wrong because you can see that the color API container has stopped why has it stopped well I actually know why it stopped and I knew it would feel and that the reason and this is still doing stuff but I'll just explain why it's feeling over here and I'll start up its using the default server string of localhost no basically it cannot resolve that it can't really use this localhost string to connect to our secret sale but it doesn't know what that is so a very quick fix for that is to use the name of the service of your sequel server simply copy that and paste that in here instead as a default set of own name that should work so if we save that and we'll just ctrl C in here that will just close down on running running containers I'm going to get rid of these you don't have to do this I just like to do it for a peace of mind to make sure that's rebuilding everything that I need it to be build and I'll also delete the image that it created so it's just remove there as well cool so again we've not changed our docker compose file at so I mean exactly the same all we have changed is the name of the server and that's going to use the name of the service and docker compose and because it's running in docker composites docker compose knows too basically translate that tag name into a an IP address I would presume that allows us to connect to the sequel server so let's try that one more time okay so that all looks like it's worked correctly as you can see who's adding data because clearly the image didn't exist I'm sorry the container didn't exist prior to that so if we move back over to a scatter instance of Firefox something with me I'll go over to our localhost and we use a zero eight zero yes remember that's the last time there you go it's actually pulled back the colors from our database if you go back over to here I'm just I'm just connected them tonight yeah I'm just disconnecting that I'll just kill that I don't kill that and we'll we connect in so we want to go to localhost 1433 and you can see that we've connected into that container and yeah it's created everything as expected no again just to reiterate we've actually connected into that container for an external application that was not it's not running in a container if that makes sense so we could just connect straight into it how about coming back to the point of things are running in containers then they're automatically barred from talking to each other hence the need for docker compose okay so one of the things that I just want to finish off on is we actually hard-coded the name of the sequel server service in our Accords which is not what you want to do and the whole reason for using environment variables is so that we can inject these values and more than in a more dynamic way so let's let's revert this back to something that's not going to work so again if you don't have a DB server environment variable we're going to use localhost and as you know that will not work it's what we can then do is go over to our docker compose file and add in in the same way we did with a sequel server and environment section and pass in any of these environment vehicles that we wanted to so we're going to pass in DB server so it's just copy that make sure you give it the same name and then whatever you want the DP 17 to be we're just going to copy this we'll save that and we'll delete these containers and then we'll rerun our docker compose file and it should work and indeed it does so both containers are still running and we can probably go back to here and just leave on that and yeah it's all cool okay well that brings us to the end of this video on docker compose it was quite a lot set up to get us where we needed to be and there's clearly a lot more things we could do with docker compose I really just scrape the surface with it but we did enough I feel to get everything up and running that we needed to and to provide you with a nice foundation on which to move forward so other than that thanks again for joining me if you liked the video please give it a like and if you haven't done so already please subscribe but I won't hold that against you after you decide mortis or until I make my next video I'll see you again [Music]
Info
Channel: Les Jackson
Views: 53,214
Rating: 4.9677634 out of 5
Keywords: dotnetplaybook, docker, docker image, docker container, docker compose, sql server, .net, .net core, api, asp.net, rest, dockerfile, docker-compose.yml, les jackson, step by step, binarythistle, tutorial, docker-compose up
Id: 4V7CwC_4oss
Channel Id: undefined
Length: 68min 15sec (4095 seconds)
Published: Tue Sep 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.