search

Home  >  Q&A  >  body text

springboot application cannot establish connection with sql database running on docker

<p>I have a project running in localhost using intellij as the Springboot instance, using Xampp's SQL without any issues. This works very well. But now I want to deploy everything to docker. So I created a dockerfile and a docker-compose up file in my directory like this: </p> <pre class="brush:php;toolbar:false;">-Application | -Frontend (not in docker implemented yet) -Backend(springAPI) | -dockerfile -Python (flaskAPI) (not in docker implemented yet) docker-compose.yml</pre> <p>docker-compose.yml looks like this: </p> <pre class="brush:php;toolbar:false;">version: '3.9' services: db: image: mysql:latest restart: always environment: MYSQL_ROOT_PASSWORD: <pass> MYSQL_DATABASE:mydb MYSQL_USER: <user> MYSQL_PASSWORD: <pass> ports: - "3306:3306" volumes: - ./data:/var/lib/mysql phpmyadmin: image: phpmyadmin/phpmyadmin:latest restart: always depends_on: -db ports: - "8888:80" environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: example spring-api: image: spring-api:latest ports: - "8080:8080" depends_on: -db volumes: dbdata: mysql-data:</pre> <p>The dockerfile of springapi is as follows:</p> <pre class="brush:php;toolbar:false;"># Use an official OpenJDK runtime as a parent image FROM openjdk:17-jdk-slim # Set the working directory to /app WORKDIR/app # Copy the application jar file to the container COPY build/libs/backendAPI.jar backendAPI.jar # Run the application CMD ["java", "-jar", "backendAPI.jar"]</pre> <p>This can be composed of docker and run containers. How does java spring boot throw an error and shut down.The error is: </p> <pre class="brush:php;toolbar:false;">`2023-03-06T12:25:37.603Z INFO 1 --- [main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting. .. 2023-03-06T12:25:38.685Z 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.`</pre> <p>I tried reconfiguring my application.settings like this: </p> <pre class="brush:php;toolbar:false;">spring.datasource.url=jdbc:mysql://localhost:8888/mydb spring.datasource.username=<theusername> spring.datasource.password=<thepassword> spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver</pre> <p>But it has no effect. Because it keeps throwing the same error. The SQL server can be accessed via the given url. So running fine (except not having admin rights, but that's a different issue.) </p> <p>This setup, with some tweaks to application.settings, works great when I'm just using Xammp and intellij. But now I can't connect it. What am I missing here? </p> <p><strong>Edit what I've tried so far</strong></p> <pre class="brush:php;toolbar:false;">db: image: mysql:latest restart: always container_name: sql-db</pre> <p>Then change application.settings: </p> <pre class="brush:php;toolbar:false;">spring.datasource.url=jdbc:mysql://sql-db:8888/mydb (and also with sql-db:3306)</pre> ; <p>But this still gives the same error. </p>
P粉156532706P粉156532706483 days ago693

reply all(1)I'll reply

  • P粉510127741

    P粉5101277412023-08-30 00:12:12

    Specify a container name for your database service in the docker compose file

    db:
        image: mysql:latest
        restart: always
        **container_name: db-container**

    Then in your jdbc url you should reference the configured database container name and the actual configured port of the database container.

    So your datasource.url looks like

    spring.datasource.url=jdbc:mysql://db-container:3306/mydb

    reply
    0
  • Cancelreply