搜索
首页JavaDocker Compose + Spring Boot + Postgres 连接

php小编西瓜今天为大家带来一篇关于如何使用Docker Compose连接Spring Boot和Postgres的教程。Docker Compose是一个用于定义和运行多容器Docker应用程序的工具,而Spring Boot则是一个用于构建Java应用程序的框架,Postgres则是一个功能强大的关系数据库。通过结合使用这三者,我们可以轻松地搭建一个包含Spring Boot应用程序和Postgres数据库的开发环境。本教程将带你一步步学习如何配置和连接这三者,让你能够快速开始开发你的应用程序。

问题内容

我有一个可与 Postgres 数据库配合使用的 Java Spring Boot 应用程序。我想对它们都使用 Docker。我最初只将 Postgres 放入 Docker 中,并且有一个 docker-compose.yml 文件,定义如下:

version: '2'
services:
    db:
        container_name: sample_db
        image: postgres:9.5
        volumes:
            - sample_db:/var/lib/postgresql/data
        environment:
            - POSTGRES_PASSWORD=sample
            - POSTGRES_USER=sample
            - POSTGRES_DB=sample
            - PGDATA=/var/lib/postgresql/data/pgdata
        ports:
            - 5432:5432

volumes:
    sample_db: {}

然后,当我发出命令 sudo dockerdsudo docker-compose -f docker-compose.yml up 时,它正在启动数据库。例如,我可以使用 pgAdmin 进行连接,使用 localhost 作为服务器和端口 5432。然后,在我的 Spring Boot 应用程序中,在 application.properties 文件中定义了以下属性。

spring.datasource.url=jdbc:postgresql://localhost:5432/sample
spring.datasource.username=sample
spring.datasource.password=sample
spring.jpa.generate-ddl=true

此时,我可以通过 Spring Suite 在本地运行我的 Spring Boot 应用程序,并且一切正常。然后,我还想将我的 Spring Boot 应用程序添加为 Docker 映像。我首先在项目目录中创建了一个 Dockerfile,如下所示:

FROM java:8
EXPOSE 8080
ADD /target/manager.jar manager.jar
ENTRYPOINT ["java","-jar","manager.jar"]

然后,我进入了发布mvn clean的项目目录,然后是mvn install。接下来,发出 docker build -f Dockerfile -t manager . ,后跟 docker tag 9c6b1e3f1d5e myuser/manager:latest (id 是正确的)。最后,我编辑了现有的 mvn clean的项目目录,然后是mvn install。接下来,发出 docker build -f Dockerfile -t manager . ,后跟 docker tag 9c6b1e3f1d5e myuser/manager:latest (id 是正确的)。最后,我编辑了现有的 docker-compose.yml 文件,如下所示:

version: '2'
services:
    web:
      image: myuser/manager:latest
      ports: 
          - 8080:8080
      depends_on:
          - db
    db:
        container_name: sample_db
        image: postgres:9.5
        volumes:
            - sample_db:/var/lib/postgresql/data
        environment:
            - POSTGRES_PASSWORD=sample
            - POSTGRES_USER=sample
            - POSTGRES_DB=sample
            - PGDATA=/var/lib/postgresql/data/pgdata
        ports:
            - 5432:5432

volumes:
    sample_db: {}

但是,现在如果我发出 sudo docker-compose -f docker-compose.yml up 命令,数据库会再次正确启动,但我会收到错误并退出 Web 应用程序部分的代码 1。问题是连接字符串。我相信我必须将其更改为其他内容,但我不知道它应该是什么。我收到以下错误消息:

web_1  | 2017-06-27 22:11:54.418 ERROR 1 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.
web_1  | 
web_1  | org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

有什么想法吗?

解决方法

每个容器都有自己的网络接口和自己的本地主机。因此改变 Java 指向 Postgres 的方式:

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

致:

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

db 将解析为正确的 Postgres IP。

奖金。使用 docker-compose,您无需手动构建镜像。所以改变:

web:
  image: myuser/manager:latest

致:

web:
  build: .

我遇到了同样的问题,我花了一些时间来理解和解决这个问题:

org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

我展示了所有属性,以便每个人都能理解。 application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL82Dialect
spring.jpa.hibernate.ddl-auto=update

docker-compose.yml:

version: "3"
  services:
    springapp:
      build: .
      container_name: springapp
      environment:
        SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/testdb
      ports:
        - 8000:8080
      restart: always
      depends_on:
        - db
    db:
      image: postgres
      container_name: db
      environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
        - POSTGRES_DB=testdb
        - PGDATA=/var/lib/postgresql/data/pgdata
      ports:
        - 5000:5432
      volumes:
        - pgdata:/var/lib/postgresql/data
      restart: always
  volumes:
    pgdata:

为了使用本地数据库启动 Spring 应用程序,我们使用 url localhost。 为了使用数据库连接到容器,我们需要更改数据库服务上的“localhost”,在我的例子中,将“localhost”更改为“db”。 解决方案:在docker-compose.yml中添加SPRING_DATASOURCE_URL环境,重写spring.datasource.url中添加SPRING_DATASOURCE_URL环境,重写spring.datasource.url连接值:

environment:
    SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/testdb

我希望这可以帮助人们节省时间。

以上是Docker Compose + Spring Boot + Postgres 连接的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:stackoverflow。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。