postgresql Docker Compose + Spring Boot + Postgres 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44790923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:35:37  来源:igfitidea点击:

Docker Compose + Spring Boot + Postgres connection

springpostgresqldockerdocker-composedockerfile

提问by typos

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-compose.ymlfile defined like this:

我有一个适用于 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: {}

Then, when I issued the commands sudo dockerdand sudo docker-compose -f docker-compose.yml up, it was starting the database. I could connect using pgAdminfor example, by using localhostas server and port 5432. Then, in my Spring Boot app, inside the application.propertiesfile I defined the following properties.

然后,当我发出命令sudo dockerd和 时sudo docker-compose -f docker-compose.yml up,它正在启动数据库。pgAdmin例如,我可以使用localhostas server 和 port 进行连接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

At this point I could run my Spring Boot app locally through Spring Suite, and it all was working fine. Then, I wanted to also add my Spring Boot app as Docker image. I first of all created a Dockerfile in my project directory, which looks like this:

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

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

Then, I entered to the directory of the project issued mvn cleanfollowed by mvn install. Next, issued docker build -f Dockerfile -t manager .followed by docker tag 9c6b1e3f1d5e myuser/manager:latest(the id is correct). Finally, I edited my existing docker-compose.ymlfile to look like this:

然后,我进入到发布的项目目录,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: {}

But, now if I issue sudo docker-compose -f docker-compose.yml upcommand, the database again starts correctly, but I get errors and exit code 1 for the web app part. The problem is the connection string. I believe I have to change it to something else, but I don't know what it should be. I get the following error messages:

但是,现在如果我发出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

Any ideas?

有任何想法吗?

回答by Robert

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

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

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

To:

到:

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

dbwill resolve to the proper Postgres IP.

db将解析为正确的 Postgres IP。



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

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

web:
  image: myuser/manager:latest

To:

到:

web:
  build: .

回答by cemahi

You can use this.

你可以用这个。

version: "2"
services:
    sample_db-postgresql:
        image: postgres:9.5
        ports:
            - 5432:5432
        environment:
            - POSTGRES_PASSWORD=sample
            - POSTGRES_USER=sample
            - POSTGRES_DB=sample
         volumes:
            - sample_db:/var/lib/postgresql/data

volumes:
    sample_db:

回答by Alexander Yushko

I had the same problem and I lost some time to understand and solve this problem:

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

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.

I show all the properties so that everyone understands.
application.properties:

我展示了所有的属性,以便每个人都理解。
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:

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:

For start spring application with local database we use url localhost.
For connect to container with database we need change 'localhost' on your database service, in my case 'localhost' to 'db'.

Solution: add SPRING_DATASOURCE_URLenvironment in docker-compose.ymlwich rewrite spring.datasource.urlvalue for connect:

对于使用本地数据库启动 spring 应用程序,我们使用 url localhost。
为了使用数据库连接到容器,我们需要在您的数据库服务上更改“localhost”,在我的情况下,“ localhost”更改为“ db”。

解决方案:在其中添加SPRING_DATASOURCE_URL环境以docker-compose.yml重写spring.datasource.url连接值:

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

I hope this helps someone save his time.

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