Search code examples
mysqlspring-bootdockerdocker-compose

springboot docker container not connecting to mySQL container that is being created along side it in docker compose


Im trying to create a docker image with a springboot application and a mysql database but im unable to connect to it from the springboot container.

Initially i thought the Springboot container exited after being unable to connect to the database due to it taking a while to start, but i created a .sh script so that the springboot container didn't run before the db was initialized, but even with the .sh i have been unable to connect to my db.

In the docker-compose i also tried removing all network related code since i thought i wasn't necessary to specify a network if im building them together like this, but it seems like it has no effect, below you can find my Dockerfile, my docker-compose, the properties of the springboot app, and the little .sh script also a the logs from the containers, any help is greatly appreciated!

Dockerfile

FROM maven:3.8.1-openjdk-11 AS build

WORKDIR /app

COPY pom.xml .
COPY src ./src

RUN mvn clean package -DskipTests

FROM openjdk:11-jre-slim

RUN apt-get update && apt-get install -y default-mysql-client && rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY wait-for-it.sh .

RUN chmod +x wait-for-it.sh

COPY --from=build /app/target/Spring-MyParcial-MVC-DataJPA-Yamid-Punto3-0.0.1-SNAPSHOT.jar .

CMD ["./wait-for-it.sh", "db:3306", "--", "java", "-jar", "Spring-MyParcial-MVC-DataJPA-Yamid-Punto3-0.0.1-SNAPSHOT.jar"]

docker-compose.yml

services:
  app:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      db:
        condition: service_healthy
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/test
      SPRING_DATASOURCE_USERNAME: qwerty
      SPRING_DATASOURCE_PASSWORD: qwerty
      SPRING_JPA_HIBERNATE_DDL_AUTO: update
    networks:
      - app-network

  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: test
      MYSQL_ROOT_PASSWORD: qwerty
      MYSQL_USER: qwerty
      MYSQL_PASSWORD: qwerty
    ports:
      - "3306:3306"
    networks:
      - app-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 10s
      retries: 10

networks:
  app-network:
    driver: bridge

application.properties

# Database connection settings
spring.datasource.url=jdbc:mysql://db:3306/test
spring.datasource.username=qwerty
spring.datasource.password=qwerty
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

wait-for-it.sh

#!/bin/bash
# wait-for-it.sh

host="$1"
shift
cmd="$@"

until mysqladmin ping -h "$host" --silent; do
  >&2 echo "MySQL is unavailable - sleeping"
  sleep 1
done

>&2 echo "MySQL is up - executing command"
exec $cmd

springboot-db-1 latest logs

2024-06-02 18:23:02 2024-06-02T23:23:02.745554Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2024-06-02 18:23:02 2024-06-02T23:23:02.745716Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.37'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2024-06-02 18:23:03 Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
2024-06-02 18:23:03 Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
2024-06-02 18:23:03 Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
2024-06-02 18:23:05 Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
2024-06-02 18:23:05 Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
2024-06-02 18:23:05 Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2024-06-02 18:23:06 2024-06-02T23:23:06.050472Z 13 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.37).
2024-06-02 18:23:08 2024-06-02T23:23:08.086402Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.37)  MySQL Community Server - GPL.
2024-06-02 18:23:09 2024-06-02T23:23:09.284886Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2024-06-02 18:23:09 2024-06-02T23:23:09.286715Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.37) starting as process 1
2024-06-02 18:23:09 2024-06-02T23:23:09.294392Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-06-02 18:23:09 2024-06-02T23:23:09.474764Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-06-02 18:23:09 2024-06-02T23:23:09.769065Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2024-06-02 18:23:09 2024-06-02T23:23:09.769133Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2024-06-02 18:23:09 2024-06-02T23:23:09.778336Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2024-06-02 18:23:09 2024-06-02T23:23:09.810279Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2024-06-02 18:23:09 2024-06-02T23:23:09.810419Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.37'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

springboot-app-1 logs, it never stops since it never connects

2024-06-02 18:24:11 MySQL is unavailable - sleeping
2024-06-02 18:24:12 MySQL is unavailable - sleeping
2024-06-02 18:24:13 MySQL is unavailable - sleeping
2024-06-02 18:24:14 MySQL is unavailable - sleeping

Solution

  • mysqladmin takes the host and the port with different options, so using -h "db:3306" will not work correctly.

    You can do it like this instead (notice the upper case -P):

    mysqladmin ping -h db -P 3306 --silent
    

    or since 3306 is the default, this is enough:

    mysqladmin ping -h db --silent
    

    So the quickest solution for your Docker image would be to change the CMD to this:

    CMD ["./wait-for-it.sh", "db", "--", "java", "-jar", "Spring-MyParcial-MVC-DataJPA-Yamid-Punto3-0.0.1-SNAPSHOT.jar"]