Docker Compose + Spring Boot + Postgres连接 [英] Docker Compose + Spring Boot + Postgres connection

查看:328
本文介绍了Docker Compose + Spring Boot + Postgres连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.yml file defined like this:

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 dockerd sudo docker-compose -f docker-compose.yml up ,它正在启动数据库。我可以使用 pgAdmin 进行连接,例如,使用 localhost 作为服务器和端口 5432 。然后,在我的Spring Boot应用程序的 application.properties 文件中,定义以下属性。

Then, when I issued the commands sudo dockerd and sudo docker-compose -f docker-compose.yml up, it was starting the database. I could connect using pgAdmin for example, by using localhost as server and port 5432. Then, in my Spring Boot app, inside the application.properties file I defined the following properties.

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

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

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:

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

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

Then, I entered to the directory of the project issued mvn clean followed 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.yml file to look like this:

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。问题是连接字符串。我相信我必须将其更改为其他内容,但我不知道应该怎么做。我收到以下错误消息:

But, now if I issue sudo docker-compose -f docker-compose.yml up command, 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:

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的方式:

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

收件人:

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

db 将解析为正确的Postgres IP。

db will resolve to the proper Postgres IP.

奖金。使用docker-compose,您无需手动构建图片。因此更改:

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

web:
  image: myuser/manager:latest

收件人:

web:
  build: .

这篇关于Docker Compose + Spring Boot + Postgres连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆