1
resposta

Erro ao rodar app via Docker

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2021-01-02 16:08:41.261  INFO 1 --- [           main] br.com.alura.forum.ForumApplication      : Starting ForumApplication v0.0.1-SNAPSHOT on c900b2efdaa9 with PID 1 (/app.jar started by spring in /)
2021-01-02 16:08:41.263  INFO 1 --- [           main] br.com.alura.forum.ForumApplication      : The following profiles are active: prod
2021-01-02 16:08:42.292  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-01-02 16:08:42.369  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 69ms. Found 3 JPA repository interfaces.
2021-01-02 16:08:43.444  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-01-02 16:08:43.456  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-01-02 16:08:43.456  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2021-01-02 16:08:43.516  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-01-02 16:08:43.517  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2196 ms
2021-01-02 16:08:43.900  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-01-02 16:08:44.366  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.17.Final
2021-01-02 16:08:44.581  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2021-01-02 16:08:44.743  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-01-02 16:08:45.784 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

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.

1 resposta

O problema está no endereço do banco localhost, o docker roda como se fosse outra máquina, logo o localhost de dentro do docker estará apontando para ele mesmo.

na aula isso funciona pq ele está usando o H2 que é um banco em memória (ou seja dentro do próprio docker da aplicação ele irá conseguir usar o H2), já você está usando o postgres, por isso não funciona

aqui eu tenho um exemplo de uma aplicação spring rodando com docker/docker-compose utilizando o postgres

https://bitbucket.org/erico_monteiro/commons-api/

repare na configuração do bando dessa aplicação de exemplo: https://bitbucket.org/erico_monteiro/commons-api/src/master/src/main/resources/application.properties

spring.datasource.url=jdbc:postgresql://postgres-compose:5432/commons
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.show-sql=true

Como eu estou utilizando o banco de dados também em docker no host da url é informado o nome do service do docker-compose

https://bitbucket.org/erico_monteiro/commons-api/src/master/src/main/docker/docker-compose.yml

version: '3.1'

services:
  commons-api-compose:
    image: ericoh/commons-api:0.0.1
    ports:
    - "18080:8080"
    networks:
      - commons-api
    depends_on:
      - postgres-compose
      - pgadmin-compose

  postgres-compose:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: "postgres"
      POSTGRES_DB: "commons"
    ports:
      - "15432:5432"
    networks:
      - commons-api

  pgadmin-compose:
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: "pgadmin"
      PGADMIN_DEFAULT_PASSWORD: "pgadmin"
    ports:
      - "15433:80"
    depends_on:
      - postgres-compose
    networks:
      - commons-api

networks:
  commons-api:
    driver: bridge