Docker Compose + Spring Boot + Postgres connection

后端 未结 3 1598
孤街浪徒
孤街浪徒 2021-02-12 17:15

I have a Java Spring Boot app which works with a Postgres database. I want to use Docker for both of them. I initially put just the Postgres in Docker, and I had a docker-

3条回答
  •  半阙折子戏
    2021-02-12 17:28

    Each container has its own network interface with its own localhost. So change how Java points to Postgres:

    spring.datasource.url=jdbc:postgresql://localhost:5432/sample
    

    To:

    spring.datasource.url=jdbc:postgresql://db:5432/sample
    

    db will resolve to the proper Postgres IP.


    Bonus. With docker-compose you don't need to build your image by hand. So change:

    web:
      image: myuser/manager:latest
    

    To:

    web:
      build: .
    

提交回复
热议问题