How to build, run and connect Spring Boot and PostgreSQL containers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey it's random code here and today I want to showcase how I can set up a spring boot project with a post Grill database inside each own Docker container and how we can connect them inside Docker containers and then in the end have an accessible spring boot API and in this video I'm very much going to be focusing on the docker side setup and running stuff so I'm just very quickly going to run through how my spring boot is set up and my SQL for my post Grill but most of this video is going to be focusing on how we should do all the dock part of this entire setup so firstly let's just have a quick rundown of my spring boot application it's a very simple spring boot application where I have a controller with an rest endpoint SL ususer SL ID where we can get some users by their ID is then passed through a service layer which then simply retrieves the user from a repository which is connected to our database it then simply Maps this user enti from a database to a user dto user data transfer object and the main takea away from this is that we ex is that we extract all the data from the database including IDs in this case we not want to provide the IDS so we map the US entity is only allowing us to get the name age and email for example where if we look at the entity it contains an ID name age email and a password in this case we only want to show name AG email and not ID and password we're doing some mapping some basic backend stuff a repository then connected through our user entity connected to the table of name users use the repository using our user entity we have a simple GPA query inut get user entities by ID but again it's very much not about the spring setup in this video but I just wanted to go through it quickly to have a basic understanding of how everything works so entity Repository service send it to the controller and then provide an input SL user ID that's the spring boot setup we then have a Docker file for our spring boot application which runs on ellipse 17 so we have run Java 17 we are then creating an argument the jav file saying that we need to find a Target folder from our current position and inside the target folder take the jav file and think of this jav file as the variable name jav file then copy this F J file as it is into our container with the name .j in like the root position and this then allow us to find any J file no matter the version number or anything like that and it would then just be copied into our spring boot do container it's more just a Java Elon container and we then run into with Java J and then poting to the jav file relatively simple so just one thing to note about this is that because you're running the jar every time you make changes in your code you need to run a maven clean and install to update your jar in your target folder it doesn't it's not enough to just change the code you also need to update the ja file but once again this would very much be at the end of a development cycle where we have some kind of like complete service so there shouldn't be many changes everything should be tested and ready and you then create your jav file and put it into a container so that is very much the back end setup we then also have a post quest in this case datase and I'm running post 16 just to define a specific version doesn't matter too much shouldn't change too much we then copy an init SQL file into this position of Docker entry point initi init dbb because if we copy this file like this it will then be run and then set up the basic environment for our database and we then set some environment variables we Define a post database name so going to be calling it company in this case use a random and a code password called code and I want need SQL file then very simply in this case just create a table called users matching the entity in the spring boot also we matching the setup of our structure for our entity in Spring boot these should be matching and just to have some data I'm inserting three users a John a Jane and a bob some random ages emails and passwords and note my ID is serial so it's going to be also incrementing so it's automatically going to be creating the IDS of one two three to then actually build everything I would do it one step at a time so let's first start by building our assistant layer so our post chis to container I would open a terminal and one thing to note I will push this entire project to GitHub and I have tried my best to create a read me file which should guide you through what I'm doing and you can also see all the commands I'll be running in the terminal so first of all I would build my or actually very first we would start by creating a doger network because when containers are connected we need to have them on the same network so we just simp do dogger Network create the name of my network in this case we just going to call it my network it's very simple and we get a an ID from the Crea network not so important and if we now do doger Network pluss we should see that my network is on the list of networks I'm right now on a very clean Docker setup so I actually have nothing here so just showcase we have Docker images it's going to be empty container LS is going to be empty so first of all I would need to go to the position of my post dock file so right now in the root of my project so I just simply go into my CD into I am from here so Source main slash resources SL persistance and from here we should be able to see we have a Docker file so now I can very simply just run Docker build for my current position and then name it with the tag post DB which just makes it a bit easier in the next command to actually connect to our image so now this should take a few seconds it's probably going to be quicker for me because I have some previous build version probably laying somewhere hiding maybe not but otherwise when it's done loading we should be able to see that we now have a image so post DP image build a few seconds ago and inside this terminal I'm just going to be running it and note I'm not adding SL d which I'm doing later which puts inment mode which should allow us to actually see what's happening so just go through we doing dogger run we're naming the container my database we are defining that the network of this container is going to be my network which we just created previously and the imagery we're going to be running is post DP as we see up here this would then start our post container and the main thing to note is because we're also running from a post Bas image this post bace image does already expose the port the container port for this specific database type and as we can see here we get a bunch of logs print to the console most importantly we can see that our database or our container is listening on Port 5432 which is the base port for postgress so by default any postgress base image will expose the container p P but note also if we now [Music] do Docker container LS Noe you can also do do do PS to see the containers and from here we can see there be exposing Port 5432 but it's only a container Port meaning it's only accessible from other doger containers on the same network so now I would also run my the first build my spring boot Docker setup so now I'm in the rout of my project which is where I also have my do file for my spring boot project I will then do Docker build once again from the current position to the current Docker file and tag it with spring boot service this should be relatively quick what should take the longest is probably copying the jav file and there we go it took me about I don't know 13 seconds I think and we should now also be able to see darker images we can now see we all also have a spring boot service image and we can then also run this image and for this image to run a container I'm doing do run ddor so I'm going to put in the background I don't not want to show it have it running in a specific terminal like this we are port forwarding port 8080 to Port 880 Local Host meaning we are port forwarding the port that my spring boot service is going to be exposing allowing me to actually hit the/ user SL ID endpoint from Local Host which means that we can actually check everything works which is going to be naming in SL just back end again running it on the same network and we're going to be running our spring boot service image so now when you do this get an ID of our container and we should be able to see that if we do Docker coner LS we should here be able to see that we now have two containers running our post DP and our spring boot service and our spring boot service is going to be port forwarding container port 8080 to Local Host port 8080 and now if we open something like Postman or any website or any browser at this point we should be able to add and look at localhost 880 user ID and when we do SL one we are expecting to get some basic information about John and as we can see here we do and we see here inside the container of the postest instance that we get no errors so obviously we get the data so everything's running fine and that's very much the point that now we have two containers our database exposing local or container Port 5432 on the same network our spring fot service then connecting to this port and and I didn't showcase it earlier but inside our spring boot application we have a property folder where we're defining that we're running on Port 880 we're defining a URL for our connection to our hostress DB and normally we would add some kind of here we would add some kind of Ip but because we connecting through containers and we named our container my DB as you can see here for names we can actually connect it directly just using the name of the container as long as they're on the same dock Network we just using we're telling it it's a postest SQL instance the position of my database the IP more or less the port number and the specific database we want to connect to again matching the username and password from our setup or Docker container running our poess and we're just defining a post SC driver just again defining that it's post that we can be connecting to so very much our connection is very automatically done by Spring boot simply passing it some properties with the UR and username password which makes this process very easy and it's very nice that we can actually just directly connect our spring application using the docker container name but in general I think this is very much just all I want to showcase today and as mentioned before I will put a full or I'll put this full project on GitHub where you can have a look at all the parts yourself and the read me should hopefully be a bit more helpful to very clearly showcase which commands I've been running and which commands actually does want so if you enjoyed this Showcase of running uh spring food in a dog container Post in a dog container connect them through Docker so do on a do Network and then exposing our spring Foods Local Host in general if you enjoyed this video please leave like And subscribe and I wish you all a wonderful day
Info
Channel: Random code
Views: 1,495
Rating: undefined out of 5
Keywords: random code, spring boot, spring boot docker, docker, docker postgresql, docker compose, postgresql, spring boot tutorial, spring boot with postgresql, postgresql docker, spring boot postgresql, java spring boot, spring boot postgresql crud api, docker in spring boot, spring boot and docker, docker tutorial, spring boot restful crud api with docker, spring boot docker tutorial, spring boot postgresql docker, how to use spring boot, dockerize spring boot application, spring, java
Id: SNhmx_zXmg4
Channel Id: undefined
Length: 13min 0sec (780 seconds)
Published: Sun Oct 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.