0

I have a REST API built with Spring Boot that connects to a MySQL database running in a Docker container. The application works fine. Now I'm trying to deploy the API using Docker Compose, but I get an error when starting the container. Here is the content of my application.properties file:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/task_adc
spring.datasource.username=root
spring.datasource.password=123-abc
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update

Here is the content of my Dockerfile located at the root of my project:

FROM maven:3.8.5-openjdk-17

WORKDIR ../Task_adc_docker
COPY . .
RUN mvn clean install

CMD mvn spring-boot:run

Here is the content of my docker-compose.yml file:

services:
   mysqldb:
     image: mysql:5.7
     restart: unless-stopped
     env_file: ./.env
     environment:
        - MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
        - MYSQL_DATABASE=$MYSQLDB_DATABASE
     ports:
        - $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
     volumes:
        - db:/var/lib/mysql
app:
   depends_on:
     - mysqldb
   build: ../Task_adc_docker
   restart: on-failure
   env_file: ./.env
   ports:
     - $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
   environment:
       SPRING_APPLICATION_JSON: '{
          "spring.datasource.url"  : 
              "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
          "spring.datasource.username" : "$MYSQLDB_USER",
          "spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
          "spring.jpa.properties.hibernate.dialect" : 
              "org.hibernate.dialect.MySQL5InnoDBDialect",
          "spring.jpa.hibernate.ddl-auto" : "update"
          }'
    volumes:
       - .m2:/root/.m2
    stdin_open: true
    tty: true

 volumes:
   db:

Here is the content of my .env file:

MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=123-abc
MYSQLDB_DATABASE=task_adc
MYSQLDB_LOCAL_PORT=3307
MYSQLDB_DOCKER_PORT=3306

SPRING_LOCAL_PORT=8080
SPRING_DOCKER_PORT=8080

Here is the error I get:

    119.3 2025-04-07T11:51:25.568Z  WARN 73 --- [Task_adc] [           main] 
    o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
    119.3 2025-04-07T11:51:25.568Z ERROR 73 --- [Task_adc] [           main] 
    o.h.engine.jdbc.spi.SqlExceptionHelper   : Communications link failure
    119.3 
    119.3 The last packet sent successfully to the server was 0 milliseconds ago. The 
    driver has not received any packets from the server.
    119.3 2025-04-07T11:51:25.570Z  WARN 73 --- [Task_adc] [           main] 
    o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to 
    query metadata
    119.3 
    119.3 org.hibernate.exception.JDBCConnectionException: unable to obtain isolated JDBC 
    connection [Communications link failure
    119.3 
    119.3 The last packet sent successfully to the server was 0 milliseconds ago. The 
    driver has not received any packets from the server.] [n/a]
    119.3   at 
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDel 
   egate.java:100) ~[hibernate-core-6.6.5.Final.jar:6.6.5.Final]
   119.3    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExcepti 
   onConverter.java:58) ~[hibernate-core-6.6.5.Final.jar:6.6.5.Final]
   119.3    at 
   org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:108) 
   ~[hibernate-core-6.6.5.Final.jar:6.6.5.Final]
    119.3   at 
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:94) 
   ~[hibernate-core-6.6.5.Final.jar:6.6.5.Final]
   119.3    at 
org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWo 
   rk(JdbcIsolationDelegate.java:116) ~[hibernate-core-6.6.5.Final.jar:6.6.5.Final]
   119.3    at 
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJd 
   bcMetadata(JdbcEnvironmentInitiator.java:320) ~[hibernate-core- 
   6.6.5.Final.jar:6.6.5.Final]

Does anyone see what might be happening? I would really appreciate your help, as I'm stuck and don't know how to move forward.

Thanks in advance

1 Answer 1

1

Since both of your apps are running in container now, your database won't be accessible at 127.0.0.1 anymore. You should use the name of the service (from docker compose) instead.

Try changing the datasource url to

spring.datasource.url=jdbc:mysql://mysqldb:3306/task_adc
Sign up to request clarification or add additional context in comments.

2 Comments

I modified the connection URL in the application.properties file, replacing 127.0.0.1 with mysqldb, but I'm still getting the same error.
@user3118887 hmm, I would suggest to connect to the app container shell and check if mysqldb host is accessible at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.