Docker PostgreSQL 中的连接被拒绝

Posted

技术标签:

【中文标题】Docker PostgreSQL 中的连接被拒绝【英文标题】:Connection refused in Docker PostgreSQL 【发布时间】:2019-08-23 21:18:08 【问题描述】:

我目前正在尝试将 Docker 用于我的项目 Springboot/nginx/Tomcat/Postgres

docker-compose.yml:

version: '3.1'
services:
  tomcat:
    build:
      context: ./tomcat
      dockerfile: Dockerfile
    container_name: tomcat8
    image: 'tomcat:search-api'
    links:
      - app-db
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    container_name: nginx
    image: 'nginx:searchapi'
    links:
      - tomcat
    volumes:
      - './proxyConf:/etc/nginx/conf.d'
    depends_on:
      - tomcat
    ports:
      - '80:80'
      - '443:443'
  app-db:  
    build: ./db
    ports:
      - '5432:5432'

Tomcat Dockerfile:

FROM tomcat:8.5.20-jre8

# Copy to images tomcat path
ADD app.war /usr/local/tomcat/webapps/

ADD settings.xml /usr/local/tomcat/conf/
ADD tomcat-users.xml /usr/local/tomcat/conf/

数据库 Dockerfile:

FROM postgres:10.4  

ENV POSTGRES_USER flowApplication  
ENV POSTGRES_PASSWORD flowApplication  
ENV POSTGRES_DB flowApplication 

Nginx Dockerfile:

FROM nginx:alpine

proxyConf:

server 
  listen 80;
  server_name localhost;

  location / 
    proxy_pass http://tomcat:8080;
  

application.yml: (我的 app.war 上的配置文件)

[...]
    datasource:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:postgresql://localhost:5432/flowApplication  
        username: flowApplication  
        password: flowApplication  
        hikari:
            auto-commit: false
    jpa:
        database-platform: io.github.jhipster.domain.util.FixedPostgreSQL82Dialect
        database: POSTGRESQL
        show-sql: false
        properties:
            hibernate.id.new_generator_mappings: true
            hibernate.connection.provider_disables_autocommit: true
            hibernate.cache.use_second_level_cache: false
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: false
[...]

有错误的控制台输出:

Recreating expose_nginx_app-db_1 ... done
Recreating tomcat8               ... done
Recreating nginx                 ... done
Attaching to expose_nginx_app-db_1, tomcat8, nginx
app-db_1  | 2019-04-02 13:38:10.752 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
app-db_1  | 2019-04-02 13:38:10.752 UTC [1] LOG:  listening on IPv6 address "::", port 5432
app-db_1  | 2019-04-02 13:38:10.754 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
tomcat8   | 02-Apr-2019 13:38:12.714 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.5.20
app-db_1  | 2019-04-02 13:38:10.788 UTC [23] LOG:  database system was shut down at 2019-04-02 13:37:30 UTC
tomcat8   | 02-Apr-2019 13:38:12.716 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Aug 2 2017 21:35:49 UTC
app-db_1  | 2019-04-02 13:38:10.797 UTC [1] LOG:  database system is ready to accept connections
tomcat8   | 02-Apr-2019 13:38:12.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.5.20.0
tomcat8   | 02-Apr-2019 13:38:12.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
tomcat8   | 02-Apr-2019 13:38:12.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.10.0-957.10.1.el7.x86_64
tomcat8   | 02-Apr-2019 13:38:12.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
tomcat8   | 02-Apr-2019 13:38:12.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-8-openjdk-amd64/jre
tomcat8   | 02-Apr-2019 13:38:12.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_141-8u141-b15-1~deb9u1-b15
tomcat8   | 02-Apr-2019 13:38:12.718 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
tomcat8   | 02-Apr-2019 13:38:12.718 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
tomcat8   | 02-Apr-2019 13:38:12.718 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
tomcat8   | 02-Apr-2019 13:38:12.718 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
tomcat8   | 02-Apr-2019 13:38:12.718 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
tomcat8   | 02-Apr-2019 13:38:12.719 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
tomcat8   | 02-Apr-2019 13:38:12.719 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
tomcat8   | 02-Apr-2019 13:38:12.719 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
tomcat8   | 02-Apr-2019 13:38:12.719 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
tomcat8   | 02-Apr-2019 13:38:12.719 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
tomcat8   | 02-Apr-2019 13:38:12.719 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.12] using APR version [1.5.2].
tomcat8   | 02-Apr-2019 13:38:12.720 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
tomcat8   | 02-Apr-2019 13:38:12.720 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
tomcat8   | 02-Apr-2019 13:38:12.724 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.0f  25 May 2017]
tomcat8   | 02-Apr-2019 13:38:12.843 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
tomcat8   | 02-Apr-2019 13:38:12.871 INFO [main] org.apache.tomcat.util.net.NioselectorPool.getSharedSelector Using a shared selector for servlet write/read
tomcat8   | 02-Apr-2019 13:38:12.875 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
tomcat8   | 02-Apr-2019 13:38:12.877 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
tomcat8   | 02-Apr-2019 13:38:12.877 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 759 ms
tomcat8   | 02-Apr-2019 13:38:12.915 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
tomcat8   | 02-Apr-2019 13:38:12.915 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.20
tomcat8   | 02-Apr-2019 13:38:12.962 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/usr/local/tomcat/webapps/app.war]
tomcat8   | 02-Apr-2019 13:38:18.756 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
tomcat8   |
tomcat8   | 2019-04-02 13:38:20.920  INFO 1 --- [ost-startStop-1] c.a.w.r.f.application.ApplicationWebXml  : Starting ApplicationWebXml on b42ce6e6fad3 with PID 1 (/usr/local/tomcat/webapps/app/WEB-INF/classes started by root in /usr/local/tomcat)
tomcat8   | 2019-04-02 13:38:20.926  INFO 1 --- [ost-startStop-1] c.a.w.r.f.application.ApplicationWebXml  : The following profiles are active: prod
tomcat8   | 2019-04-02 13:38:27.610 ERROR 1 --- [ost-startStop-1] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
tomcat8   |
tomcat8   | 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.
tomcat8   |     at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280)
tomcat8   |     at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
tomcat8   |     at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
tomcat8   |     at org.postgresql.Driver.makeConnection(Driver.java:454)
tomcat8   |     at org.postgresql.Driver.connect(Driver.java:256)
tomcat8   |     at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:117)
tomcat8   |     at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:123)
tomcat8   |     at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:365)
tomcat8   |     at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:194)
tomcat8   |     at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:460)
tomcat8   |     at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:534)
tomcat8   |     at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
tomcat8   |     at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
tomcat8   |     at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:385)
tomcat8   |     at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:103)
tomcat8   |     at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.afterPropertiesSet(AsyncSpringLiquibase.java:93)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1753)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1690)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
tomcat8   |     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
tomcat8   |     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)
tomcat8   |     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
tomcat8   |     at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:626)
tomcat8   |     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:445)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1246)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
tomcat8   |     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:312)
tomcat8   |     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:131)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1597)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1349)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
tomcat8   |     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
tomcat8   |     at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
tomcat8   |     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)
tomcat8   |     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
tomcat8   |     at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:819)
tomcat8   |     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:725)
tomcat8   |     at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:198)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1266)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1123)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
tomcat8   |     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
tomcat8   |     at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
tomcat8   |     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)
tomcat8   |     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
tomcat8   |     at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:819)
tomcat8   |     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:725)
tomcat8   |     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:475)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1246)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
tomcat8   |     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
tomcat8   |     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1087)
tomcat8   |     at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.createEndpointBean(EndpointDiscoverer.java:148)
tomcat8   |     at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.createEndpointBeans(EndpointDiscoverer.java:135)
tomcat8   |     at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.discoverEndpoints(EndpointDiscoverer.java:124)
tomcat8   |     at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.getEndpoints(EndpointDiscoverer.java:118)
tomcat8   |     at org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.servletEndpointRegistrar(ServletEndpointManagementContextConfiguration.java:76)
tomcat8   |     at org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration$$EnhancerBySpringCGLIB$$94f2637b.CGLIB$servletEndpointRegistrar$0(<generated>)
tomcat8   |     at org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration$$EnhancerBySpringCGLIB$$94f2637b$$FastClassBySpringCGLIB$$8ae1af32.invoke(<generated>)
tomcat8   |     at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
tomcat8   |     at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:365)
tomcat8   |     at org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration$$EnhancerBySpringCGLIB$$94f2637b.servletEndpointRegistrar(<generated>)
tomcat8   |     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
tomcat8   |     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
tomcat8   |     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
tomcat8   |     at java.lang.reflect.Method.invoke(Method.java:498)
tomcat8   |     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
tomcat8   |     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1246)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
tomcat8   |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
tomcat8   |     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
tomcat8   |     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
tomcat8   |     at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:226)
tomcat8   |     at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:214)
tomcat8   |     at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addServletContextInitializerBeans(ServletContextInitializerBeans.java:91)
tomcat8   |     at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:80)
tomcat8   |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:261)
tomcat8   |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:234)
tomcat8   |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:185)
tomcat8   |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154)
tomcat8   |     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
tomcat8   |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
tomcat8   |     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
tomcat8   |     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)
tomcat8   |     at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
tomcat8   |     at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:157)
tomcat8   |     at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:137)
tomcat8   |     at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:91)
tomcat8   |     at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
tomcat8   |     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
tomcat8   |     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
tomcat8   |     at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
tomcat8   |     at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
tomcat8   |     at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
tomcat8   |     at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:988)
tomcat8   |     at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1860)
tomcat8   |     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
tomcat8   |     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
tomcat8   |     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
tomcat8   |     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
tomcat8   |     at java.lang.Thread.run(Thread.java:748)
tomcat8   | Caused by: java.net.ConnectException: Connection refused (Connection refused)
tomcat8   |     at java.net.PlainSocketImpl.socketConnect(Native Method)
tomcat8   |     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
tomcat8   |     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
tomcat8   |     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
tomcat8   |     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
tomcat8   |     at java.net.Socket.connect(Socket.java:589)
tomcat8   |     at org.postgresql.core.PGStream.<init>(PGStream.java:70)
tomcat8   |     at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
tomcat8   |     at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
tomcat8   |     ... 125 common frames omitted
tomcat8   |

为什么在我的 Postgres 容器中连接被拒绝?

【问题讨论】:

【参考方案1】:

localhost:5432 不起作用...您应该指定类似 app-db:5432 的内容

【讨论】:

以上是关于Docker PostgreSQL 中的连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

PostgreSQL 连接被拒绝

Elastic Beanstalk 上的多容器 Docker 环境中的容器连接被拒绝

谷歌云应用引擎到 postgresql 连接被拒绝

从 grafana 连接到 postgresql

Docker,Mongodb,Windows 上的 Spring Boot 出现连接被拒绝错误

initctl:无法连接到 Upstart:无法连接到套接字 /com/ubuntu/upstart:docker 映像中的连接被拒绝