Docker Compose + Spring Boot + Postgres connection

后端 未结 3 828
耶瑟儿~
耶瑟儿~ 2021-02-12 16:40

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:23

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

提交回复
热议问题