搜尋
首頁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 是正確的)。最後,我編輯了現有的 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連線值:

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尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。