How To Dockerize Spring Boot Application & PostgreSQL Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to code wise in this video we will talk about how to dockerize springboot application and postgresql database so both the applications will be running in two different Docker containers and I hope the docker setup at your local system has already been done so the first step would be going to the docker Hub which is hub.docker.com and there you register yourself if you have not registered yet and then you go to the explore part and you will come to this dashboard scroll down little bit until you find out postgres image now if you can see we have a command given Docker pull postgres so by using this command we are going to pull this pose grid SQL image to our local system and then we have the tag section so basically it tells which version of postgresql you're gonna use so just open your command prompt and type Docker pull postgres and if you want to bring a specific version of postgres then you can mention the version after a colon if 15.1 then you can mention 15.1 y 15.1 because here in the tag section we have given specific version now if you won't specify anything then by default the latest version will be pulled and before executing this command I'll show you how many images I have and for that I'll execute Docker images as of now there is no image in my local so I'll copy this pull command and execute now this will take a while to download all the dependencies related to this postgres image all right so the image has been pulled now to see that we we will again execute Docker images you can see the image with the latest tag so the latest version of postgres has been pulled and then we are going to run this postgres database in the docker container for that I'll execute a command Docker run postgres postgres with the image name but this is not the sufficient information in the command line we are giving so it also needs some additional properties like we are going to run this postgres container in a detached mode so that it will run in the background also we will need a port mapping so the default Port provided by the post recipient database is 5432 and we will map that with 5432 again so what basically mean is the right hand side port represents the port mapping done to the postgres SQL container and the left hand side 5432 represents the port which is going to be exposed to outside world so that our springboot application can connect with it and then we need some environment variables saying uh the database name username and password so just go to the docker Hub again scroll down a little bit till the environment variable and you will find the keys where you can specify the password username and the database so we'll use that hyphen e postgres password I'll give admin hyphen e postgres user will also be admin and again hyphen e to specify the database copy the post Grace underscore DB say my DB and one important thing is is volume mapping that volume mapping should be done because when we stop this postgres SQL container and when we start the postgresql container again we should not lose our data so it will be treated as a backup so just to do the volume mapping go to Docker Hub search for volume again and here you can find out the hyphen V certain properties then copy that property paste it here the left hand side represents the persistent storage path provided by Docker so the data folder is the persistent storage provided by Docker and the right hand side custom path we don't need obviously so we will create a folder in our C drive say postgres container data I'll copy this path and paste it to the left hand side of the column so now we have successfully uh you know mapped the persistence storage provided by the docker container to this postgres SQL database so now one last property we need which is the name of the container so we can use double hyphen name equal to postgres underscore con You observe one more thing after equal to you should not put any white space otherwise the command will fail once that is done just hit enter now if you want to see the running container you can execute Docker PS and you will find the postgres equal underscore con the container we have created just now and to see all the running containers we can execute command called Docker PS hyphen a as of now we already have we we have only one container so Docker having PS Docker PS iPhone a will provide you all the containers which is in running state or even not in running State we will get all the informations so the step one is done now the step two is we are going to create an image for spring boot application and that's spring boot application is uh Test App application this is the same application that I've been using since the beginning of the video tutorial where we had configured the in-memory S2 database performed crud operations we also did some Swagger configuration and also sent some custom request header through Swagger and then also we had seen how to mask or hide sensitive contains in the log so you can also check out those videos I'll provide those video disc links in the description as well and so let's start the second step where we have to create uh you know the image for this test app application earlier I've been using this H2 database I'll comment out all the connection properties since we are going to use the postgres SQL database real estate connection properties and by the way my application is running under 8089 Port so here I'm going to use the postgres SQL connection properties for postgresql connection properties we need the driver class name data source URL username and password the same connection properties you have given in the command line right my DB is the database admin is the username and password the same thing we have provided here and the remaining three properties are related for the hibernate JPS so that it will create the schema hybrid it will create the schema prepare the schema in the postgresql database running in the docker container as soon as this test app container runs or as soon as the spring boot application starts running in the docker container so one more important thing since both test apps spring boot application and postgres SQL database file base is running in two different Docker containers so when we are trying to connect spring boot application running in one container to postgresql database running in another container the Springwood application will fail to connect because we are using localhost 5432 so we need to provide the exact hostname and the port number port number 5432 is what we are exposing I had mentioned this 543 to left hand side 5432 we have to map with spring boot application which we are already doing but the host name hostname should not be localhost so to see exactly uh what is the host name or exactly what is the IP address that we have to map then you can use another command called Docker inspect followed by the container name or the container ID so I'll give you the container name which is postgres underscore con and then I'll hit enter and at the end you will see IP address 1721702 copy that and replace this localhost by that IP address that is the very important step and the second important step is creating a Docker file so the docker file we will create under the root folder of the Springwood application Docker file where the D is capital and then we will write down certain commands or the steps so that when we are building an image for this test type application Docker will execute them so the step one is from open jdk colon 17 I'm using jdk 17 so I'll also pull this image and then I'll copy the jar file after Maven clean install there will be a jar file available I have already done the mavenclin install so there is a jar file you can see generated for this pre-mute application so I'll copy this test tab.jar by using this copy command so the jar file will be copied from the target folder and it will create test app.jar and then we will expose our spring boot application to a certain port to run right so we have to mention the same port we have provided here so Expose 28089 and then the last command is entry point here we'll mention Java American child test app.jar so basically this is the typical uh that Java iPhone Java command for running a Java application so the same command we have to mention so this Docker file part is done now we will open the terminal I'll open the terminal in the IntelliJ itself now we will execute this Docker file or we will build an image for this desktop application by executing Docker build command and and we'll mention dot why dot because under the same directory we have this Docker file and important thing is we will also mention the name of the image to mention the name we are not going to use double hyphen name rather we will use double hyphen tag or you can mention single hyphen and then T minus t you can mention will tell the name test app underscore IMG and then when we hit enter it will create an image all right so it took a while to create a image of the desktop application so let's see that by using docker images command so now we have two images test tab underscore image as well so we are almost close uh to the end so the last step is to create container out of this test app underscore IMG so let me quickly show you how to do that Docker run we need a detach mode as well with then we need a port mapping so the port is 8089 colon again 8089 as I told you the right hand side of the port represents the container Port so 8089 is the port where spring boot application Test App application will run in the docker container and this left hand side 8089 Port will be exposed to outside world so from browser when we hit the request to localhost 20089 so this will be the port where it will be listened and then the image name and also we need the container name for that we will use double hyphen name equal to test app underscore con that's it hit enter container got created so execute Docker PS to see the running containers and now both the containers are running and to see the spring boot related logs we can use Docker logs test app underscore con and this will show the spring boot related logs now go to your browser and search for localhost 8089 slash Swagger iPhone UI slash index.html and we have order and product controller and I'll show you how the data is getting saved into the postgresql database as well so first let me save a product okay so we are getting 200 response and I'll show you on PG admin 4 I'll connect with post recipient database and I'll show you the inserted details so once you are in the PG admin 4 it will ask you a master password my master password is admin now click on the server then go to object register server server name is let's say test app DV connection name host should be localhost Port is fine 5432 database is mydb username admin password admin and Save test app connection has been created and you can see the database expand the schema then scroll down expand the tables and as you can see however it is properly creating tables based on the entities we have so let me show you the entity details as well under srcm in Java under entity package we have order in product and I'll show you the product the one which I inserted right click on the product and go to query tool and you can execute select star from product and you can see the product is already inserted so this is how we got the confirmation that uh from Swagger also we can do the we can perform the crude operation and our spring boot application which is running in uh in one container let me quickly show you Docker PS so spring application is running in one container Docker is running in another container and we connected the spring boot application to Docker by using the postgres SQL connection properties where we had to provide the IP address of the postgres SQL database and for that I executed Docker inspect command and then postgres underscore con and where I found out the IP address which is this one and I configured it to the application properties and that's how we connected so thank you so much for watching this video uh there is I know there is another solution by using Docker compose also we can do the containerization for both Test App application and postgresql database so I'll create a separate video for this so stay tuned for the upcoming videos bye
Info
Channel: CodeVice
Views: 18,548
Rating: undefined out of 5
Keywords: spring boot, postgresql, containerising spring boot, springboot postgres docker, dockerizes spring boot, dockerizes postgresql, how to dockerize postgresql, how to dockerize springboot, spring boot postgresql crud rest api, spring boot with postgresql and docker example, codevice, code vice, docker tutorial, how to docker, what is docker spring boot postgresql tutorial, docker example, spring boot + postgresql + docker example, docker, microservice, hibernate jpa, psql, crud example
Id: 3fALfjlkfuU
Channel Id: undefined
Length: 15min 0sec (900 seconds)
Published: Mon Feb 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.