本文介绍了Docker:Springboot 容器无法连接到 PostgreSql 容器连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个 Springboot 2.0 应用程序.我试图将我的 Springboot 应用程序放入一个 docker 容器,将我的 PostgresDB 放入另一个容器中.

I am building my first Springboot 2.0 application. I am trying to put my Springboot application into one docker container and my PostgresDB into another container.

我的 Dockerfile

    FROM frolvlad/alpine-oraclejdk8:slim
    VOLUME /tmp
    ADD springboot-api-demo-0.1*.jar app.jar
    RUN sh -c 'touch /app.jar'
    EXPOSE 9443
    ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/urandom -jar /app.jar" ]

我的 docker-compose.yml 文件

version: "2.1"

services:
  springboot-api-demo:
    image: "fw/springboot-api-demo"
    mem_limit: 1024m
    ports:
      - "8080:8080"
    environment:
      - SPRING_PROFILES_ACTIVE=local
      - AWS_REGION=local
      - ENVIRONMENT=local
      - AUTH_ENABLED=false
  postgres:
    container_name: pgdb
    image: postgres:9.6-alpine
    environment:
    - 'POSTGRES_ROOT_PASSWORD=postgres'
    - 'POSTGRES_USER=postgres'
    - 'POSTGRES_PASSWORD=postgres'
    ports:
    - "54321:5432"

我在我的 application.properties

spring.datasource.url= jdbc:postgresql://localhost:54321/java_learning
spring.datasource.username=postgres
spring.datasource.password=postgres

我可以测试两个图像都已启动.同样从 docker log 和 docker events,我看到 postgres 容器运行良好,即使我可以访问它并且也创建了一个数据库.但是 springboot 容器启动了,但我死了,因为它无法连接到 postgress 并在下面抛出错误.

I can test that Both of the Images are up. Also from docker log and docker events, I see that postgres Container is running fine, even I can access it and also created a DB too.But springboot container started but i died because it could not connect to postgress and throwing error below.

无法从数据库获取连接:连接尝试失败

请注意,我的主机已经在端口 5432 上安装了 Postgres,这就是我在 postgres 容器上对 54321:5432 进行端口映射的原因.这是证明:) -

Note that my host machine already has Postgres on port 5432 thats why I did a port mapping ofr 54321:5432 on my postgres container. Here is Proof :) -

➜  springboot-api-demo git:(master) ✗ lsof -i:54321
COMMAND     PID             USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
com.docke 44345 shailendra.singh   18u  IPv4 0xf62897fbdd69e31d      0t0  TCP *:54321 (LISTEN)
com.docke 44345 shailendra.singh   21u  IPv6 0xf62897fbdd119975      0t0  TCP localhost:54321 (LISTEN)

➜  springboot-api-demo git:(master) ✗ lsof -i:5432
COMMAND  PID             USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
postgres 715 shailendra.singh    5u  IPv6 0xf62897fbb43e03b5      0t0  TCP localhost:postgresql (LISTEN)
postgres 715 shailendra.singh    6u  IPv4 0xf62897fbbaeea9bd      0t0  TCP localhost:postgresql (LISTEN)

我不确定是什么问题.但是我的 Springboot 应用程序无法连接我的 postgres 容器,该容器使用适当的凭据运行良好.

I am not sure what is the problem. But my Springboot application is not able to connect my postgres container which is running fine with proper creadentials.

推荐答案

尝试:

spring.datasource.url= jdbc:postgresql://pgdb:5432/java_learning

postgres 数据库不在本地主机上运行,​​它在另一个容器中运行,该容器具有另一个 IP(但未知).

The postgres database is not running on localhost, it's running in the other container which has an other IP (yet unknown).

幸运的是,docker-compose 会自动创建一个在 docker-compose.yml 中的所有容器之间共享的网络(除非明确说明不要这样做),因此您可以神奇地使用 服务名称作为主机名.

Thanksfully, docker-compose automatically create a network shared among all the containers in the docker-compose.yml (unless explicitly said to do not), as a result you can magically use the service name as an hostname.

此外,您在端口中有一个错字,Postgres 默认使用 5432,而不是 54321

Also, you have a typo in the port, Postgres use 5432 by default, not 54321

这篇关于Docker:Springboot 容器无法连接到 PostgreSql 容器连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:51