运行 docker compose 会导致“连接到 localhost:5432 被拒绝”。例外
Posted
技术标签:
【中文标题】运行 docker compose 会导致“连接到 localhost:5432 被拒绝”。例外【英文标题】:Running docker compose causes "Connection to localhost:5432 refused." exception 【发布时间】:2018-12-13 01:39:15 【问题描述】:我查看了与此问题相关的 SO 帖子 here、here、here 和 here,但我对建议的修复没有任何运气。每当我运行命令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 文件非常简单,但我一定遗漏了一些重要的东西,导致两个容器之间的内部路由失败。
编辑
我的 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”上。 您的weg-api
无法连接到数据库。 localhost:5432
上没有运行数据库。你不能像那样访问数据库。您需要改为连接到jdbc:postgresql//db:5432/weg
。您链接到的其他问题之一也清楚地说明了这一点。
ECONNREFUSED for Postgres on nodeJS with dockers的可能重复
【参考方案1】:
您的数据库在db
容器上运行,而不是在weg-api
容器内的localhost
上运行。因此,你必须改变
spring.datasource.url=jdbc:postgresql://localhost:5432/weg
到
spring.datasource.url=jdbc:postgresql://db:5432/weg
我还建议您为每个容器提供container_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"
【讨论】:
进行了编辑,使用docker build -t web-api .
重建了 weg-api 映像,并且仍然收到与以前相同的堆栈跟踪。
如果您收到 same 堆栈,那么抱歉,您没有编辑任何内容。你确定问题只是不是你遗漏了什么吗?
整个项目是开源的。你可以在这里查看代码来确认我改变了变量:github.com/jamesjmtaylor/weg-api/tree/docker
好收获。我对 Dockerfile 进行了更改,然后再次构建映像并收到错误 Step 3/13 : RUN git clone https://github.com/jamesjmtaylor/weg-api.git && git checkout docker ---> Running in 0772e0e9f9e3 Cloning into 'weg-api'... fatal: Not a git repository (or any of the parent directories): .git
。所以我改回来,将docker分支合并到master中,构建镜像(成功),然后再次运行 docker-compose 并收到相同的错误。
别担心,伙计。我不会说你的评论是准确的。我确实进行了一些批判性思考并开发了一个替代解决方案,该解决方案完全消除了检查分支的要求。删除了无关 git 分支的变量后,我不确定它为什么会生成相同的堆栈跟踪。以上是关于运行 docker compose 会导致“连接到 localhost:5432 被拒绝”。例外的主要内容,如果未能解决你的问题,请参考以下文章
如何使用Docker Compose将容器A中的Node.js TCP客户端连接到容器B中的TCP服务器?
运行 docker compose up 时 Postgres db 未连接到 docker
使用 docker-compose 连接到 RabbitMQ 容器