Docker Tutorial (+ Node & Postgres setup)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello class in this video we're going to be learning about docker what docker is and what it's used for and also we're going to learn from scratch how to set it up and how to create images and containers which will learn what they are and also at the end we're gonna see how to create an app that has multiple containers that talk to each other and works perfectly fine before we do any of that what is docker so docker is a tool that allows us to create things called containers now what are containers containers think of them like virtual machines and if you don't know what that is it's like a machine that is running as a piece of software on your computer or on a server and it has its own environment separate from the host machine so i'm on a windows right now and i can have a virtual machine that has linux and some other dependencies and software that is not on my main machine now there are some differences between containers and vms to actually let me google this so let's say containers versus vms and there should be some cool images here um actually yeah this one so these this is the main difference between containers and vms they're pretty much the same the infrastructure is the same meaning your computer or like a production server or whatever and also here at the top is the same both will have binaries and libraries and your applications running on them but the difference lies here in the middle each separate virtual machine requires its own guest os that says here which is an operating system meanwhile containers don't containers run all on one operating system and then the container engine manages the different environments and allocates resources properly now this is the main advantage because obviously the operating system will take a lot of space and processing power and also some other resource provisioning utilities that are hard to manage on different vms but a container engine in this case docker makes this really easy and by the way if this is a bit too complex don't worry all of this is managed in the background i'm just explaining the advantages of containers over vms now you might be thinking okay that sounds cool and fancy but what is the use of docker to a developer so there are a couple of advantages one of them is you can set up development environments really easy like with one command like let's say for example i mainly work on node.js apps and then sometimes i have to work on a project that involves like using django with python i can literally just have one command that pulls an image from the web that has all the dependencies for django and python and then i could just run that one command and boom i have a container that runs that environment and then i can run any python code or django code on that container execute it and develop it and then once i'm done i can just run another command which will remove that entire environment from my pc so i don't have to install all the software and also i don't have to uninstall it at the end also if you want to run multiple versions of the same piece of software the same dependency when you're developing let's say you had like a piece of uh like a library that required node version 12 but you have on your pc node version 14. you can just spin up a container that has node 12 and then put that dependency on there and then use it with your machine or with another container and you can have them working both side by side this can also be useful when testing if you're making a package and you want to test it on different versions of the same library you can spin up multiple containers with different versions of that library and then test it on all those containers on the same machine and also it can make it really easy to sync up environments with other people like let's say you're working on a company and then you guys hired like a new developer and then now you want that new developer to have the same environment as you because you don't want them to write some code and then push it to production and then it will fail because they're using a different version of some package and then when they pushed it to production it it failed or if someone pulled the code it failed on their computer you can literally just send them a docker image or a docker compose file and they can run one command and they will have all the dependencies and the environment exactly the same as the other developers in that team now there are other advantages that but i don't want to get too much into the theory you know me guys i like to teach by actually doing i think that way you memorize and remember things the most all right i might have lied when i said from scratch because i already have docker installed uh you can install it yourself it's really easy just click on get started in docker.com and here download it depending on your system i'm using windows so i downloaded that and i just installed it i think at the end of the installation of docker it's going to ask you to enable hyper-v and containers and i think it's going to give you a pop-up and you just click yes and that will be done automatically if not you can always go to yeah turn windows features on or off and you can enable hyper-v here all right so you can pause the video install docker and then come back when you're done all right now that you installed docker we can actually start to use it but before we start to use it there's one important thing that you need to do which is to hit like on this video so that it gets out there and more people get to learn docker thank you all right let's go to the terminal okay so here in my terminal to test that docker is running and by the way when you start it you're going to get this system tray icon yeah this one and if you double click on it it's going to open this docker desktop gui but we're not going to use the gui we're going to use the terminal it's just good practice to get used to the terminal because sometimes you're going to be on your server and you're not going to have access to a gui and you're going to get stuck there so in the terminal just to test that the docker is up and running you can just type docker and hit enter and it should bring up all these commands which means that docker is up and running so to get started with something that can be a good example on how docker can be useful is let's say i want to use node.js and i'm going to be using node.js because i'm sure most of you guys will be familiar because of the type of stuff that i publish on this channel so let's go ahead i'm going to google something in google node docker and hit enter and we're going to get this link hub.docker.com similar to github this is where people put their docker images and share them with other people and this is where you can get some useful images to use for yourself or you can as well upload stuff here just like in github so here i'm in this note page here's telling me i can run docker poll node which pulls the image and if you scroll down you can see all these different tags which are different they're different versions of node in this case and also different different linux distributions here which don't worry about them too much i'm just showing you here if you want to pull a specific version you can just pull one of these so let's go back to the terminal and let's run that command let's say docker pull node and hit enter and now it says it's using a default tag latest and now it's pulled this image and we can see the images that we have by saying docker images and there we go so i have a couple of other images because i do actually use docker as well and here we have this node image with the tag latest for okay and i want to quickly uh explain what images are so basically an image to a container is what a blueprint is to a house so when you have an image you can spin up as many containers as you have same thing when you have a blueprint of a house you can build an entire city full of houses of the same design based on that blueprint so an image is what's important is what you share with other people and then they pull the image and then create a container based on that image which is a piece of software that runs whatever that were there was in that image and you can check which containers you have by running docker ps and this will show you only running containers so if you want to see all the containers even the ones that are not running you can say docker ps a or you can use also a different syntax by saying docker container ls both work just fine and of course you can use dash a for this as well and now we can run that node image by saying docker run node and hit enter and it actually just uh yeah actually just exited because running node doesn't do anything it created this container but it instantly exited as you can see here now you can say docker oops docker run and do dash dash help to see what flags that you can use to have different operations we can say docker run dash i t which allows us to plug into the terminal straight away after running it and now we can say node and there we go we're inside a node terminal we can say console.log and say hello world and there we go now notice the difference here we have node 15.10.0 and if i open a node a new terminal so this will be on my machine and i'll say node.v you see that i have 1455. so you see indeed i'm in a different environment with a different node version and even if i didn't have node on my machine i would have been able to have this terminal and have this container run node just fine so let's exit out of here and now let's create a an example up to show you how you can run an app on a container so here i have this folder called docker tutorial you can create any folder and i'm going to create a file here i'll call it server.js and here this will be a very basic express app so here i'll say const actually let's install in this directory let's install npm install express i'm showing you an example while installing dependencies even though you don't need to do this you can just have a package json actually let's say npm init dash y so we can have a package json file and in this server js it's just going to be a basic app so we'll say const express equals require require express and we'll do const app equals express and we'll have just one route and we'll say app.get slash and we'll say request response and here this will just say rest.send and it will send hello world with exclamation mark and here we'll say app.listen and we'll listen at port 5000 and then once the server starts we're just gonna console.log and say server up on oops on port 5000 save this and we can run this just to test it node server.js and there we go we get port 5000 printed and we can say in a different terminal curl localhost 5000 and we can get hello world all right so how can we put this in a docker container easy we're going to create a file in this directory so new file and we're going to call it docker file and it doesn't have an extension so a docker file is like instructions for docker to build an image by the way you can get the extension so you can go to the marketplace and get the extension called docker for vs code and this will give you syntax highlighting and also suggestions here in this file and also give you this tool here that allows you to start and stop and manage containers and images from inside of vs code which is pretty cool i mainly use it for the syntax autocomplete and highlighting in the dockerfiles alright so i'm going to close these so here we need to say at the beginning we're going to say from and from usually takes a base image so we want to base this image on the node image so we're going to say node i'm going to do colon and say 15 so what's after the colon is the tag and i want to use version 15 and then here next we're going to say workda which sets the working directory which usually is slash up which creates a folder inside the container and navigates into it because we want to put everything in this slash app so we say this now next thing i want to copy everything that's here into the container so i'll be able to run it from there so we can simply say copy and you can say dot and space dot and if you find the syntax a bit confusing you can just say dot slash dot slash this works as well and what this does is it copies everything from here to slash up because we set it as the working directory and now when we copy the files we want to start our server so we can do this using something called cmd which allows us to run a command when the container is created and here this command will say it will be running the server.js using node so it will be node server.js but here we're going to say node in one string and then next to it we're going to put another string in the array and this will be server.js so this is the syntax for this it's just going to run node server.js so let's save this and now we need to build this and to build this in the terminal let me close this in the terminal in the same directory where we have this docker file we can say docker build dot and when you say dot it's going to look in this directory look for a docker file and if it's different you're going to use a flag dash f and give it the name of the file in this case it's docker file so we can just say dot and we can say dash dash tag and give it a tag to give it a name i'm just going to call this node server and hit enter and now that's finished as you can see here you have a couple of instructions you have this from docker which is from the image the base image and then you have so one out of three and then two out of three is setting the work directory and then three out of three is copying so it's going through the instructions in the docker file and putting them in different layers inside the container or inside the image so now that's created you see we get an id here and we can copy this id and we could say docker actually we don't need to copy this side you can just say docker images and there we go we get our node server image so now let's start it so we'll say docker run so to run a container we need to give it the image which in this case is node dash server and notice it's the exact same syntax that we used to run the node image from the cloud by default when you say docker run it's going to look in your machine and if it doesn't find it it's going to look on the cloud in this case we have one on our machine so it's going to run our image and here at the beginning i'm going to give it dash dash name and we're just gonna call it node server container for now and hit enter and there we go we get server up on port 5000 and if we open a new terminal and say docker ps we get this container that has the image node server and here has the name node server container and is up and running but if we do curl localhost 5000 we're not going to get anything and that's because containers by default are isolated and this port 5000 is not open to the public so we need to open it and that's easy so let's go back let me close this terminal let's go back to the other terminal control c to stop this and by the way when you stop it so if you do docker ps a the container is still there so let's actually delete it i'll show you how to delete the container so here node server container copy that and then we can simply say docker docker rm for remove and then paste that name and hit enter okay it's still running okay so we need to stop it so here let's say docker stop and then give it the name of the container and hit enter okay now we can actually delete it so do docker remove node server container and now that container is gone so let's recreate it with the exposed port so let's say docker run node server our image and now let's give it the same name so dash dash name node server container we can actually say up because they're all containers and now let's expose that port so we can simply do that by saying dash p for port and we're gonna say 5000 colon 5000 what does this mean it means that this second 5000 is the internal port which that app is running on and this first 5000 is the port that is exposed to the public which is what we are going to use to access this from other sources and i'm going to add another flag here say d which runs it in detached mode which means when we run it it's not going to hang and just stay like this and hold the terminal it's just going to stop or it's just going to like go away so now if we hit enter there we go we get the id and now it's running now if we do curl localhost 5000 we should get a response and we indeed do get hello world cool all right let's say we want to update our code and rebuild it again so that's easy so i want to do something here we can say const or actually not cons we can say let count equals zero and this place encounter to show the user every time they visit this is an example i saw on the docker page which is pretty cool so here we'll say whenever someone requests the slash endpoint we're gonna say count increment it so count plus plus and then we're going to send back and we're going to say here do backticks and say hello world this has been visited and then do a dollar sign curly brace and concatenate the count and then say uh times and remove that make it backticks and save and now to rebuild it we can simply say docker build dot and give it the same tag tag node server and there we go now it's rebuilt it if we do docker images we get this node server and the old image is now without a tag so that's preserved in case you need it and by the way if you want to remove let's say an image if you want to remove this one you can either remove it by the repository name the image name or the image id so let's say when i remove this because we don't need it anymore you can just copy this id and say docker you can either say docker image rm or you can say rmi for short which removes an image and then paste the id okay i think there's a container that's using this image so we're not unable to delete it so let's say docker ps yeah we have this container running so we need to actually stop this container and we're going to say docker stop and then paste the id and by default it wastes 10 seconds to stop it you can override this and say dash t 0 and that will stop it instantly okay i put the image id my bad we need to put either the container id or the container name so i'm going to take the container id and then run this same command but with the container id instead of the image id run that and there we go that's stopped and now we can remove it so docker rm and paste the id so now it's removed we can actually remove the image so let's take the image id again and then say docker rmi and paste that image id and there we go that image was deleted so let's run a container with this new image node server so again let's say docker run let's give it the name of node server app and it's going to use the node server image important the image needs to always be at the end so again we need to expose the port so let's say dash p 5000 colon 5000 oops 5000 and let's also say d so it's detached and we can escape out of the terminal so hit enter there we go so now if we say curl local host 5000 we're gonna get hello world this has been visited one time i should have been times but whatever so if we request again we get two three and so on all right so our code has been updated and our image now has the new code now let's look at our docker file so here this first part is fine and then we're copying everything which in this case includes the node modules which is not ideal because it's kind of slower let's we need to install the dependencies on the image itself so that's easy we can after we copy everything we can say run and here we can give a command that runs with the image is being created and here we can simply say npm install and now to make sure that the node modules is not copied we can create a new file here a dot docker ignore very similar to git ignore so here we're going to say node modules slash and now it's actually going to notice and it's not going to copy the node modules and let's also say docker file here because we don't need the docker file itself inside the image so save that and now let's rebuild it so let's say docker build dot oops dot and dash t short for dash dash tag and the tag will be node dash server so run that and there we go now you get four out of four run npm install and cool now the image is created let's uh stop this existing container yeah let's uh docker stop node server app and do t0 and let's remove it so docker remove node server up and now let's run a new container based on this new image so let's say docker run again dash p to expose the port 5000 and then colon 5000 dash d dash dash name we'll call it again node server up and the image is node server so hit enter and there we go it's working exactly the same so if we do curl localhost 5000 of course it's reset to one because that's not in a database or anything we can also actually execute commands inside of our container so for example if we wanna look inside the container what there is there we can actually execute bash and browse the file system there so we can say docker exec which stands for execute and we need to give the name of the container which is node server up and the command that we want to execute which in this case is bash and for bash to work we need to give an extra flag dash i t which basically allows us to open a terminal there so hit enter and now you see we're inside the container if you type ls you'll see that we have node modules and package json and server exactly like here and notice we don't have the docker file because that's in a docker ignore so now we can run commands here and we can also exit out of here we can run commands just like standalone commands for example let's say here actually without the it we can say for example want to see dependencies we can say npm list and hit enter and there we go we get we have express and we get some like notice here so you can have actually a container that just basically has node and you can use that container to run npm or node on a machine that does not have either of them now let's say our app got a bit more complicated and we needed to use a database or app and we wanted to also isolate that in an environment now you can put the database inside of the same container but that's not recommended usually it's recommended every service should be in its own container so that it's more maintainable you don't want to update the container that has the database engine every time you change the code because those are separate things so let's look into using multiple containers and have them talking to each other so for this i prepared a code base so you can clone this i'll put a link to this in the description and this is just a simple app a simple node.js app that uses a postgres database so you can go ahead and take the link to this and clone it so i'm actually going to delete everything here and clone this instead so i'll say rm and just delete everything here actually i'm just going to say r cool okay left out the docker ignore so i'm just going to remove that as well so now i'm going to say get clone and paste that and clone it here say dot i'm just going to walk you through this quickly so we have an entry point server.js which imports express and morgan and then starts an express app which has these two routes for fetching users and for creating a user and then it starts the app and then we have this db file here that starts a nex which is a query builder instance and that connects to a database and then we're going to use that database to manage our users and then we have these two scripts here which migrate the database create the users table and this seed script which creates two dummy users you can actually set up the credentials here to your local and test it and run it on your machine before running on docker to get an idea on how it works i'm going to skip that part because i don't want to make the video extra long so i'm going to recreate the docker file so here i'll create a file let's call it docker file again actually i should have kept it because it's almost identical so here we'll say from node colon 15 and here we'll do our work directory and this is slash app and here we'll copy everything so copy dot to dot and we'll do our run npm install and we'll do our command and here actually i'm gonna say npm start and save because if you see the package.json we have npm start which starts our server.js so we'll use that so we need to create a docker container that has postgres engine on it so that we can connect to it and manage our data there so let's go to docker hub and search for postgres hit enter and we get here this most popular image just named postgres and we can scroll down to see some extra flags on how to use this so there we go there's an example here docker run and then you give it the name just like we did and here there is this dash e which sets an environment variable and in this case you can set postgres underscore password to set a password for your default user so we can actually just copy this so copy all of this and then let's go to the terminal and paste that i'm gonna rename the name so here we'll say i think i have another postgres container so i'll call this post grass dash docker and for the password let's set it to one two three four five six and it's detached but i'm gonna add something extra i'm gonna add dash b so that we can access it from this machine right now so the internal one the one on the right i'm gonna expose the internal port four five three which is the default postgres port and i'm gonna expose it as four three two one just to show you how you can use it differently because it's easier to understand that way now let's hit enter it doesn't find postgres locally it's gonna download the image from the web so i'll wait for that and there we go just this easily we have postgres running and to prove it now because i have postgres installed i can actually connect into it but even if you don't have postgres installed you can do that so using psql we can say psql and say actually we don't need to say the database or actually we do so we'll say d postgres because that's the default database that's created and here let's say dash h for host and this will be on this machine so localhost and then say p to give the port and this will be four three two one and hit enter and i'll ask us for the password okay so ask me for password user class that's wrong i'm just gonna exit because the the user by default is postgres so i'm going to add the flag dash uppercase u postgres and hit enter and now i'm going to type one two three four five six because that's what we set hit enter and there we go we have postgres installed in this container and we can access it we can list the the databases and we can see them there and now we can oh actually exit work cool and even if you don't have postgres installed you can just open a terminal inside that container by saying docker exec dash id and we called it postgres docker and you can run actually you can straight away run psql there and hit enter okay raw root does not exist so we can add the flag dash u postgres and there we go we don't need the password because we're running it from inside the machine and we have postgres cool so let's connect our app with this postgres database so actually just to test it i'm going to say npm install and here inside dbjs let's replace the credentials here so the user is postgres the password is one through six the database i'm just going to set it to postgres actually interesting i don't know if this takes support i think it does yeah so let's say port 4321 save and now this should actually connect we can test it by running the migrate script so npm run migrate hit enter there we go it says user created users table and to double check we can execute the psql command inside the container again and then list the tables backslash d there we go get the users table so our app on our machine is connecting to this postgres inside of this container now the next step is to build this up into a container and then have those containers talk to each other now there are two ways way number one is a bit dirty to be honest which is to expose the ports like we did here and then have that container talk to it through our machine way number two is better which is to create these two containers together inside one network which is another concept that we're going to touch on now that way they can talk to each other without exposing any ports we can do this through the terminal but i'm sick of typing like docker run p like it's a lot of like flags okay so we can use something called docker compose which allows us to write this stuff one time and then not have to worry about again so how does that work so let's create here a new file call it docker dash compose dot yaml and this the docker composes a file that has instructions on how to create multiple images for multiple services which are containers that we need for a certain project for this we need to first we need to set a version and right now the latest version is 3.9 so i suggest you use the same in case in the future it changes and you'll have some problems so for this you should use the same as i'm using 3.9 and here we're going to say services and this is a list of services aka the containers that compose our project so first here i'm going to call this server and be careful with indentation because yaml is very strict if you make a mistaken indentation is not going to be able to pass it properly and here to build this image from this docker file we can simply say build and the value will be dot which will tell it to look for the docker file inside of this directory and here we can say ports and these are a list so we can expose multiple ports but we just need so put these in uh quotes just in case it has problem passing special characters so it will say 5000 colon 5000 so we don't have to type it in the terminal every time and that's it for the server for the node server image now let's go back to indentation spaces and here let's put the container for the database and i'll just call it db so here we're not going to use build we're going to use image which takes an image from the web or even locally if you have it because we don't need a docker file for it it's already made so here this will be a string and this will be postgres now i want to give it because for this postgres we gave it earlier a postgres password i want to give them through here and to do this you can use environment environment and here you can pass as many environment variables as you want so let's check the docs again yeah so it's this postgres password so here go back and do postgres password and then this will be let's set it to the same one through six and we can also set uh what can we set let's see yeah we can set a default user and based on this default user a default database with the same name will be created so here for example we can say postgres user and oops and we can call this docker and here actually we can do the ports as well so here we can say ports here that we're gonna expose to four three two one we're gonna expose five four three two so save and now let's go back to our terminal i'm actually gonna remove that existing yeah actually both these existing containers so i'm gonna take their id so here we'll say docker stop and paste this actually can we stop two at once i think we can so dash t zero run nut cool yeah it stopped both and now i'm gonna remove both so here docker rm and that's gonna remove both cool so let's check images i'm gonna remove both these images just to have a clean slate probably for you it's gonna be empty when you remove everything so it's easier to tell which is which and also it's good to practice these commands so that you remember them repetition is practice and practice makes perfect so here docker rmi to remove images and i'm going to remove node.server and can i tag this by id and this by name so paste that cool so it removed those images now to build these services in this docker compose we can simply say docker dash compose up and here let's do dash d so it starts in detached mode and there we go it's building server so that's done and notice it doesn't say building postgres because that image is already built and it's on the web and there we go it's created these two containers and it's giving them the name or the prefix docker tutorial because it's the name of the folder that they're in and we have db one and server dash one let's check if this is working let's run a command to migrate the database so let's say docker exec and we're gonna execute this on docker tutorial underscore server 1 or whatever the name of the container for you and we're going to execute npm run migrate then hit enter okay we get an error and let's see error connects reviews okay so it couldn't connect to the database that means it couldn't connect to the other containers so oh okay my bad i forgot to change this this is localhost this shouldn't be localhost because on that machine localhost is that container itself which the database is not there now one nice thing one piece of magic that docker compose does is we can hear say just db which is the exact same name as the service and docker in the background is going to replace this db with the address of that container which is really nice and also we're not going to use this port we're actually going to use the default port of postgres and i'll explain what happens in a second so let's rebuild this first let's remove those containers and we can actually say docker dash compose and we can say down which will remove those we will stop those containers and actually remove them which is really nice we don't have to manually remove them so if we do the docker ps dash a as you can see those uh docker tutorial beginning containers are actually gone but the image is still there because the image is important you don't want to delete it this way you want to always delete it manually which is the proper behavior so to rebuild this we can say docker dash compose up but if we do this it's actually going to start the containers based on this existing image but we can add the flag dash dash build and again add d and what the dash dash build does is going to check if anything has changed which indeed it has and then it's going to build it if there's some differences so now if we run this it's actually going to recreate the docker tutorial server image and now if we do docker exec and run the command on this docker tutorial server 1 and we run npm run migrate and hit enter okay we get another error okay my bad so i've another mistake i forgot to change the username because in the docker compose we set it to be docker so i'm just going to change this to docker and the database will be docker as well because there's a database created based on the username like i said earlier so okay so i'm just going to rebuild it again so change that and let's do again a docker composed down and again let's rebuild so docker compose up dash dash build dash d and third time is the charm so this time it should work so now if we execute the same command npm run migrate there we go it created the users table and we can actually in a different terminal check again so let's do psql-p 4321-d docker dash u docker and hit enter and the password is one through six and if we list the tables we see that there is a users table and if we select its select star from users it's empty and now if we run the seed command which creates two users we get added dummy users and if we select the data we see that there is data added to that table so we're able to connect to that container with the database from the container with the server and if we do curl localhost 5000 slash users we get our users and we can actually add users so we can do a post request we can say curl dash uppercase x to give it a method and we'll say the method is post and we could say d to send some form data and here i'll say name equals ahmed to add my name on the list and then execute this and then if we fetch slash users again we get ahmed is added to the list and if we check again in that database ahmed is added to that database so we're able to interact with it through the api as well so that's cool now there is a little problem with this which is if we run docker compose down it's going to stop and remove our containers which is a problem in this case because in our database is stored inside the container now we can leverage a concept called volumes in docker which is a separate directory where docker will store certain files that you want to have persisted in this case we want to create this container with our database but we want the data in it to be persisted and not removed anytime we remove that container or even the image itself so how do we do that so let's go back to the postgres yeah and postgres image in the docker hub and we can scroll down yeah so here it shows us an example where they run a docker container based on this image but pay attention to here there's dash v actually let me zoom this so this dash v and then it gives it and then they give it a path custom path and then colon and then they give it this path which contains the data of the postgres engine or server we can do the same through the command line or we can also do it on the docker compose file so we can go back to the docker compose and here for this db server we can add another key down here on the same level as environment and we can say volumes and this is usually a list and here i'll call this data and do colon slash and then we're going to actually paste this path so copy that and paste it here and actually here i'm going to declare another volumes key which is the volumes for the entire network so here we'll say volumes and we'll say data and i'm just going to keep it like that because we don't need more info and by the way we can remove this port from here because we don't need to export this port or expose this port because by default in docker compose creates a network that allows these services to talk to each other and we don't need to access the database from outside so we can keep it locked like that so save this and let's go back to our terminal and now we can say docker compose up dash dash build dash d hit enter and now you see first thing is it created the network it did that already earlier as well and now it created a volume docker tutorial underscore data so now we get exactly the same thing happens but let's say can i find that command yes let's run the npm run migrate to migrate and then let's run seed and then let's run actually let's fetch the data you'll see that it's of course reset to the dummy users data now let's run the command to create a user and i'm going to add myself and i'm going to maybe add i don't know another user maybe chris and now we have four users but now if we say docker dash compose down and if we remove the containers the data will still persist and where does it persist it persists in let's do docker volumes ls actually not volumes volume ls and you'll see there are a couple of volumes that are not related to this but there is this one volume which contains the data for our postgres database so to prove this we can actually just run it again so we can where is it docker compose up we don't need to do dash dash build because we didn't change anything but we can still do it anyway so hit enter cool everything is created and if we run curl local host local host 5000 slash users we're gonna get that data and it's the updated data so our database data is actually persisting and not removed anytime we remove the container or the image itself so this is a really cool way to preserve your data there's a lot more to do with docker with deployment and using things like kubernetes for orchestrating complicated projects with many images but i believe this information that you have here is modern enough to get started and to use it in a useful way for your projects and of course not too much that it overwhelms you and you don't even retain any of it all right that's it for this one you'll find this code in the same repo but probably on a different branch if you need access to it and please again make sure to like this video it really means a lot and helps get the channel growing and of course subscribe if you haven't done so yet and i will catch you in the next one peace
Info
Channel: Classsed
Views: 7,002
Rating: 5 out of 5
Keywords: docker nodejs, docker tutorial, learn docker, docker postgres, docker express tutorial, docker for beginners
Id: Dm0CmZz-QyI
Channel Id: undefined
Length: 40min 36sec (2436 seconds)
Published: Fri Feb 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.