How To Create Multi Containers Using Docker Compose | For SpringBoot & Postgresql

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we will see by using Docker Compass tool how we are going to create containers for spring boot application and postgresql database by using single command called Docker compose up and by the way this is the second video related to Docker and in the first video I had told you how to dockerize 10 step springboot application and postgres SQL database by running two different Docker run commands we will also do the same thing for these two applications but this time we will use Docker compose tool and by using single Docker compose run command we will start multiple containers for testam application and postgres SQL database also we will do the volume Mount and we will see how this thing is going to work in the part 1 video I actually used this postgres SQL image so I executed this Docker pull postgres command in my command line to pull the postgres image to my local system so basically this is already added to my local system let me execute Docker images command and you can see the latest postgres image is already available and also last time I created a Docker file under this desktop springboot application root directory and here you can see I'm using jdk 17 image and also I'm exposing this application to 8089 port and the same port has been configured in application properties under server.port so I'll make use of this postgres image and the docker file which is available already so let's quickly create a Docker compose file under the project root which will be Docker hyphen compose Dot yaml alright so you have to mention the version at the very first step and followed by you have to mention services and one more thing if you won't Define the version then latest version will be taken so we can delete the version step and we will directly start from the services and basically the containers that are going to be created by Docker compose are treated as Individual Services so we will configure two Services one for 10 step spring boot application another one is for the database under the services section I'll start from the database configuration first so I'll give the service name as DB first you can mention any XYZ name there is no compulsion that you have to put DB and another one is backend for springboard application now first important configuration is ports under DB service supports is an array and every element in an array is prefixed with hyphen based on the yaml syntax followed by the default Port provided by postgres SQL which is 5432 colon 5432 and again the right hand side of the colon defines the port mapped with the postgres SQL container whereas left hand side port 5432 is exposed to outside so that you can connect your spring boot application with this database under the left hand side 543 to Port then you can give the container name which is postgresql container since we already have a postgres image in our local so we will use that postgres image uh to configure in our Docker campus gml for that we have a property called image and then you mention the postgres image name which is postgres this is important step now next step is environment for that you have to add environment object the environment object can be treated treated as an object or can be treated as an array so if you treat it as an array then you have to mention individual element prefixed by hyphen all right so the first element or the first property under this environment will be postgres underscore DB equal to my db2 then postgres underscore password I'll give it admin too then postgres e underscore user also equal to admin 2. so all right so these are the environment variables we need to add and by the way this environment variables are not randomly added to see the exact environment variables first go to Docker hub and go to this postgres image section and and scroll down to the environment variables part and here you can see the postgres password user and DB so this can this exactly same environment variables have configured in the docker have been composed a DML file as well now here comes the very important step called volumes this volume mapping is very important and as I have already told you this volume mapping is needed because when you are shutting down this postgres SQL container and when you are restarting the container again you should not lose your progress or you should not lose the schema related changes for example when you are performing crowd operations by hibernate jpa into the suppose your SQL database and for some reason if you have to shut it down and you have to restart the postgres SQL container then you should not lose your inserted records or you should not lose the schema related changes you have performed so this volume mapping will be treated as a backup so what we are going to do in this step is we are going to map our local directory or we will create a new folder in our crd drive and we will map the directory to the persistent storage provided by Docker to this postgres SQL database while running in the docker container and let me quickly show you how so the first step would be going to the docker Hub again and search for volumes and come to this part there is a hyphen V followed by some Command right only copy this portion and paste this property under volumes as a first element and you can see the right hand side of this column represents the processor storage provided by Docker Dr disposed receivable database where the data is the persistent storage directory and left hand side of the colon will should be the local directory so I already have created a empty folder called postgres underscore data part under the C drive so I'll copy the directory path and replace the entire left hand side of the colon expression so by doing this we have successfully did the volume mapping and one another important step and by the way the last step under this in DB Services restart there are a bunch of options for the restart but I will configure always as a value to this restart property so by doing this we are telling this Docker compose tool that hey for some reason if this postgres container didn't get created in the first attempt then try to recreate a container for this postgres as a retry attempt so there will be a fallback and retry attempt will happen by using this the restart always property so let's quickly start the backend configuration so the first thing is we do not have any image for springboard application but rather we are going to create an image for this test app application by using the docker file so we are going to tell this Docker Compass tool that hey there is a Docker file already available under the test app root directory so use that to create an image for this backend application and to tell Docker compost to do that we have a property called build and by the way this is an object and this object contains a property called Docker file to tell exactly the name of the docker file you have to create an image for this backend application and for our case this is Docker file with d capital but for the Simplicity purpose I will not mention this Docker file property I'll remove that rather I'll put a DOT so that this Docker compose uh find this Docker file under the same root directory where this Docker compose dot yaml file is already available so this dot represents the current directory so as I said I don't have any image so we do not need to configure image property just like we did for the database so we will do directly the configuration for container name which is container underscore name then ports it's an area so the element of the of this array is prefixed with hyphen in our Port which is configured in the application window file which is 8089 and this is the same port we also have configured in the docker file so now the next thing is environment configuration now again in your application properties file if you have externalized the database connection properties such as the database URL username and password so that you can get those properties from the environment variable then only you have to configure this environment part in the docker hyphen composed tml so the first property is DV hyphen URL having value called jdbc colon postgres SQL colon double slash DB colon 5432 and then my db2 in uppercase an important part You observe this host name the host name is not given as localhost or IP address and rather this DB is the DB service at line number two now the next property is D we have in username which is admin 2 then DB password and then to tell this Docker Compass tool that this backend service is depending on the database service we have a property called depends on all right so for our newer version it's an area so the first element will be the database service name which is DB and we will also configure restart always for this backend service so that's it for the configuration standpoint all right so now let's clear out the console and we'll execute a command called docker compose hyphen f I'll type Doc and press tab button so it will be automatically filled out the docker compose dot yaml name then up and now you you also can run these two containers in a detached mode or I should say you can run these two containers in the background so you can put hyphen D flag at the end as well but I won't put that hyphen d flat for now just because I want to show you exactly what log messages got generated now hit enter now you can see just because of that build dot property we have given in the line number 15 for backend service it is trying to create an image for this springboard application so it is trying to read the docker file from the root directory and create trying to create an image then automatically just because of the depends on property we also have configured under back end so this Docker compose tool can identify which is the dependent container or I should say which is the dependent service and which is the dependency service for our case since backend service depends on DB service so this Docker compose will automatically give the priority to the DB Service First and it will try to create the container for the DB service and then it will come to the dependent service which is backend service all right so now the spring boot container also has started running and it took a while to start both the containers in one shot because there were some connectivity issues for the spring boot application thanks to that restart always property this Docker compost 2 will also retried again and again for the spring boot application to connect with the postgres SQL database and once the connection got established our springboard application related container also got created quickly jump to the browser and go to the Swagger dashboard I'm using springfox swagger 3 all right so all the controller related endpoints are getting rendered here and for a quick demonstration I'm going to insert one record for this orders to the order table order ID not required amount can be 1000 I can insert multiple records now we'll execute I can see the 200 response and I'll quickly open the PG admin 4 to show you the newly inserted record so first of all you have to give the master password probably if you're opening this PC admin for for the first time then you have to create a new master password now click on the service go to object go to register then register a new server server name can be any name I'll give you my dv2 hyphen div and go to the connection tab under the connection name hostname should be localhost followed by the port number which is 5032 as a default Port which is already fine database is my db2 username is admin 2 password also admin 2. I'll save the password and connect and you can see this order and product tables get created by hibernate now let's quickly check our newly inserted record okay so now right click on this orders table then go to view and edit and click on all rows and yeah so we can see the two newly inserted records and let me quickly show you the two running containers by opening a new terminal here so run command called Docker p is to see the running containers and you can see there are two running containers one for test app container and postgres container and now to stop this two container in one go you have to use Docker hyphen compose and then down alright so now you can see the containers got stopped and removed to re-initiate this containers again again you have to execute Docker compose up command and you can see the building of an image for this test tab application step has been skipped because we already have created an image for this test type application so in the next Docker compose up command you didn't see this tape [Music] all right so this is pretty much it from this video thank you so much for watching
Info
Channel: CodeVice
Views: 5,033
Rating: undefined out of 5
Keywords: docker compose, springboot postgresql docker compose, docker config for spring boot and postgresql, dockerize spring boot and postgresql, compose spring boot postgres, how to docker compose, docker compose for beginner, can't connect spring boot to postgresql using docker compose, docker compose up, docker compose yaml, docker compose not working for spring boot and database, codevice, codevice111, docker compose spring boot, docker compose spring boot postgresql, compose spring
Id: yodeo205pp0
Channel Id: undefined
Length: 15min 35sec (935 seconds)
Published: Sun Mar 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.