运行 docker compose up 时 Postgres db 未连接到 docker

Posted

技术标签:

【中文标题】运行 docker compose up 时 Postgres db 未连接到 docker【英文标题】:Postgres db is not getting connected to docker while running docker compose up 【发布时间】:2021-08-15 01:50:06 【问题描述】:

我尝试运行 github repo 将 docker 与 Spring Boot 应用程序中的 Postgresdb 集成。

我收到以下错误:

Attaching to springboot-postgresql, spring-postgres-docker-master_postgresqldb_1
[36mpostgresqldb_1  |[0m 2021-05-26T17:56:33.421051000Z 
[36mpostgresqldb_1  |[0m 2021-05-26T17:56:33.421082900Z PostgreSQL Database directory appears to contain a database; Skipping initialization
[36mpostgresqldb_1  |[0m 2021-05-26T17:56:33.421087000Z 
[36mpostgresqldb_1  |[0m 2021-05-26T17:56:33.442666500Z 2021-05-26 17:56:33.441 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
[36mpostgresqldb_1  |[0m 2021-05-26T17:56:33.443003700Z 2021-05-26 17:56:33.442 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
[36mpostgresqldb_1  |[0m 2021-05-26T17:56:33.443162700Z 2021-05-26 17:56:33.443 UTC [1] LOG:  listening on IPv6 address "::", port 5432
[36mpostgresqldb_1  |[0m 2021-05-26T17:56:33.449237200Z 2021-05-26 17:56:33.449 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
[36mpostgresqldb_1  |[0m 2021-05-26T17:56:33.460257800Z 2021-05-26 17:56:33.460 UTC [26] LOG:  database system was shut down at 2021-05-26 17:54:09 UTC
[36mpostgresqldb_1  |[0m 2021-05-26T17:56:33.465938200Z 2021-05-26 17:56:33.465 UTC [1] LOG:  database system is ready to accept connections
[36mpostgresqldb_1  |[0m 2021-05-26T18:00:11.847098000Z 2021-05-26 18:00:11.846 UTC [1] LOG:  received fast shutdown request
[36mpostgresqldb_1  |[0m 2021-05-26T18:00:11.850283600Z 2021-05-26 18:00:11.850 UTC [1] LOG:  aborting any active transactions
[36mpostgresqldb_1  |[0m 2021-05-26T18:00:11.850656500Z 2021-05-26 18:00:11.850 UTC [47] FATAL:  terminating connection due to administrator command
[36mpostgresqldb_1  |[0m 2021-05-26T18:00:11.851800800Z 2021-05-26 18:00:11.851 UTC [1] LOG:  background worker "logical replication launcher" (PID 32) exited with exit code 1
[36mpostgresqldb_1  |[0m 2021-05-26T18:00:11.851986200Z 2021-05-26 18:00:11.851 UTC [27] LOG:  shutting down
[36mpostgresqldb_1  |[0m 2021-05-26T18:00:11.876857100Z 2021-05-26 18:00:11.876 UTC [1] LOG:  database system is shut down
[36mpostgresqldb_1  |[0m 2021-05-26T18:01:00.186946400Z 
[36mpostgresqldb_1  |[0m 2021-05-26T18:01:00.186972700Z PostgreSQL Database directory appears to contain a database; Skipping initialization
[36mpostgresqldb_1  |[0m 2021-05-26T18:01:00.186977400Z 
[36mpostgresqldb_1  |[0m 2021-05-26T18:01:00.212738500Z 2021-05-26 18:01:00.212 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
[36mpostgresqldb_1  |[0m 2021-05-26T18:01:00.213944000Z 2021-05-26 18:01:00.213 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
[36mpostgresqldb_1  |[0m 2021-05-26T18:01:00.214379300Z 2021-05-26 18:01:00.214 UTC [1] LOG:  listening on IPv6 address "::", port 5432
[36mpostgresqldb_1  |[0m 2021-05-26T18:01:00.224616400Z 2021-05-26 18:01:00.224 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
[36mpostgresqldb_1  |[0m 2021-05-26T18:01:00.236474300Z 2021-05-26 18:01:00.236 UTC [27] LOG:  database system was shut down at 2021-05-26 18:00:11 UTC
[36mpostgresqldb_1  |[0m 2021-05-26T18:01:00.243839900Z 2021-05-26 18:01:00.243 UTC [1] LOG:  database system is ready to accept connections
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:18.529568500Z 2021-05-26 18:27:18.529 UTC [1] LOG:  received fast shutdown request
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:18.532785500Z 2021-05-26 18:27:18.532 UTC [1] LOG:  aborting any active transactions
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:18.534806800Z 2021-05-26 18:27:18.534 UTC [1] LOG:  background worker "logical replication launcher" (PID 33) exited with exit code 1
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:18.535712000Z 2021-05-26 18:27:18.535 UTC [28] LOG:  shutting down
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:18.557672500Z 2021-05-26 18:27:18.557 UTC [1] LOG:  database system is shut down
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:35.132022500Z 
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:35.132048100Z PostgreSQL Database directory appears to contain a database; Skipping initialization
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:35.132051200Z 
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:35.164350000Z 2021-05-26 18:27:35.163 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:35.166108300Z 2021-05-26 18:27:35.165 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:35.166170300Z 2021-05-26 18:27:35.165 UTC [1] LOG:  listening on IPv6 address "::", port 5432
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:35.173596000Z 2021-05-26 18:27:35.173 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:35.182848700Z 2021-05-26 18:27:35.182 UTC [27] LOG:  database system was shut down at 2021-05-26 18:27:18 UTC
[36mpostgresqldb_1  |[0m 2021-05-26T18:27:35.188808600Z 2021-05-26 18:27:35.188 UTC [1] LOG:  database system is ready to accept connections
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.643064400Z 
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.643145200Z   .   ____          _            __ _ _
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.643218300Z  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.643223400Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.643226700Z  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.643229600Z   '  |____| .__|_| |_|_| |_\__, | / / / /
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.643232500Z  =========|_|==============|___/=/_/_/_/
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.644210700Z  :: Spring Boot ::                (v2.5.0)
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.644238200Z 
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.741993200Z 2021-05-26 17:56:34.740  INFO 1 --- [           main] c.e.p.S.SpringDocPostApplication         : Starting SpringDocPostApplication v0.0.1-SNAPSHOT using Java 11.0.11 on b368c8a1c9f1 with PID 1 (/app.jar started by root in /)
[33mspringboot-postgresql |[0m 2021-05-26T17:56:34.742460200Z 2021-05-26 17:56:34.742  INFO 1 --- [           main] c.e.p.S.SpringDocPostApplication         : No active profile set, falling back to default profiles: default
[33mspringboot-postgresql |[0m 2021-05-26T17:56:35.355989700Z 2021-05-26 17:56:35.355  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[33mspringboot-postgresql |[0m 2021-05-26T17:56:35.405038300Z 2021-05-26 17:56:35.404  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 42 ms. Found 1 JPA repository interfaces.
[33mspringboot-postgresql |[0m 2021-05-26T17:56:35.885076900Z 2021-05-26 17:56:35.884  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
[33mspringboot-postgresql |[0m 2021-05-26T17:56:35.896293800Z 2021-05-26 17:56:35.895  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:35.896442600Z 2021-05-26 17:56:35.896  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.46]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:35.954874200Z 2021-05-26 17:56:35.954  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
[33mspringboot-postgresql |[0m 2021-05-26T17:56:35.954912200Z 2021-05-26 17:56:35.954  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1157 ms
[33mspringboot-postgresql |[0m 2021-05-26T17:56:36.166592300Z 2021-05-26 17:56:36.165  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:36.205236400Z 2021-05-26 17:56:36.204  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.31.Final
[33mspringboot-postgresql |[0m 2021-05-26T17:56:36.321015200Z 2021-05-26 17:56:36.320  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.1.2.Final
[33mspringboot-postgresql |[0m 2021-05-26T17:56:36.393416400Z 2021-05-26 17:56:36.393  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433133600Z 2021-05-26 17:56:37.432 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433160700Z 
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433176600Z org.postgresql.util.PSQLException: Connection to postgresqldb:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433180200Z     at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303) ~[postgresql-42.2.20.jar!/:42.2.20]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433183500Z     at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.20.jar!/:42.2.20]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433186400Z     at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223) ~[postgresql-42.2.20.jar!/:42.2.20]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433190300Z     at org.postgresql.Driver.makeConnection(Driver.java:465) ~[postgresql-42.2.20.jar!/:42.2.20]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433193100Z     at org.postgresql.Driver.connect(Driver.java:264) ~[postgresql-42.2.20.jar!/:42.2.20]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433195900Z     at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433198800Z     at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433201700Z     at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433204700Z     at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433207700Z     at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433210600Z     at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433213500Z     at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433216400Z     at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433220100Z     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433223900Z     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433227000Z     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433230300Z     at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433233400Z     at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433236400Z     at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433243900Z     at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433247000Z     at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433250100Z     at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433253100Z     at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433256100Z     at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433259100Z     at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:176) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433262200Z     at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:127) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433265400Z     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433268500Z     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433271500Z     at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433274500Z     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433277600Z     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433280800Z     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433283800Z     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433286800Z     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433289800Z     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433292900Z     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433298300Z     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433301500Z     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433304700Z     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433307700Z     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433310600Z     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433313500Z     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433316500Z     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433319600Z     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.7.jar!/:5.3.7]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433322500Z     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.0.jar!/:2.5.0]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433325500Z     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.5.0.jar!/:2.5.0]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433328500Z     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:438) ~[spring-boot-2.5.0.jar!/:2.5.0]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433331300Z     at org.springframework.boot.SpringApplication.run(SpringApplication.java:337) ~[spring-boot-2.5.0.jar!/:2.5.0]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433334200Z     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1336) ~[spring-boot-2.5.0.jar!/:2.5.0]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433337100Z     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1325) ~[spring-boot-2.5.0.jar!/:2.5.0]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433340000Z     at com.example.postgres.SpringDocPost.SpringDocPostApplication.main(SpringDocPostApplication.java:20) ~[classes!/:0.0.1-SNAPSHOT]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433343000Z     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433345800Z     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433348700Z     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433351600Z     at java.base/java.lang.reflect.Method.invoke(Unknown Source) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433354700Z     at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[app.jar:0.0.1-SNAPSHOT]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433359700Z     at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[app.jar:0.0.1-SNAPSHOT]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433362700Z     at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[app.jar:0.0.1-SNAPSHOT]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433365500Z     at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[app.jar:0.0.1-SNAPSHOT]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433368400Z Caused by: java.net.ConnectException: Connection refused (Connection refused)
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433371300Z     at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433374000Z     at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433376800Z     at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433379600Z     at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433382400Z     at java.base/java.net.SocksSocketImpl.connect(Unknown Source) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433385200Z     at java.base/java.net.Socket.connect(Unknown Source) ~[na:na]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433388000Z     at org.postgresql.core.PGStream.createSocket(PGStream.java:231) ~[postgresql-42.2.20.jar!/:42.2.20]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433390800Z     at org.postgresql.core.PGStream.<init>(PGStream.java:95) ~[postgresql-42.2.20.jar!/:42.2.20]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433393800Z     at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98) ~[postgresql-42.2.20.jar!/:42.2.20]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433396700Z     at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213) ~[postgresql-42.2.20.jar!/:42.2.20]
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433399700Z     ... 58 common frames omitted
[33mspringboot-postgresql |[0m 2021-05-26T17:56:37.433402500Z 

以上 docker compose up log IPv4 和 IPv6 监听端口 5432。 但是,为什么 postgresqldb 连接到端口 5433。

我已将 application.properties 定义为:

spring.datasource.url=jdbc:postgresql://postgresqldb:5432/employeedb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create

和 docker-compose.yml 一样

version: '3.1'
services:
  app:
    container_name: springboot-postgresql
    image: springboot-postgresql
    build: ./
    ports:
      - "8080:8080"
    depends_on:
      - postgresqldb
  postgresqldb:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=employeedb

即使我检查了与 5432 端口连接的数据库。

但是,我在尝试时仍然遇到错误

码头工人组成

如果可以解决我的错误,请说明我如何将我的 postgresdb 连接到 5432 端口?

我检查了 postgresql.conf,它显示 5432 端口。

【问题讨论】:

【参考方案1】:

从日志来看,数据库已启动并准备就绪。

我认为问题在于.jar的构建过程

能否尝试在项目目录下使用以下命令构建jar:

./mvnw clean package -DskipTests

然后再次尝试docker-compose up

【讨论】:

我试过这个但没有用,我认为数据库没有准备好,因为错误日志显示:连接到 postgresqldb:5433 被拒绝。检查主机名和端口是否正确,以及 postmaster 是否接受 TCP/IP 连接。 数据库已启动并在 postgres 映像日志中准备就绪:[36mpostgresqldb_1 |[0m 2021-05-26T18:27:35.188808600Z 2021-05-26 18:27:35.188 UTC [1] LOG: database system is ready to accept connections。我尝试了回购,我的电脑上没有错误。您是否在 docker/机器上运行了其他 postgres? 你用过windows吗?在运行之前,我已经删除了在 docker 中运行的其他容器/图像。即使那样,它也给出了同样的错误。 你试过docker rmi springboot-postgresql 让我们重新从头开始构建镜像吗? 如果您在删除图像时遇到错误,请尝试docker rmi -f springboot-postgresql。我认为重新创建图像可以解决您的问题。

以上是关于运行 docker compose up 时 Postgres db 未连接到 docker的主要内容,如果未能解决你的问题,请参考以下文章

如何在系统启动时运行 docker-compose up -d?

使用 docker-compose up 运行时如何优雅地停止 Dockerized Python ROS2 节点?

对于nginx容器,docker-compose up失败

使用`docker-compose run`时无法调试,但`docker-compose up`有效

无法弄清楚为啥 docker compose up 没有运行我的 React JS 应用程序

docker-compose up Windows named pipe error:(code: 2)