0

Im trying to dockerize by project, but when i try to run docker-compose up, my database gets failure with errror: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure. I've tried many sollutions, and none was working.

My docker-compose.yml file:

version: "3"
services:

  database:
    platform: linux/x86_64
    container_name: 'mysql'
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_PASSWORD: p@ssword
      MYSQL_USER: testUser
      MYSQL_DATABASE: testDatabase
    ports:
      - "3306:3306"
    networks:
      - mysql-db
  maildev:
    image: maildev/maildev
    ports:
      - "1080:80"
  web:
    build: .
    ports:
      - "8080:8080"
    networks:
      - mysql-db
    depends_on:
      - database
    environment:
      SPRING_DATASOURCE_PASSWORD: p@ssword
      SPRING_DATASOURCE_USER: testUser
      SPRING_DATASOURCE_NAME: testDatabase
      SPRING_DATASOURCE_URL: jdbc:mysql://database:3306/testDatabase
    links:
      - database
networks:
  mysql-db:
    driver: bridge

And my application.properties file:

spring.datasource.password=p@ssword
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://database:3306/testDatabase
spring.datasource.username=testUser
spring.mail.host=localhost
spring.mail.port=1025
spring.mail.username=hello
spring.mail.password=hello
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.show_sql=true
spring.jpa.properties.format_sql=true
spring.jpa.logging.level.org.hibernate.type=trace
spring.jpa.logging.level.org.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.logging.mail.smtp.ssl.trust=*
spring.jpa.logging.mail.smtp.auth=true
spring.jpa.logging.mail.smtp.starttls.enable=true
spring.jpa.logging.mail.smtp.connectiontimeout=5000
spring.jpa.logging.mail.smtp.timeout=3000
spring.jpa.logging.mail.smtp.writetimeout=5000
logging.level.org.hibernate.SQL=TRACE
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
vaduzion
  • 1
  • 3

1 Answers1

1

You have way more configuration than you need. I guess you are trying to solve the problem and trying any possible solution :)
So bellow is "cleaned up" version of your compose file. I removed everything that is not essential (put anything back that you know you need).

Could you spot already why this config will work? ;)

The problem is that you defined custom container_name mysql for your database service but in connection you still use database name. So you need to choose one of them and use in connection: either remove container_name declaration from database (as in the code below) or change connection string to jdbc:mysql://mysql:3306/testDatabase

version: "3"
services:
  database:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_PASSWORD: p@ssword
      MYSQL_USER: testUser
      MYSQL_DATABASE: testDatabase
  maildev:
    image: maildev/maildev
  web:
    build: .
    environment:
      SPRING_DATASOURCE_PASSWORD: p@ssword
      SPRING_DATASOURCE_USER: testUser
      SPRING_DATASOURCE_NAME: testDatabase
      SPRING_DATASOURCE_URL: jdbc:mysql://database:3306/testDatabase
    links:
      - database
Aurelijus Rozenas
  • 2,176
  • 2
  • 28
  • 40
  • Thank you, it works like a charm! I though that i am supposed to replace localhost by this "web", "database" thing. – vaduzion Dec 13 '21 at 19:18
  • well, sorry. I've tried once more time, and i still got the same error, maybe i have wrong mysql server configuration? – vaduzion Dec 13 '21 at 19:22
  • Try to do `docker-compose down` just to see that there are no persisted incorrect changes in containers. It _should_ work. If it still does not work - connect to `web` container to execute `sh` or `bash`, install ping util and mysql client and try `ping database` and `mysql -h database`. If this works you will know that mysql is up and accepting connections and the problem is in the app. – Aurelijus Rozenas Dec 14 '21 at 19:20