从SpringBoot Docker容器内部访问本地Postgres DB

时间:2018-03-23 01:32:34

标签: docker

我有一个springboot应用程序访问我的localhost:5432 postgress数据库

我已将它停靠并希望从docker容器连接到数据库

像这样运行容器

docker run  -p 8080:8080 risk-assesment:v1

我收到以下错误

2018-03-22 20:29:34.105  INFO 7 --- [           main] c.c.h.r.RiskAssessmentApplication        : The following profiles are active: local
2018-03-22 20:29:34.180  INFO 7 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2ac1fdc4: startup date [Thu Mar 22 20:29:34 CDT 2018]; root of context hierarchy
2018-03-22 20:29:36.568  INFO 7 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-03-22 20:29:36.838  INFO 7 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$af64b9d0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-03-22 20:29:37.516  INFO 7 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9000 (http)
2018-03-22 20:29:37.570  INFO 7 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-03-22 20:29:37.571  INFO 7 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.28
2018-03-22 20:29:37.595  INFO 7 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-03-22 20:29:37.754  INFO 7 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-03-22 20:29:37.755  INFO 7 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3578 ms
2018-03-22 20:29:39.215  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-03-22 20:29:39.224  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-03-22 20:29:39.226  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-03-22 20:29:39.226  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-03-22 20:29:39.227  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-03-22 20:29:39.227  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpTraceFilter' to: [/*]
2018-03-22 20:29:39.228  INFO 7 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webMvcMetricsFilter' to: [/*]
2018-03-22 20:29:39.538  INFO 7 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-03-22 20:29:39.625  WARN 7 --- [           main] unknown.jul.logger                       : ConnectException occurred while connecting to localhost:5432

1 个答案:

答案 0 :(得分:0)

如果您使用的是mac而不是localhost:5432使用此功能从docker容器内部连接到您的计算机。

docker.for.mac.localhost:5432

如果这不起作用

进入泊坞窗容器

docker exec -it yourcontainername /bin/bash

获取主机IP地址

netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}'

在springboot aplication中使用此ip而不是localhost。