How To Dockerize Spring Boot Application & PostgreSQL Database

Поділитися
Вставка
  • Опубліковано 1 лют 2025

КОМЕНТАРІ • 75

  • @TheGruwy10
    @TheGruwy10 8 місяців тому +2

    The only video that helped me out :) Thank You very much! Good explanation

  • @malithatapattu
    @malithatapattu 3 дні тому

    Clear and concise. Thanks!

  • @Louyark
    @Louyark 5 місяців тому

    5 🌟 for this very well detailed explaination. I got the connection up and running thanks to your video. Much appreciated pal

  • @shashankrana03
    @shashankrana03 Рік тому +4

    Thanks! Look forward to the docker compose tutorial!

  • @4wrk
    @4wrk Рік тому +11

    Problem: You cannot create a .jar after you change localhost to different value.
    Solution 2: You can activate Toggle 'Skip Tests' mode in Maven Tab before creating jar. (looks like circle, like this (/))
    Solution 1: You should clean your maven project in common way, after that you should go to the terminal in intelliJ(i hope you use intelliJ, cause idk if it works in other IDE's).
    In terminal you should write next:
    mvn install -DskipTests=true
    and press CTRL+ENTER, if you press only enter it will not work.
    If this comment helped you just press the like button.
    P.S. IDK why the author avoid answer to this question, i read all his answers down below and that is just "ahh, yeah, you can't do this" without solution :/
    P.P.S. If you try to use this command in terminal but it's not work and you just skipped the section above use ctrl+enter, it's might be a little bit confusing like it was for me)

    • @posichief365
      @posichief365 Рік тому +1

      BIG THANK YOU❤ I was spending hours with this problem for my bachelors project. Thanks 🙏🏽

    • @wtfWhoIsIt
      @wtfWhoIsIt 9 місяців тому +1

      Or you can activate Toggle 'Skip Tests' mode in Maven Tab before creating jar. (looks like circle, like this (/))

    • @4wrk
      @4wrk 9 місяців тому +1

      @@wtfWhoIsIt that's even better solution, than mine😀

    • @sreelathavk4450
      @sreelathavk4450 Місяць тому +1

      @@wtfWhoIsIt Thank you, It helped alot.

    • @Vandana_Prabhu
      @Vandana_Prabhu 17 днів тому +2

      Thank you so much!! Spent a lott of time on that & then found this comment.

  • @suvopov
    @suvopov 2 місяці тому

    Many thanks for this video.

  • @sreelathavk4450
    @sreelathavk4450 Місяць тому

    I was not able to connect at first . Thanks for inspect command and IP fetch step. Now I'm able to connect. Thank you so much for keeping it simple and straight to the point. But now it throws error like "PSQLException: ERROR: relation "employees" does not exist" . Please suggest.

    • @CodeVice111
      @CodeVice111  Місяць тому

      Is your @Entity & @Table("EMPLOYEES") correctly done?
      If you have installed pgadmin4 please check the schema got generated or not.
      Also you can set show-sql flag to true to see the executed self query by hibernate.
      Please do let me know sir. If you still need further assistance. Thank you

    • @sreelathavk4450
      @sreelathavk4450 Місяць тому

      @@CodeVice111 Thanks for your reply. I was able to run the app from docker now. Database is empty on container. How generally developers test application if there is no data.. How can we get data on that DB.

    • @CodeVice111
      @CodeVice111  Місяць тому

      @sreelathavk4450 Hibernate Jpa will take care of schema generation. All you need is just to add those annotations on top of your entiry class. There are many ways to do.. But I would request you to see a simple example on Hibernate Jpa + Spring boot CRUD example.
      Thank you Sir.

  • @hamdiaminehkh
    @hamdiaminehkh 8 місяців тому +1

    the problem in this approch is tha the IP address of a container can change when the container is stopped and started again, or if the host machine is restarted. so you should change the properties file everytime the ip is changed. i think using docker compse is the better approch

    • @CodeVice111
      @CodeVice111  8 місяців тому

      You're right!, Easy approach would be ... 'docker create network my-custom-network' and then running both the containers under '--network=my-custom-network'. Then, container IP will no longer be needed.

  • @AnkushMohanty-z1r
    @AnkushMohanty-z1r Рік тому +1

    I am facing problem while making the jar file after change the host name with the container ip.I am not been able to create jar through maven clean and maven install or through the mvn package or through the build path and package .I have tried a lot.Please do help me.

    • @CodeVice111
      @CodeVice111  Рік тому

      At the beginning of your pom.xml where the groupId, artifact id, version etc.. are defined. Right below that you should have xml tag called jar. Secondly there should also be a pluging configured called maven-jar-plugin. But these things are by default configured when you freshly create a spring boot app. Anyways, give it a try.

    • @4wrk
      @4wrk Рік тому

      Like my main comment please, if it helps
      Solution: You should clean your maven project in common way, after that you should go to the terminal in intelliJ(i hope you use intelliJ, cause idk if it works in other IDE's).
      In terminal you should write next:
      mvn install -DskipTests=true

  • @SAGARMN-ho9md
    @SAGARMN-ho9md 8 місяців тому

    I am having Spring-boot in my local machine and need to connect it to container which has database can i follow these steps for that ?

    • @CodeVice111
      @CodeVice111  8 місяців тому +1

      Then you can simply configure ur database through localhost..

  • @tranquythuong
    @tranquythuong 8 місяців тому

    You didn't re-install maven for target folder, why does it still work?

    • @CodeVice111
      @CodeVice111  8 місяців тому

      What issue are you getting when you do that!

    • @tranquythuong
      @tranquythuong 8 місяців тому

      @@CodeVice111 After I had successfully dockerized my postgresql, it worked correctly, then I change the IP of application.properties file in my spring boot project, but when maven clean install, it encountered a problem with connecting attempt to DB

    • @CodeVice111
      @CodeVice111  8 місяців тому +1

      @@tranquythuong looks like, your application build process is modifying your db connection props. can you plz confirm by printing those values in your console to ensure all props are accurate!. And i also assume, your backend app is not containerized but postgres is.. and If so, then use localhost instead of container port.

    • @tranquythuong
      @tranquythuong 8 місяців тому

      @@CodeVice111 My postgres container work fine if I change the IP to localhost and run with intelliJ. I've solve my problem, when changed the IP I built the .jar file by using "mvn clean package -DskipTests" to skip testing step, and then I built the image, It was working correctly
      I'm still wondering why you just modify the application.properties file without cleaning and re-building .jar file with maven, and it still works :v

    • @CodeVice111
      @CodeVice111  8 місяців тому +1

      Glad it works for you. Thanks for watching.

  • @RamadeviMtech
    @RamadeviMtech 2 місяці тому

    good explanation ,could you please share the repo link here

    • @CodeVice111
      @CodeVice111  2 місяці тому

      @@RamadeviMtech Pardon Sir. Didn't put those code into repo. And don't have the backup either.
      Thanks for watching.

  • @tomsowyer4956
    @tomsowyer4956 Рік тому

    Thanks for episode. Very usefull!

  • @voluantran5090
    @voluantran5090 Рік тому +1

    i only ran Postgres container, in your video you only using dockerfile not docker-compose, but my spring boot application didn't connect to postgres

    • @CodeVice111
      @CodeVice111  Рік тому

      If you are only running postgres on docker container, but not spring boot app. Then connecting to postgres db is quite simple. It should be through : localhost 5432
      spring.datasource.url=jdbc:postgresql://localhost:5432/my-db
      spring.datasource.username=give_username
      spring.datasource.password=give-password
      Additionally u hv to specify
      spring.jpa.hibernate.ddl-auto=update
      spring.jpa.show-sql=true
      spring.jpa.properties.hibernate.format_sql=true
      spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect

    • @voluantran5090
      @voluantran5090 Рік тому

      @@CodeVice111 sorry, i mean 2 have two images: postgres and my-springapp, then i create a container of postgres image and it run ok, but when i create container of my-springapp it say refuse connect postgres. Why

    • @CodeVice111
      @CodeVice111  Рік тому +2

      Understood.
      When both apps hv their own container, then they hv be in the same network. Here is how:
      docker network create myNetwork
      Now u can include the netwoek using --net flag in the run cmd. Example:
      docker run -d -p 8000:8000 --net myNetwork --name springboot-container springboot-image
      docker run -d -p 5432:5432 --net myNetwork --name postgres-container postgres
      Try this out. Thanks for watching.

  • @shihabahmed754
    @shihabahmed754 2 місяці тому

    7:55 section eased me.... 😓

  • @ayushjaiswal4449
    @ayushjaiswal4449 Рік тому

    Hi I have one question can't we pack postgres and my application in the same container and expose endpoints for both

    • @CodeVice111
      @CodeVice111  Рік тому +1

      Technically yes. But not recommended. Not even a best practice for prod env for obvious reasons.

    • @jirehla-ab1671
      @jirehla-ab1671 10 місяців тому

      ​@@CodeVice111lets say i have postgres database alredy installed in my server.
      Now when dockerizIng my postgrres, how do i keep the extensions with it?

    • @CodeVice111
      @CodeVice111  10 місяців тому

      Please have a look: stackoverflow.com/questions/40040540/how-to-create-postgres-extension-inside-the-container
      Do let me know if it doesn't work out

  • @bsal5347
    @bsal5347 Рік тому

    Hello , when i entered the details on pgAdmin and click save , Error occurs and error says "unable to connect to server : connection timeout expired".Can you help me with this , I am struggling with this error and can't seem to find the solution.

    • @CodeVice111
      @CodeVice111  Рік тому +1

      It happened with me once. And fixed it by reatarting the PGadmin. Thabks for warching

  • @diraneserges6076
    @diraneserges6076 5 місяців тому

    When i change localhost to ip adress, i cannot generate a jar file. how i can fix it please

    • @CodeVice111
      @CodeVice111  5 місяців тому +1

      I think someone has already answered on this comcern in the comment section.
      Which is to use mvn clean install -DskipTests=true before building your .jar

  • @TheRagreis
    @TheRagreis Рік тому

    Hi, don't we need to paste the PostgreSql dependencies in order to compile the project?

    • @CodeVice111
      @CodeVice111  Рік тому +1

      Yes you are right! you've to add the dependency in the pom.xml

  • @checkmate6988
    @checkmate6988 Рік тому

    After changing localhost to IpAddress in spring.datasource.url, I can't start the application directly from Intellij. Therefore, I can't run "mvn clean install" properly. Is that ok or am I doing something wrong? Is the whole point of Dockerfile not to run application from Intellij interface, rather running it everytime using the Dockerfile?

    • @CodeVice111
      @CodeVice111  Рік тому +1

      If you have already dockerized the postgres, but not yet dockerized the spring boot app... then you should use localhost instead of iPaddress in the db url. Ip address will only help you connecting to dockerized postgres when the spring boot app is also dockerized. 👌 thanks for watch.

    • @4wrk
      @4wrk Рік тому +1

      Like my main comment please, if it helps
      Solution: You should clean your maven project in common way, after that you should go to the terminal in intelliJ(i hope you use intelliJ, cause idk if it works in other IDE's).
      In terminal you should write next:
      mvn install -DskipTests=true

    • @OnWeek
      @OnWeek 11 місяців тому +1

      @@CodeVice111 you help me a lot with this comment, thanks a lot

    • @PARTY6857
      @PARTY6857 8 місяців тому

      @@CodeVice111 how did you generate Jar file after modifying the ip address?

    • @CodeVice111
      @CodeVice111  8 місяців тому

      @@PARTY6857 Good point. Better to use 'docker create network --network=custom-network' first, and then 'docker run' those two containers using --network flag. With this approach, it won't ask you to put IP in your config file. Leme know it helps... Thanks for watching.

  • @Doisgiga
    @Doisgiga Рік тому

    Nice content brother. What do you say about new UI in the IntelliJ idea?

    • @CodeVice111
      @CodeVice111  Рік тому

      Very compact, cleaner interface & yet little bit confusing for me when i switched to new one for the first time. 😄

  • @kyeiiih4422
    @kyeiiih4422 Рік тому

    Awesome job

  • @luckytheracer4557
    @luckytheracer4557 Рік тому

    How have you create jar file with container ip. It will generate errors

    • @CodeVice111
      @CodeVice111  Рік тому

      You're right. If you're creating a jar of your app without "externalizing env variables"! you'll end up getting errors.

  • @hieutranhuu1048
    @hieutranhuu1048 7 місяців тому

    Why don't you use docker compose file, i need it

    • @CodeVice111
      @CodeVice111  7 місяців тому +1

      @@hieutranhuu1048 please watch its second part. Find the link in the description. Thanks for watching

  • @AbanoubAsaad-YT
    @AbanoubAsaad-YT Рік тому +1

    Awesome, thanks!
    But I'm facing a problem after deploying the postgres database and creating its container.
    In the spring boot app, when I try to replace localhost by the IP of the container and run the the app, I see there's no connection happens to the db.

    • @CodeVice111
      @CodeVice111  Рік тому +1

      Please make sure you have configured the port correctly as well. And make sure before starting the spring boot container the postgres container should be up and running.

    • @4wrk
      @4wrk Рік тому +1

      Like my main comment please, if it helps
      Solution: You should clean your maven project in common way, after that you should go to the terminal in intelliJ(i hope you use intelliJ, cause idk if it works in other IDE's).
      In terminal you should write next:
      mvn install -DskipTests=true

    • @OnWeek
      @OnWeek 11 місяців тому

      @@4wrk it works well on eclipse sts

    • @Kauaaa1010
      @Kauaaa1010 4 місяці тому

      ​@@4wrk It's happening with me and your tips work, but I'd like to know if it's a good way to avoid this error or just a trick.

    • @4wrk
      @4wrk 4 місяці тому

      @@Kauaaa1010 I don't know to be honest, it might be a right solution, but for me it looks like a trick. And again: I don't know is there another ways to fix that :)

  • @basheeral-momani2032
    @basheeral-momani2032 Рік тому

    Thank you, is the code available on github?

    • @CodeVice111
      @CodeVice111  Рік тому

      Apologies. I haven't not shared any snippets yet.
      Thanks for watching.

  • @josequintin2467
    @josequintin2467 Рік тому

    Good jobs

  • @mitakaka2
    @mitakaka2 6 місяців тому +1

    why do you call it pUstgres? Its weird. Just say pOstgres.