1

I have a springboot app that accesses my localhost:5432 postgress database

I have dockerised it and want to connect to the database from docker container

Running the container like this

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

i get the below error

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
Siraj Syed
  • 1,151
  • 1
  • 11
  • 17
  • Is the container with postgres in it also running? – Robert Moskal Mar 23 '18 at 01:37
  • postgres is installed locally on my mac – Siraj Syed Mar 23 '18 at 01:40
  • Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – Sami Kuhmonen Mar 23 '18 at 01:48
  • Localhost in Docker is the container, not the host, unless you specify —net=“host” when starting it – Sami Kuhmonen Mar 23 '18 at 01:49
  • Tried adding --net="host" when starting the containr and replaced localhost with 127.0.0.1 in the code,` ConnectException occurred while connecting to 127.0.0.1:5432` – Siraj Syed Mar 23 '18 at 01:54

1 Answers1

0

If you are on a mac instead of localhost:5432 use this to connect to your machine from inside docker container.

docker.for.mac.localhost:5432

If this dosn't work

Get inside the docker container

docker exec -it yourcontainername /bin/bash

Get your host ip address

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

In your springboot aplication use this ip instead of localhost.

jonhid
  • 2,075
  • 1
  • 11
  • 18