How To Dockerize Spring Boot Application & PostgreSQL Database

Sdílet
Vložit
  • čas přidán 12. 02. 2023
  • Click here to stay tuned with all the upcoming videos:
    / @codevice111
    - - - -
    Learn how to run spring boot application and PostgreSQL database in two different docker containers and perform CRUD operation through Swagger3.
    NOTE: We're not using docker-compose to create separate containers for running Spring Boot and PostgreSQL locally.
    - - - -
    Previous Videos:
    - - - - - - - - - - - - - - -
    [Masking Sensitive details from console and Rolling-File appender]: • Mask, Hide & Replace S...
    - - - -
    Development Frameworks & Tools:
    #docker
    #spring
    #springboot
    #swagger
    Thank you so much for going through the detailed description. Feel free to add comments with your suggestions.
    Don't forget to subscribe
    / @codevice111

Komentáře • 60

  • @TheGruwy10
    @TheGruwy10 Před 2 měsíci +1

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

  • @shashankrana03
    @shashankrana03 Před rokem +4

    Thanks! Look forward to the docker compose tutorial!

  • @tomsowyer4956
    @tomsowyer4956 Před rokem

    Thanks for episode. Very usefull!

  • @kyeiiih4422
    @kyeiiih4422 Před rokem

    Awesome job

  • @4wrk
    @4wrk Před 8 měsíci +9

    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 Před 6 měsíci +1

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

    • @wtfWhoIsIt
      @wtfWhoIsIt Před 3 měsíci +1

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

    • @4wrk
      @4wrk Před 3 měsíci +1

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

  • @josequintin2467
    @josequintin2467 Před rokem

    Good jobs

  • @TheRagreis
    @TheRagreis Před rokem

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

    • @CodeVice111
      @CodeVice111  Před rokem +1

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

  • @SAGARMN-ho9md
    @SAGARMN-ho9md Před 2 měsíci

    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  Před 2 měsíci +1

      Then you can simply configure ur database through localhost..

  • @user-mb6jq4xn8e
    @user-mb6jq4xn8e Před rokem +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  Před rokem

      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 Před 8 měsíci

      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

  • @hamdiaminehkh
    @hamdiaminehkh Před 2 měsíci +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  Před 2 měsíci

      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.

  • @Doisgiga
    @Doisgiga Před rokem

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

    • @CodeVice111
      @CodeVice111  Před rokem

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

  • @bsal5347
    @bsal5347 Před 6 měsíci

    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  Před 6 měsíci +1

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

  • @ayushjaiswal4449
    @ayushjaiswal4449 Před 8 měsíci

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

    • @CodeVice111
      @CodeVice111  Před 8 měsíci +1

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

    • @jirehla-ab1671
      @jirehla-ab1671 Před 4 měsíci

      ​@@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  Před 4 měsíci

      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

  • @luckytheracer4557
    @luckytheracer4557 Před 9 měsíci

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

    • @CodeVice111
      @CodeVice111  Před 9 měsíci

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

  • @vishalch9127
    @vishalch9127 Před rokem +1

    I am facing error while running the docker container, i found that in logs. The error is I am unable to connect to postgres.
    " Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connection"

    • @CodeVice111
      @CodeVice111  Před rokem

      Suspecting:: Hope your are giving ipv4(you can get it by runnung 'docker inspect containerId' as shown in the video) instead of localhost. If you've changed the port of postgres then obviously you have to give the correct one. Also suspecting:: Make sure all the connection properties are correct incase if you're getting it from env variable.

    • @user-lx6yg7ft4l
      @user-lx6yg7ft4l Před 9 měsíci

      Did it get resolve?

  • @AbanoubAsaad-YT
    @AbanoubAsaad-YT Před rokem +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  Před rokem +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 Před 8 měsíci

      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 Před 5 měsíci

      @@4wrk it works well on eclipse sts

  • @checkmate6988
    @checkmate6988 Před rokem

    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  Před rokem +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 Před 8 měsíci +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 Před 5 měsíci +1

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

    • @PARTY6857
      @PARTY6857 Před 2 měsíci

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

    • @CodeVice111
      @CodeVice111  Před 2 měsíci

      @@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.

  • @basheeral-momani2032
    @basheeral-momani2032 Před 11 měsíci

    Thank you, is the code available on github?

    • @CodeVice111
      @CodeVice111  Před 10 měsíci

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

  • @voluantran5090
    @voluantran5090 Před 10 měsíci +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  Před 10 měsíci

      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 Před 10 měsíci

      @@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  Před 10 měsíci +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.

  • @hieutranhuu1048
    @hieutranhuu1048 Před měsícem

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

    • @CodeVice111
      @CodeVice111  Před měsícem

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

  • @tranquythuong
    @tranquythuong Před 2 měsíci

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

    • @CodeVice111
      @CodeVice111  Před 2 měsíci

      What issue are you getting when you do that!

    • @tranquythuong
      @tranquythuong Před 2 měsíci

      @@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  Před 2 měsíci +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 Před měsícem

      @@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  Před měsícem +1

      Glad it works for you. Thanks for watching.

  • @user-fc2zx8iy7z
    @user-fc2zx8iy7z Před 19 dny +1

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