How To Dockerize Spring Boot Application & PostgreSQL Database

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

Пікірлер: 60

  • @TheGruwy10
    @TheGruwy102 ай бұрын

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

  • @shashankrana03
    @shashankrana03 Жыл бұрын

    Thanks! Look forward to the docker compose tutorial!

  • @CodeVice111

    @CodeVice111

    Жыл бұрын

    sure!

  • @tomsowyer4956
    @tomsowyer4956 Жыл бұрын

    Thanks for episode. Very usefull!

  • @kyeiiih4422
    @kyeiiih4422 Жыл бұрын

    Awesome job

  • @josequintin2467
    @josequintin2467 Жыл бұрын

    Good jobs

  • @hamdiaminehkh
    @hamdiaminehkh2 ай бұрын

    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

    2 ай бұрын

    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.

  • @4wrk
    @4wrk8 ай бұрын

    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

    6 ай бұрын

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

  • @wtfWhoIsIt

    @wtfWhoIsIt

    3 ай бұрын

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

  • @4wrk

    @4wrk

    3 ай бұрын

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

  • @TheRagreis
    @TheRagreis Жыл бұрын

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

  • @CodeVice111

    @CodeVice111

    Жыл бұрын

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

  • @SAGARMN-ho9md
    @SAGARMN-ho9mdАй бұрын

    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

    Ай бұрын

    Then you can simply configure ur database through localhost..

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

  • @user-mb6jq4xn8e
    @user-mb6jq4xn8e Жыл бұрын

    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

    8 ай бұрын

    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

  • @bsal5347
    @bsal53476 ай бұрын

    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

    6 ай бұрын

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

  • @luckytheracer4557
    @luckytheracer45579 ай бұрын

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

  • @CodeVice111

    @CodeVice111

    9 ай бұрын

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

  • @ayushjaiswal4449
    @ayushjaiswal44498 ай бұрын

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

  • @CodeVice111

    @CodeVice111

    8 ай бұрын

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

  • @jirehla-ab1671

    @jirehla-ab1671

    3 ай бұрын

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

    3 ай бұрын

    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

  • @vishalch9127
    @vishalch9127 Жыл бұрын

    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

    Жыл бұрын

    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

    9 ай бұрын

    Did it get resolve?

  • @AbanoubAsaad-YT
    @AbanoubAsaad-YT Жыл бұрын

    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

    Жыл бұрын

    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

    8 ай бұрын

    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

    5 ай бұрын

    @@4wrk it works well on eclipse sts

  • @basheeral-momani2032
    @basheeral-momani203211 ай бұрын

    Thank you, is the code available on github?

  • @CodeVice111

    @CodeVice111

    10 ай бұрын

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

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

    Жыл бұрын

    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

    8 ай бұрын

    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

    5 ай бұрын

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

  • @PARTY6857

    @PARTY6857

    2 ай бұрын

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

  • @CodeVice111

    @CodeVice111

    2 ай бұрын

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

  • @hieutranhuu1048
    @hieutranhuu104829 күн бұрын

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

  • @CodeVice111

    @CodeVice111

    29 күн бұрын

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

  • @voluantran5090
    @voluantran509010 ай бұрын

    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

    10 ай бұрын

    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

    10 ай бұрын

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

    10 ай бұрын

    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.

  • @tranquythuong
    @tranquythuongАй бұрын

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

  • @CodeVice111

    @CodeVice111

    Ай бұрын

    What issue are you getting when you do that!

  • @tranquythuong

    @tranquythuong

    Ай бұрын

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

    Ай бұрын

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

    Ай бұрын

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

    Ай бұрын

    Glad it works for you. Thanks for watching.

  • @user-fc2zx8iy7z
    @user-fc2zx8iy7z10 күн бұрын

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

  • @CodeVice111

    @CodeVice111

    10 күн бұрын

    @@user-fc2zx8iy7z 😊typo

Келесі