Docker Compose Tutorial with PostgreSQL and Node.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so a lot of times instead of starting just a single darker container you want to start up multiple and have them be able to connect with each other so in this video we're going to be using a tool called docker composed to help us out and to start both a Postgres and a node.js server and have the node.js server connected Postgres so we're gonna start by creating a docker compose DYI ml file so this is going to be the configuration of how to set up these containers then we're gonna add retry logic so our node.js server can connect to our database and if it's not up yet we can retry that and lastly we're going to talk about how we can start up and shut down all these containers so let's go ahead and start with the docker compose file so I'm going to create a docker compose diamo and this file is where we gonna set up all the docker compose things or containers that we want to start up now before we get started on this I should mention that docker compose is a separate installation from docker so go ahead and install that if you haven't already and at the top here I'm gonna say version and specify the version of docker compose that I want to use or at least that the configuration file is going to be the latest is 3.7 so I'm going to use that and then I'm gonna say services and here I'm going to list all the containers that I want to create and I can give them a name so the first one that I want to create is a Postgres container I'm gonna call it DB and then I can specify the image which is going to be Postgres now here I can pass in all the flags or the commands that I was passing in if I was just going to start a docker container with docker run so here I put 3 environment files or environment variables and then I create a volume and I can do the same over here in the docker compose so I can say environment and then I can say create my variables so I'm going to copy the Postgres one and then so the name of the environment variable colon and then I'm gonna put the value so here's gonna be Postgres and then we can do the same thing for the user and then lastly DDB and then we can also create a volume I'm gonna say volumes and then I can say - and then I'm going to say PG data and I'm gonna do it in the current directory so it's going to create a current folder in this current directly called PG data and that's where it's gonna store the data and I can keep the same path over here so it should be PG data : and in the path inside the container and I can get rid of that backslash at the end there okay so as it is right now we have this container here it's gonna be a DB service it has the image using the Postgres image we pass it three environment variables so it knows the startup with Postgres user and password and then also create a database called stripe example and then we set up a volume that way it persists the data and I change it so it's going to be in just this local directory it's gonna store the data alright and then lastly I can access or I can say the port's that I want to start in so here I can say I want 5 4 3 2 and inside the container as well alright and there's a lot more basically values you can pass in here if you want to but we're going to keep it bare-bones for now and then here the second service is gonna be my node.js server so I'm gonna call this web and the image is going to be the same thing that I've been using so if I look at my doctor build command I've been naming this image stripe example so I'm gonna copy that and use that image and then here I'm just going to say ports and specify that I want it to start on port 4000 and inside that container it's also running on port 4000 and then lastly I'm gonna add one thing to my web container here I'm gonna say depends on and I'm gonna say - dB so what this does here is it starts up the database container first then it's gonna start up my stripe my node.js server here second now what's going to happen is it's going to take a second for the database here to start up and the website's going to start up as well and it's going to try connecting to the database to not be able to connect because the database isn't quite up yet so we need to act we need to add some retry logic in our code so if I come over here to the index file I here's where my connection is so here's where I try to connect and if it fails it basically just crashes so here I'm going to add a try-catch and we're gonna have an error here and I'm just gonna console dot log the air and so what i'm gonna do is i'm going to say retries and let's say we give it five retries and while there's still somebody tries left we're going to loop so here we're gonna just loop and try to keep connecting I'm gonna say break if we're able to create the connection here otherwise we're gonna say catch and if we want to and we're gonna - one from the retries and i want to console that log how many tries are left three tries left and then at the very bottom here I don't want to just call create connection really fast over and over I want to sleep for a second couple seconds or wait for like a second or so to do this what I can do is I can say a wait new promise and I'm gonna say resolve and we're gonna say set timeout and we'll say five we'll say five seconds um yeah five seconds so what I did here is I just set a timeout and I wait for the promise to finish when the timeout is done and I said five thousand milliseconds which is five seconds so we'll wait for that to finish and then I'll retry again so that's the retry code that we're going to use for this you can also use something like this so they have a whole page talking about have to startup order the docker docks if you go to this URL and it talks about it recommends putting it in your application yourself put an application code the retry logic which is what we just did but you can also use stuff like there's scripts for example the wait for it script and you can go check that out you can use that as well if you prefer instead of putting the logic here anyway so that'll try to connect the other thing is to talk about what is the how do we actually connect to this docker instance over here so how do I connect to this docker container this Postgres instance well we don't use the host local host so we name this thing DB here and what docker is gonna do is actually allow us to access the Postgres container using the host called DV because we named it DB there so I created a separate arm config for this which I just called arm config dock or JSON and this is where I'm gonna store basically my connection details for the docker instance you'll notice for the host I chose DB here so it's going to connect to the Postgres DB if the host is gonna be DB port 5 4 3 2 and then the username password database the environment variables that we chose and then in my docker file for this container I just copy over the arm config and then I can change this to the arm config dot JSON so I copy over the docker JSON file for the arm config and rename it to just the arm config so it uses that so that's that's what I'm using to connect to the database now if I were to rename this something else like I called this Postgres then my hosts over here and the arm config I would rename the host to Postgres but it is DV so we'll connect that alright so that is all setup we added the retry logic let's show how you can start up these containers now so I just made changes to my my my file or my code there so I'm gonna have to rebuild the image so I'm going to run the docker build on this this image for my node.js server and then we can go ahead and start this up so like I said we're gonna need to have doctor compose installed and that's a separate install we can do - be currently using 1.2 - and so I'm currently in this server folder here and in my server folder I have the docker compose file so I can say docker compose up and I'm going to be using the - D flag what that does is allows me to start up these containers in the background and notice it talks about it creating them or recreating them if you've already created them before and here I'm gonna say docker container LS and I can see that I have two containers started up and let's run that command again it's a little longer yep there we go so I can see the container IDs and I can see the names of the two that I started up you also notice it's created a new folder over here called PG data so that's the data for the Postgres I can run docker compose blogs and I can see the logs for this so here I can see that the DB that's our Postgres you can see it actually took a little bit to start up we can see some logs for it at the bottom you can see our web logs so we can see the error it was unable to connect told us for there's for retries left wait it a little bit try it again and then it was able to connect so just because our database is up doesn't mean it's ready to accept connections so it takes a few tries so you can play with the retry logic over here as well if you want to increase the number of retries or increase the number of seconds it waits but now is up at localhost 4000 so I can go to that if I want to and here we go and now I can see my schema over here we're able to run that I can get a registers true we can login with this user and so that all works so that is how you can startup containers with docker compose how do we shut them down now so I can say docker compose down and that's going to just stop both those containers so that's pretty much an introduction to docker compose right there so we started our week declared what we wanted to do or what containers is startup and our docker compose that ml file we're able to connect to the database instance because docker set up basically a network that we could access and we use the DB as our host because we use that there we also set up a volume here to store our Postgres data PG data and we can add that to our get ignore because we don't that show up on github and then other than that we use docker compose up to start the containers and docker compose down to shut them down and then I used blogs to show the logs for docker compose and there's a lot more commands you can use for this this is just introduction to docker compose it has a lot of cool things you can do with it so I recommend giving it a try you
Info
Channel: Ben Awad
Views: 54,502
Rating: 4.9222107 out of 5
Keywords: Docker Compose, Docker Compose Tutorial, Docker Compose Tutorial with PostgreSQL, Docker Compose Tutorial with PostgreSQL and Node.js, docker compose postgresql, docker compose node.js
Id: A9bA5HpOk30
Channel Id: undefined
Length: 11min 48sec (708 seconds)
Published: Wed Oct 24 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.