Home > Software engineering >  How to connect docker db from dockerized spring boot app?
How to connect docker db from dockerized spring boot app?

Time:04-15

I am beginner on docker stuff and try to learn but I am stuck so hard.

When I only run db from docker-compose.yml and run the spring app called dockerdeneme directly from IDE then it connects to db, no problem. But when I try to run both from docker-compose.yml file then the app cannot connect to db. My docker-compose.yml file is:

version: '3.9'

services:
  mysql:
    platform: linux/amd64
    image: mysql:8
    container_name: mysqlcustomerdb
    domainname: nishcustomer
    hostname: localhost
    ports:
      - '3306:3306'
    volumes:
      - ~/apps/mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=1234
      - MYSQL_PASSWORD=1234
      - MYSQL_DATABASE=mysqlcustomerdb
      - DATABASE_HOST=mysqlcustomerdb
      - MYSQL_ROOT_HOST=%
      - lower_case_table_names=1
      - init-connect='GRANT CREATE USER ON . TO 'root'@'%';FLUSH PRIVILEGES;'

  dockerdeneme:
    container_name: dockerdeneme
    domainname: nishcustomer
    hostname: localhost
    image: dockerdeneme
    build: ./
    ports:
      - '8080:8080'
    environment:
      - MYSQL_ROOT_PASSWORD=1234
      - MYSQL_PASSWORD=1234
      - MYSQL_USER=root
      - MYSQL_DATABASE=mysqlcustomerdb
    depends_on:
      - mysql
volumes:
  cache:
    driver: local

Result of docker ps is:

CONTAINER ID   IMAGE           COMMAND                  CREATED             STATUS             PORTS                               NAMES
fb0037e10702   mysql:8         "docker-entrypoint.s…"   22 seconds ago      Up 21 seconds      0.0.0.0:3306->3306/tcp, 33060/tcp   mysqlcustomerdb

If I run the application from IDE with following application.properties then it is working good:

spring.datasource.url=jdbc:mysql://localhost:3306/mysqlcustomerdb
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

And it is the output of failing dockerized app:

2022-04-14 21:28:56.106  INFO 1 --- [           main] c.e.d.DockerdenemeApplication            : Starting DockerdenemeApplication v0.0.1-SNAPSHOT using Java 11.0.14.1 on localhost with PID 1 (/app.jar started by root in /)

2022-04-14 21:28:56.107  INFO 1 --- [           main] c.e.d.DockerdenemeApplication            : No active profile set, falling back to 1 default profile: "default"

2022-04-14 21:28:56.467  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2022-04-14 21:28:56.493  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 21 ms. Found 1 JPA repository interfaces.

2022-04-14 21:28:57.024  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)

2022-04-14 21:28:57.037  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]

2022-04-14 21:28:57.037  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.60]

2022-04-14 21:28:57.105  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext

2022-04-14 21:28:57.105  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 937 ms

2022-04-14 21:28:57.250  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...

2022-04-14 21:28:57.256  WARN 1 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.

2022-04-14 21:28:58.334 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.


com.mysql.cj.jdbc.exceptions.CommunicationsException: 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.

at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]

I think I am doing something wrong with port mapping or hostname/domain name but I am stuck.

Can someone show me what am I doing wrong, ports or hostname etc?

CodePudding user response:

You can first remove the MYSQL_PASSWORD variable since you have the MYSQL_ROOT_PASSWORD setted (for the two service of you docker-compose)

  - MYSQL_ROOT_PASSWORD=1234
  - MYSQL_PASSWORD=1234

Make sure that your db port is not already used. (used by the mysqld service or by a container you executed before)

I got this kind of issue one day, and it was caused by a mysql service instance that was running in the background. So if the mysql service is running in the background of your OS, just stop it. after this execute a docker-compose down && docker-compose up command to rerun the stuff in the docker-compose.

Keep in touch, If needed I'm gonna set up the same environment to reproduce the error.

CodePudding user response:

localhost don't work for docker-compose apps. As a summary, use the ip of the host or a well configured docker network. Check:

Pay special attention to : extra_hosts:

Here you have a similar docker-compose with mysql and wordpress:

  • Related