Docker Community Forums

Share and learn in the Docker community.

Couldn’t run SpringBoot project on Docker

Hello guys!
I am trying to run my SpringBoot application on Docker.
Its running fine locally on my machine, but when I try to build the image it fails when trying to connect to the MySQL and gets the exception:
“Communication failure” by JDBC
I am running separate containers of:
MySQL:latest
openjdk:latest
RabbitMQ: Management 3

When I change my application.properties connection string from “localhost:3306” to “mysql:3306” the maven build fails.
My Dockerfile:

FROM openjdk:latest

ADD target/my-app.jar /my-app.jar

EXPOSE 8080

CMD ["java", "-jar", "/my-app.jar"]

And my .properties:

spring.main.allow-bean-definition-overriding=true

spring.datasource.url=jdbc:mysql://mysql:3306/rabbit_messages?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=1234


spring.rabbitmq.host = rabbitmq
spring.rabbitmq.port = 5672
spring.rabbitmq.username = guest
spring.rabbitmq.password = guest

spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

And my docker-compose:

version: '3'

services:
  my-app:
    container_name: myapp
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    links:
      - rabbitmq
      - mysql
    environment:
      - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3307/rabbit_messages?createDatabaseIfNotExist=true
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_PASSWORD=1234
      - SPRING_RABBITMQ_HOST=rabbitmq
    depends_on:
      - rabbitmq
      - mysql

  java:
    image: openjdk:latest
    container_name: openjdk

  rabbitmq:
    image: rabbitmq:3-management
    container_name: rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"

  erlang:
    image: erlang:latest
    container_name: erlang

  mysql:
    image: mysql:latest
    container_name: mysql
    ports:
      - "3307:3307"
    environment:
      - MYSQL_DATABASE=rabbit_messages
      - MYSQL_ROOT_PASSWORD=1234
      - MYSQL_PASSWORD=1234
    restart: on-failure