Docker Volumes with PostgreSQL Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's up I'm bringing you a quick video to discuss an interesting topic called a darker volumes so if you have been a subscriber of this channel you might have seen me use docker a lot to spin up databases message queues and other interesting web servers right like proxies I use docker all the time and if you have noticed that every time we use docker we ended up with a ephemeral kind of data where the moment we destroy and remove the container our data is gone okay so let's explain why is that and how we can use docker volumes to persist the data of the container in a place that we can see copy back up and we can essentially make a backup of and spin up another container and reuse that data on another container so how about we do that okay so here I'm gonna use Postgres as an example and I have spin up PG admin here and I don't have any Postgres instances as of now so I am going to go ahead and spin up and it Postgres instance let's go ahead and do that using docker okay and obviously to know that you have darker just do docker run hello word make sure that you get this hello from docker and if you do that that means you're ready to rock and roll okay and alright so I'm going to spin up a docker container with a post Christ instance in it and to do that it is extremely simple you can do docker run and it's always a good idea to give your container name so I'm gonna give it a name called PG and we're gonna give it a port because Postgres is a client-server application so it's listening on some port and I want to expose the port 5 4 32 she's earning on the container here - five four three two which is running on my host right so I'm listening on 5 4 32 on my host ok assuming this is not in you and then finally the final thing is you specify the image from which you want to pull the post Chris I think this is Bosco's 12 forces the latest and if you run obviously what will happen here is and you specify the I didn't specify any data or anything so by default when you do that docker will pick a random directory on your host machine and use it as a volume and will store the data because Postgres will need data guys right it's a folder with all your databases and indexes post stored procedures and all that jazz right so now I can if you go to PG admin I'm gonna connect server connection let's go this Hossein mac and using the default thing post case post case that's the default password right and we're gonna call this PG right and if I connect I am obviously gonna establish the connection if I can spell my name correctly that's the hostname that's my machine name here and the port is for 450 that's the default port and that database is always you will get the data base for free here which is called post Chris and this is that no tables so go ahead if I create a table right now and called it I don't know employees right and there's the thing it's an employee and the employee table and I can start doing stuff and this I create tables like a data and that's it however if I go back to my container and stop it right if I stop my container and try to connect what will happen disconnect server and that's just go ahead and try to connect the server again right I'm gonna get an error right because the server is no anger no longer running that's not a problem because my containers store its pose is stopped it's not deleted right so I can do is do docker start PG that will start my container up again so I came connect again and you can see that I still have my table here so if I stop my container my container crash the data doesn't go away because the containers still there correct and it's referencing some folder that I have no idea about okay that's the problem so if I do darker stuff PG and I then do and I then do darker or n PG which means remove the container and then I do the same command to run the container again this will be a brand new container that means it will be a brand new folder with the data that it dumps in that means I just lost all my data let's confirm that so I'll spin out the database instance again go back and if I do write to click disconnect right and then write it click connect again put the password all right if I go back there are no tables because employees is done there's no data this is a brand new instance for the first time it's running how do I start up a container with Postgres have a bunch of data and and start adding a lot of data may be pouring million rows how do we do that it is extremely sample right so I'm gonna go ahead and do docker stop PG docker or MGP okay all right and here's the thing before we start the container here I want to specify a folder and in this case this is my folder a C user Huseyin data and I'm gonna create a folder here called PG it's blank right but here's a trick what I want to do is I do docker run if I can't find the command docker run name PG port and here's the thing before we end that I want to do - v1 is the volume and the volume on my host is for users the same Nasr data PG map pink - the volume or the data path of Postgres in the running container which is almost fixed all the time so it is var lab post grist sequel and then slash data so that path is always gonna be the same path on the container because it's the container is built up from I'm an image unless the image changes that path then we don't have to worry about it right that's why it's always a good idea to fix an image right you can always look like spaces here and then that maps to this folder so guess what now when I spin up that container it's gone I'm gonna start dumping all its data into my path that I specifically asked it to dump its data in that means I have full control on this folder I can back it up obviously or not while the Postgres is running right you can stop the container back up gzip it right and then maybe just destroy the container or run your test against this dead a post-christian instance and then once you're done extract a brand new folder and then point it again let's let's do this let's show you how to do that okay so what I'm gonna do here essentially as I'm gonna go ahead and reconnect obviously this is gonna be an empty right this is gonna be a brand empty database and I'm gonna go ahead and create another table here some table I don't know URLs okay so we were building a your shoulder for example and then let's build another table you you start doing your data you stop doing your stuff right just doing your stuff and here's the beautiful part if I go to finder the path itself and going to that path look at that peih-gee this is the data that it just was populated with data that I have didn't didn't do anything with right I just pointed that docker container to this folder and it's just a filling stuff and here's the critical thing now I have some data here I have some tables so what I would what I can do is I can just decide to stop container yeah I can make a backup of this guy right I can do tar - how do you do that SC z compress will this gzip and the file as let's call it PG tar.gz and I want to compress this folder okay and just like that I have now a compressed folder of this thing so so as you can see now I have a backup of my data and I can I can just decide to copy the entire directory into a completely different directory I can take this itself I'll take it to an entire other machine extract it there and spin up the container with that data so that's how about we do that right so let's just copy this folder PG as our means everything including the content from PG to Anna for this folder - PG brand-new okay and just like that I have PG brand-new right with a bunch of data here so now if I spin up a completely different container in a completely different port let's do that how about docker run give it a name PG brand-new right just the name and then the port will be just for fun I'm gonna use one two three four okay and let's just do this way and then mapping to port five four three two and then the volume is what there's the same Nasr data not PG just to be five ANSI right I want the data to be PG brand-new just cause right and then this maps to var let Postgres sequel and then data finally I won't spin our post quest database from the image right let's check this out and this will spin up the Postgres instance on a brand new pour remember this port now is one two three four my container technically is stopped right or maybe you can go ahead and remove it and that's even fine if I go back to my Edmund I have to establish a new connection this is no longer working obviously but if I stab leshan new server that's called this PG brand-new I should expect my employees and URL stable so the port is one two three four right and then same thing Postgres and Postgres and then save and then just like that go there and look at that my beautiful two tables just because I have the folder and I have zipped it you can take that zip file you can take that tour in two completely different machine complete different hosts with the with the darker there and essentially extract the host there and you can automate that process right and extract the folder there and spin up another container point the volume to that folder and I said just like that you have the data on another machine right but that's it essentially it's all really replication is just copying the data so the use case of this as you want to essentially build the test data like I have a huge set of test data and I want to run my tests against it but the the Mondial on my test the data is will be corrupted that that's it the data will be ruined so what do I do I want to make a backup of the database well you can do a PG clone which is very complicated but it is where is the use docker and have a zip file right extract a spinnaker container have your application connected the container right the application that runs the test connect the container run your test and once you're done destroy the container destroy the volume because you don't need it you have a backup right and when you want to run another test again on your newer version of the application extract it again spin up a brand new container and that said you always have a fresh copy of your data and that's just one use case of where you can use volumes volumes or powerful stuff right and yeah it's visually with kubernetes the volumes is always being used behind the scenes always especially because containers are stateful by design so if they are they are continuously being destroyed from one part and spin up and another apologist could be completely different host right so yeah that was like a quick video guys showing you how to use docker volumes on docker I hope you enjoy that see you in the next one you guys stay awesome
Info
Channel: Hussein Nasser
Views: 11,335
Rating: 4.9408283 out of 5
Keywords: docker volume, docker postgres, volumes in docker, docker postgres volume
Id: G-5c25DYnfI
Channel Id: undefined
Length: 13min 37sec (817 seconds)
Published: Fri Feb 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.