Docker and PostgreSQL in [10 Minutes]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to show you guys exactly how to spin up a darker container running a Polish grass image and then I'm gonna show you how to bash into it as well as how to connect from an external application we're gonna be connecting from our local machine as well as from a spring to application so if you don't have the docker installed on your machine go ahead and navigate to docker com4 slash get docker and you can see that it's available for all of these operating systems so I'm gonna Mac so I use you know the app whole system but if you're on Windows we're going to go ahead and pick the one available for you and you can download a darker Community Edition and also you need to actually create an account in order to be able to download darker so once you download install docker then if you navigate to your terminal or command line if you type in docker - - help you should see a bunch of commands that you can use with docker so what I want you to do first before we actually you know start with this tutorial is if you go to docker hub so help docker dot-com and then browse for Postgres right here you can see that you have you know this Postgres image and these are all the versions right here with the tags essentially and then you have a quick reference you know you can learn more about Postgres so Postgres it's an object relational database and then they have an a guide on how to spin up this instance so the first thing that we need to do actually is to pull up this docker image so we're gonna pull up this so we're gonna download it essentially we're gonna download this image into our local machine so to do that let's go ahead and simply open up terminal and I'm gonna clear that I'm gonna say docker and then pull and then post dress so don't just simply say Postgres because right here you see that we have different tags so right here you see we have nine point six point nine and these are all the questions so what I want to do is to actually get the Alpine version because it's the smallest image size and that's the one that you should be using if you are testing out things locally so I'm going to go back to my command line or terminal if you are or actually terminal or command line if you are on Windows and then simply say Alpine so this will actually do nothing for me because I already have this image installed actually downloaded so for you you would actually go through a series of steps and then you could actually see you know what you needs to download to construct this image so now that you do have this image downloaded what you can do is simply say docker and in images and right here you see I only have one image which is the docker actually which is the Postgres and the tag is a behind so now what I'm going to do is if I open up the docks and scroll down so right here you see that they say how to start the Postgres instance so let's go ahead and use that command to start an instance so just let me pull that up and pull this down a little bit just like that so you can see perfectly so I'm gonna clear the console and for us to spin up that instance is simply by running the darker command and then run and then we have to give it a name so - - name and then name it as post grass - zero so this could actually be any name that you you prefer but for now I'm gonna pick post quest - zero and then we have to pass an environment variable so - II Postgres underscore password and then you can pick a password so I'm gonna say password and then I need to run this in detached mode and then Postgres so this Postgres is the actual image so in our case we have the alpine version so simply say column and then up and also I almost forgot but we have to expose the port so this port right here 5 4 3 2 so we want to expose the container port to the outside world so to do that we're simply going to say - B and then 5 4 3 2 : 5 4 3 2 just like that so I'm gonna press ENTER and now you can see that this container is up and running so now if I say docker all lowercase dr. PS you can see that if I pull this full screen you can see that this container is running the Postgres Alpine image and you can see that status is up and right here you can see that it's exposed exposing the for the port 5 4 3 2 and this is the name that we have given it so now let's actually go ahead and bash into this container because we have a Postgres instance running inside of it so to do so just let me clear the screen run this command again because I'm gonna show you how to use this name right here so I'm going to say darker and then exec so exec and this will be in interactive mode and we want to exact inside of this Postgres 0 so the name of this content is Postgres to 0 otherwise you could actually use the actual ID but we have the name so what we want to exec is the bash command so - enter and you can see that there is no container Postgres and that's because I did actually misspelled Postgres so what I'm going to do is grab this name and then run the same command but right here actually in fact I said post guess so and then R here so this is a you know it's the wrong name but you get the idea so now I'm gonna press ENTER and now you are inside of the actual container so if i do if i type PWD you can see that I'm in the root folder if I do LS you can see that this is the Linux file structure so now if I try psql you see that I do have PS Cleo but I need to you know get the root access so in fact if I clear that and say psql and then - - help you see that these are all the commands that are available within psql so what I want is psql - and then capital u for username and then simply pass the username which has the root access so Postgres so now we are inside of Postgres so all right so now we have access to all the psql commands and create databases and so on and so forth so let's actually go ahead and see the actual user that we have so I'm gonna say backslash and then D u and right here you see that we have the Postgres user and it's the super user now if you've done some kind of relational database now this is a time for you to shine with your SQL commands so let's go ahead and create a new database so create data base and then test column make sure to have : otherwise it won't work so enter so that works to view all the databases simply type forward slash or actually back slash and then D or actually L and right here you see that we have the test database so let's go ahead and connect to it so I'm gonna say /c and then test enter and you see that we are connected inside of this Postgres database called test so now if I go ahead and say backslash N and D we have no relations whatsoever so there you go guys we successfully managed to spin up a darker container running Postgres alpine and you know from now on you can you know create a ball so on and so forth but now let's say that we want to connect to this container running this Postgres image from an application so first let's go ahead and connect from the actual from my local machine or it could be from your local machine as well so make sure that first you have psql installed in your machine and then you'll be able to run all the P SQL commands from the terminal so I just want to show you that I do have Postgres installed my machine but you can see that the version is 9.6 and it's not running so what we want to do is to connect to this one right here which is running through docker so let's go ahead and simply say P SQL and make sure you have P SQL in your path and in fact if I say less and then call them actually tilde and then dot Z SHRC and you can see that I do have Postgres in the path so I'm gonna press Q and then what I'm going to do is simply say P SQL - - help so I like you guys to see you know all the commands available for s so the connection options are you know host so right here and then the port username and then the actual password so now let's go ahead and simply say P SQL and then - H localhost so we're connecting to localhost and in - P the port of it is 5 4 3 2 so remember that we did expose 5 4 3 2 so if I go up a little bit so the command that we did use so right here so this is saying expose the container port 5 4 3 2 to the outside world in the outside world will connect to it via the same port so you could actually you know change this port for something else but usually you just stick with the same port so now let me go back to my a time you know and then what I want to do is to actually pass the actual user so - and they'll you and then post dress and then enter now we need to specify the password remember the password that you defined so if I go back so right here the password was password so I'm gonna say password and now we are in so we actually inside of that container so actually that instance running Postgres so what I'm going to do is if I do you know the same commands so you know backslash and then L you can see that we have the test database if I go ahead and you know connect to test so test and then backslash D so there are no relations there so again so now we could actually go ahead and create a new table so in fact just let me go ahead and connect or actually create so I'm gonna quit out of this and then connect to the same thing password actually put of contact but it's fine so let me go ahead and create new type database so create database and then test - and then let's connect to test to test and then - and then create table and then let's say user without any fields or actually without any columns so enter no actually this is user so this can't be user so let's say you shouldn't enter and that works so user is a reserved keyword in Postgres so now go ahead and say backslash in NDT column and you can see that we have this table right here and if I go ahead and simply say backslash D and then the actual table so student and you can see that we have no columns whatsoever so there we go guys so one last thing I want to show you is how we can connect to the same instance running so now we connected by our local machine using terminal but let's say that you have an application and you want to connect to it so right now I do have an application right here and if you are interested to learn how I did this go ahead and check out my course on springboard and databases and I do cover how to create this entire application which is awesome so what I have here is a connection to this database and if I open up this migration so I'm using fly way for database migrations so right here I've got this table called email which has an ID and email and then I also have a table called student which has named course and then an email so let's actually go ahead and connect to it via this application so it's very straightforward really so all you have to do is to define in spring world and this should be almost the same in you know whatever language or framework that you are using it should be pretty much the same so I do have this application llamo and right here this is where this is define you know everything so I define the port the username the password the host and then the database name so and we have all the city JDBC URL so this is Java world so JDBC provides us you know bridge to connect to databases and then this is URL so you can see and what I have to do now is because you know inside of that database there are no table with tables whatsoever so if I go back and connect to tests and if I do what / d+ you see that there are no relations right so if I start this application so I'm gonna start the application just give you a second there we go so you can see that one migration was applied so first it was empty and then one migration was applied and what I'm going to do now is actually check the database so if I go back and run the same command and there we go so you see that we have email Flyway schema this is from Skyway and then we have student and if I do ad T and then on email you see that or actually sorry the plus on email you see that we have ID and email and if I do DT plus on the actual student you see that we have ID name course and email ID now let's go ahead and insert a student to this database so insert into student and what we want is the actual ID so name and then course so this student doesn't have an ID for now and in values and then what we want is you it and then generate so this is a function from Postgres this will give this will gives us a random you it and then john and then the course is the finance and then end up with semicolon enter and you can see that we have an error and that's because we have to install the extension to generate you it so it's very simple so create extension if not exists and then the extension name and make sure it's capital sorry double quotes so it's you it and then OAS SP and then enter there we go if I do select and then you would generate and then v1 invoke this function you see that we have a random you it so let's go ahead and insert the student so this would be v1 by the way and then oops v1 just like that invoke that enter you can see that worked select start from and then student semicolon otherwise it won't work and you can see that we have a student right here so again they may actually go ahead and hit my end point and see if we have John inside of the database so I'm going to open up my web browser full screen local host and then 8080 fort slash API v1 ford slash students enter and there we go so you see right here that we have John inside of our database and just to prove a point if I kill the container so just let me go back to the actual container so if I scroll down if I clear out of this and the quit is command P and then Q and you know the container now is gone so if I now try to connect to it via the console so my local machine actually terminal if I pretty much just quit out of this so for /q Postgres and then the password is password actually this the works and that's because we need to kill completely so to kill the actual container so if I do docker PS you see that this is the I ID so I'm gonna say docker stop and then the ID enter just give you a second there we go and if I now try to do the same thing as I did so clear that and then connect to that and you can see that cannot connect to the server and it was refused and the same with my application so if i refresh this you see that this will hang here forever and eventually it will timeout so there we go guys I hope that this tutorial was useful because I know a lot of you sometimes born to connect we want to connect to real databases and there we go so docker is amazing Postgres is amazing and make sure to follow me on my Instagram so amigos code so this is where I provide a lot of tips programming tips quotes motivational quotes so just follow me because the actual community is growing also go ahead and join me to my brand new Facebook group where we can have discussions about any topics and eventually you can tell me what you want to see from me and I will make it happen and last but not least go ahead and visit www.scosche.com adhere so spring boot Postgres docker you know react yes so on and so forth and even java streams so i've got a bunch of more courses coming up so stay tuned make sure to subscribe to my channel and follow me on my Instagram as I mentioned and join me on the next video see ya
Info
Channel: Amigoscode
Views: 134,305
Rating: undefined out of 5
Keywords: Docker, Postgres, Postgresql, Programming, Docker Containers, Docker and Databases, Docker Images, Spring Boot, Spring MVC, Spring Boot 2.0
Id: aHbE3pTyG-Q
Channel Id: undefined
Length: 21min 6sec (1266 seconds)
Published: Sat Jun 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.