Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

app-forum-api - unhealthy - database

Tive o mesmo problema que o tópico abaixo. A aplicação não se conecta com o banco de dados pelo docker-compose.

https://cursos.alura.com.br/forum/topico-app-forum-api-unhealthy-272648

Resultado da rota: http://localhost/health

{
    "status": "DOWN",
    "components": {
        "db": {
            "status": "DOWN",
            "details": {
                "error": "org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure\n\nThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server."
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 62671097856,
                "free": 45276499968,
                "threshold": 10485760,
                "exists": true
            }
        },
        "ping": {
            "status": "UP"
        },
        "redis": {
            "status": "UP",
            "details": {
                "version": "7.0.11"
            }
        }
    }
}

docker-compose.yaml

version: '3'

networks:
  database:
    internal: true
  cache:
    internal: true
  api:
    internal: true
  monit:
  proxy:

services:
  redis-forum-api:
    image: redis
    container_name: redis-forum-api
    restart: unless-stopped
    expose:
      - 6379
    networks:
      - cache

  mysql-forum-api:
    image: mysql:5.7
    container_name: mysql-forum-api
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: 'forum'
      MYSQL_USER: 'forum'
      MYSQL_PASSWORD: 'Bk55yc1u0eiqga6e'
      MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
      MYSQL_ROOT_HOST: '%'
    volumes:
      - ./mysql:/docker-entrypoint-initdb.d
    expose:
      - 3306
    networks:
      - database
    depends_on:
      - redis-forum-api

  app-forum-api:
    build:
      context: ./app/
      dockerfile: Dockerfile
    image: app-forum-api
    container_name: app-forum-api
    restart: unless-stopped
    networks:
      - api
      - database
      - cache
    depends_on:
      - mysql-forum-api
    healthcheck:
      test: "curl -sS http://app-forum-api:8080/actuator/health"
      interval: 1s
      timeout: 30s
      retries: 60

  proxy-forum-api:
    image: nginx
    container_name: proxy-forum-api
    restart: unless-stopped
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/proxy.conf:/etc/nginx/conf.d/proxy.conf
    ports:
      - 80:80
    networks:
      - proxy
      - api
    depends_on:
      - app-forum-api

  prometheus-forum-api:
    image: prom/prometheus:latest
    container_name: prometheus-forum-api
    restart: unless-stopped
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - ./prometheus/prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle'
    ports:
      - 9090:9090
    networks:
      - monit
      - api
    depends_on:
      - proxy-forum-api

  client-forum-api:
    build:
      context: ./client/
      dockerfile: Dockerfile
    image: client-forum-api
    container_name: client-forum-api
    restart: unless-stopped
    networks:
      - proxy
    depends_on:
      - proxy-forum-api

application-prod.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://mysql-forum-api:3306/forum spring.datasource.username=forum spring.datasource.password=Bk55yc1u0eiqga6e

Realizado chmod 0777 prometheus /* (Solução do tópico mencionado)

Logs do container app-forum-api

2023-04-23 01:54:50.653 WARN 1 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 08S01 2023-04-23 01:54:50.653 ERROR 1 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper : Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 2023-04-23 01:54:50.674 ERROR 1 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause

Observação: Executando o app local funciona

1 resposta
solução!

Resolvi deletando a imagem que foi buildada.

docker image rm app-forum-api -f

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software