Postgresql 运行docker compose原因“;与本地主机的连接:5432被拒绝。”;例外

Postgresql 运行docker compose原因“;与本地主机的连接:5432被拒绝。”;例外,postgresql,docker,spring-boot,docker-compose,Postgresql,Docker,Spring Boot,Docker Compose,我已经看了这么多关于这个问题的帖子,但是我对建议的修复没有任何运气。每当我运行命令docker compose-f stack.yml up时,我都会收到以下堆栈跟踪: Attaching to weg-api_db_1, weg-api_weg-api_1 db_1 | 2018-07-04 14:57:15.384 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 db_1 | 2018-07-0

我已经看了这么多关于这个问题的帖子,但是我对建议的修复没有任何运气。每当我运行命令
docker compose-f stack.yml up
时,我都会收到以下堆栈跟踪:

Attaching to weg-api_db_1, weg-api_weg-api_1
db_1       | 2018-07-04 14:57:15.384 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1       | 2018-07-04 14:57:15.384 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1       | 2018-07-04 14:57:15.388 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1       | 2018-07-04 14:57:15.402 UTC [23] LOG:  database system was interrupted; last known up at 2018-07-04 14:45:24 UTC
db_1       | 2018-07-04 14:57:15.513 UTC [23] LOG:  database system was not properly shut down; automatic recovery in progress
db_1       | 2018-07-04 14:57:15.515 UTC [23] LOG:  redo starts at 0/16341E0
db_1       | 2018-07-04 14:57:15.515 UTC [23] LOG:  invalid record length at 0/1634218: wanted 24, got 0
db_1       | 2018-07-04 14:57:15.515 UTC [23] LOG:  redo done at 0/16341E0
db_1       | 2018-07-04 14:57:15.525 UTC [1] LOG:  database system is ready to accept connections
weg-api_1  | 
weg-api_1  |   .   ____          _            __ _ _
weg-api_1  |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
weg-api_1  | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
weg-api_1  |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
weg-api_1  |   '  |____| .__|_| |_|_| |_\__, | / / / /
weg-api_1  |  =========|_|==============|___/=/_/_/_/
weg-api_1  |  :: Spring Boot ::        (v1.5.3.RELEASE)
weg-api_1  | 
weg-api_1  | 2018-07-04 14:57:16.908  INFO 7 --- [           main] api.ApiKt                                : Starting ApiKt v0.0.1-SNAPSHOT on f9c58f4f2f27 with PID 7 (/app/spring-jpa-postgresql-spring-boot-0.0.1-SNAPSHOT.jar started by root in /app)
weg-api_1  | 2018-07-04 14:57:16.913  INFO 7 --- [           main] api.ApiKt                                : No active profile set, falling back to default profiles: default
weg-api_1  | 2018-07-04 14:57:17.008  INFO 7 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6e5e91e4: startup date [Wed Jul 04 14:57:17 GMT 2018]; root of context hierarchy
weg-api_1  | 2018-07-04 14:57:19.082  INFO 7 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
weg-api_1  | 2018-07-04 14:57:19.102  INFO 7 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
weg-api_1  | 2018-07-04 14:57:19.104  INFO 7 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.14
weg-api_1  | 2018-07-04 14:57:19.215  INFO 7 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
weg-api_1  | 2018-07-04 14:57:19.215  INFO 7 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2211 ms
weg-api_1  | 2018-07-04 14:57:19.370  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
weg-api_1  | 2018-07-04 14:57:19.375  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
weg-api_1  | 2018-07-04 14:57:19.376  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
weg-api_1  | 2018-07-04 14:57:19.376  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
weg-api_1  | 2018-07-04 14:57:19.376  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
weg-api_1  | 2018-07-04 14:57:19.867 ERROR 7 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.
weg-api_1  | 
weg-api_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.
我认为我的.yml文件非常简单,但我肯定错过了一些重要的东西,导致两个容器之间的内部路由失败

编辑

My stack.yml如下所示:

version: '3'
services:
  db:
    image: postgres
    restart: always
    container_name: db
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: password
      POSTGRES_DB: weg
    ports:
      - "5432:5432"
  weg-api:
    image: weg-api
    restart: always
    container_name: weg-api
    ports:
      - "8080:8080"
    depends_on:
      - "db"
编辑

我的Springboot应用程序属性如下:

spring.datasource.url=jdbc:postgresql://db:5432/weg
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.generate-ddl=true

我不知道如何继续。

您的数据库在
db
容器上运行,而不是在
wegapi
容器内的
localhost
上运行。因此,你必须改变

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

我还建议您为每个容器指定
container\u name
,以确保容器名称始终相同。否则,有时可能会根据配置获得不同的名称

version: '3'
services:
  db:
    image: postgres
    restart: always
    container_name: db
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: password
      POSTGRES_DB: weg
    ports:
      - "5432:5432"
  weg-api:
    image: weg-api
    restart: always
    container_name: weg-api
    ports:
      - "8080:8080"
    depends_on:
      - "db"

但是相对于您的应用程序容器,您的数据库不在本地主机上。它在“db”上。您的
wegapi
无法连接到数据库。
localhost:5432
上没有运行数据库。您不能像那样访问数据库。您需要连接到jdbc:postgresql//db:5432/weg。正如您链接到的另一个问题所明确指出的。编辑可能重复,使用
docker build-t web api重建了weg api映像。
我仍然收到与以前相同的堆栈跟踪。如果您收到的是相同的堆栈,那么很抱歉,您没有编辑任何内容。你确定问题不在于你遗漏了什么?整个项目都是开源的。您可以在这里查看代码以确认我更改了变量:是的,当然,因为当您运行git clone时https://github.com/jamesjmtaylor/weg-api.git,您不会切换到
docker
branch:-/How-about
运行git clonehttps://github.com/jamesjmtaylor/weg-api.git &&git checkout docker
?很好。我对Dockerfile进行了更改,然后再次构建映像,并收到错误
步骤3/13:运行git clonehttps://github.com/jamesjmtaylor/weg-api.git &&git checkout docker-->0772e0e9f9e3中运行的克隆到“weg api”。。。致命:不是git存储库(或任何父目录):.git
。所以我把它改回来,将docker分支合并到master中,成功地构建了映像,然后再次运行docker compose并收到相同的错误。
version: '3'
services:
  db:
    image: postgres
    restart: always
    container_name: db
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: password
      POSTGRES_DB: weg
    ports:
      - "5432:5432"
  weg-api:
    image: weg-api
    restart: always
    container_name: weg-api
    ports:
      - "8080:8080"
    depends_on:
      - "db"