5

I've looked at SO posts related to this questions here, here, here, and here but I haven't had any luck with the fixes proposed. Whenever I run the command docker-compose -f stack.yml up I receive the following stack trace:

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.

I thought that my .yml file was brain-dead-simple, but I must be missing something vital for the internal routing between the two containers to fail.

EDIT

My stack.yml is below:

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"

EDIT

My Springboot application properties are below:

spring.datasource.url=jdbc:postgresql://db:5432/weg
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.generate-ddl=true

I'm at a loss as to how to proceed.

James Jordan Taylor
  • 1,560
  • 3
  • 24
  • 38
  • But relative to your app container, your db is not on localhost. Its on “db”. – Strelok Jul 04 '18 at 15:12
  • 1
    Your `weg-api` cannot connect to the database. There is no database running on `localhost:5432`. You cannot access the db like that. You need to connect to `jdbc:postgresql//db:5432/weg` instead. As also stated clearly by one of the other questions you link to . – M. Deinum Jul 04 '18 at 15:13
  • Possible duplicate of [ECONNREFUSED for Postgres on nodeJS with dockers](https://stackoverflow.com/questions/33357567/econnrefused-for-postgres-on-nodejs-with-dockers) – David Maze Jul 04 '18 at 15:37

1 Answers1

7

Your database is running on db container, not on your localhost inside your weg-api container. Therefore, you have to change

spring.datasource.url=jdbc:postgresql://localhost:5432/weg

to

spring.datasource.url=jdbc:postgresql://db:5432/weg

I would also suggest you give container_name to each of your containers to be sure the container names are always same. Otherwise you might sometimes get different names depending on your configuration.

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"
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • 1
    Made the edits, rebuilt the weg-api image with `docker build -t web-api .` and am still receiving the same stack trace as before. – James Jordan Taylor Jul 05 '18 at 15:22
  • If you're receiving the _same_ stack, then sorry, you didn't edit anything. You sure the problem is just not you missing something? – Kevin Kopf Jul 05 '18 at 17:46
  • The entire project is open-source. You can view the code here to confirm that I changed the variables: https://github.com/jamesjmtaylor/weg-api/tree/docker – James Jordan Taylor Jul 06 '18 at 14:32
  • Yes, of course, because when you do `RUN git clone https://github.com/jamesjmtaylor/weg-api.git`, you do not switch to `docker` branch :-/ How about `RUN git clone https://github.com/jamesjmtaylor/weg-api.git && git checkout docker`? – Kevin Kopf Jul 06 '18 at 14:48
  • 1
    Good catch. I made the change to the Dockerfile and then built the image again and received the error `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`. So I changed it back and merged the ***docker*** branch into ***master***, built the image (successfully), and then ran docker-compose again and received the same error. – James Jordan Taylor Jul 06 '18 at 15:36
  • Sorry... it's `RUN git clone -b docker https://github.com/jamesjmtaylor/weg-api.git`... Don't take it wrong and personally please, but from your comment you seem like you have no idea how git works (it creates `weg-api` dir and clones repo there) or how to work with Docker. With all due respect, I'd still suggest for you to read something to boost your analytical thinking... It's very easy to derive the solution from the error message and quickly google for "git clone specific branch" than to wait for me for 3 hours to get the answer. – Kevin Kopf Jul 06 '18 at 19:01
  • Once again, sorry, I am not trying to degrade or publicly shame you, it's just a suggestion made in good faith, hoping to help you. – Kevin Kopf Jul 06 '18 at 19:04
  • 1
    No worries man. I wouldn't say your comment is accurate though. I did do some critical thinking and develop an alternative solution that removed the requirement for checking out the branch entirely. Having removed the variable of an extraneous git branch, I'm not sure why it would generate the same stacktrace. – James Jordan Taylor Jul 06 '18 at 20:04