0

I try to implement a SpringBoot Application with a MariaDB Connection running on Docker. But unfortunately SpringBoot is not able to connect with the DB Container.

This is my docker-compose.yml:

version: "3.4"

services:
  db:
    image: mariadb:10.6
    environment:
      MYSQL_ROOT_PASSWORD: 'admin'
      MYSQL_DATABASE: 'ghp_board'
      MYSQL_USER: 'ghp_board_admin'
      MYSQL_PASSWORD: 'ghp_board'
    volumes:
      - ./data/mariadb:/docker-entrypoint-initdb.d
    ports:
      - "33006:3306"

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - "8081:8080"
    container_name: ghp_board_frontend
    volumes:
      - ./frontend:/usr/src/app/ghp_board_frontend
    depends_on:
      - db

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    depends_on:
      - db
    ports:
      - "8886:8080"

  mailhog:
    image: mailhog/mailhog:latest
    ports:
      - "8025:8025"
      - "1025:1025"

And this is my application.properties:

spring.datasource.url=jdbc:mariadb://db:3306/ghp_board
spring.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.datasource.username=ghp_board_admin
spring.datasource.password=ghp_board

logging.level.org.hibernate.SQL=debug
spring.flyway.enabled=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

The DB-Container is running. I can connect to it via docker-compose exec db /bin/bash and then mysql -u ghp_board_admin -p

But if I am running docker-compose up --build or gradlew clean build I will get the following error:

java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:address=(host=db)(port=3306)(type=primary). db

Can anyone tell me what I did wrong?

Michael
  • 313
  • 1
  • 4
  • 10
  • Your error message is showing `host=localhost` in stead of `host=db`. Are you sure that the application properties you're showing are the ones you're using? – g00glen00b May 28 '22 at 23:50
  • Ah, that was the wrong error message. I updated it with the correct one. Thank you for the hint – Michael May 29 '22 at 07:05
  • I think the problem is that your database container isn't up and running yet by the time your backend container is trying to make a connection to your database. Check whether [this works](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y). – g00glen00b May 29 '22 at 11:55

0 Answers0