Home:ALL Converter>using docker-compose unable to connect mysql and spring boot in same network

using docker-compose unable to connect mysql and spring boot in same network

Ask Time:2020-01-19T21:14:29         Author:Mr.Rajeh

Json Formatter

Any help will be appreciated. I am trying to run mysql and spring-boot app in two container but, in same netowrk. Using following command two container communicate each other. mysql command docker run --name mysql-local --network=spring-boot-network -p 4000:3306 -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_USER=root-user -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=testdb mysql application command docker run --network=spring-boot-network -p 8080:8080 --name test-app -e RDH_HOSTNAME:mysql-local -e RDS_PORT:4000 -e RDS_USER:root-user -e -e RDS_PASSWORD:password 171ce195f461

using above tow docker command two containers are created in spring-boot-network and they can communicate each other. problem arise when I tried to use docker-compose. I passed same environments as command line but, my app refuse to connect with mysql.

I am using spotify plugin to create an image.

<plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.3</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <repository>rajesh132/test-docker</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>

when I startup container mysql container is created and started. I can connect to it using mysqlsh client and mysql workbench as well. However, I cannot connect to my application. Following are properties and docker files that I used in my project. Docker file

ADD target/docker-test.jar docker-test.jar
ENTRYPOINT ["java", "-jar", "docker-test.jar"]

Docker Compose file

version: '3.7'
services:
  spirng-boot-test-application:
    image: rajesh132/test-docker:0.0.1-SNAPSHOT
      #build:
      #context: .
    #dockerfile: Dockerfile
    ports:
      - "8080:8080"
    restart: always
    depends_on: # Start the depends_on first
      - mysql-app
    environment:
      RDS_HOSTNAME: mysql-app
      RDS_PORT: 4000
      RDS_DB_NAME: testdb
      RDS_USERNAME: test-user
      RDS_PASSWORD: password
    networks:
      - web-application-network

  mysql-app:
    image: mysql
    ports:
      - "4000:3306"
    restart: always
    environment:

      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: test-user
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: testdb
    volumes:
      - mysql-database-data-volume:/var/lib/mysql
    networks:
      - web-application-network

# Volumes
volumes:
  mysql-database-data-volume:

networks:
  web-application-network:

application.yml

  datasource:
    #  use this url for localhost
    #use docker container name instead 'localhost'
    #  url: jdbc:mysql://localhost:3306/authorizationDb?createDatabaseIfNotExist=true
    url: jdbc:mysql://${RDS_HOSTNAME:localhost}:${RDS_PORT:3306}/${RDS_DB_NAME:test}?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
    username: ${RDS_USER:root}
    password: ${RDS_PASSWORD:password}
    driver-class-name: com.mysql.cj.jdbc.Driver
    initialization-mode: always
    tomcat:
      test-on-borrow: true
      validation-query: SELECT 1
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
      ddl-auto: update
    properties:
      hibernate:
        show_sql: true
        format_sql: true
    database-platform: org.hibernate.dialect.MySQL8Dialect

logging:
  level:
    org:
      hibernate:
        type: trace

I followed so many blogs, documentation, however coundnt succeed to build. Any help much appreciate.

Author:Mr.Rajeh,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59810341/using-docker-compose-unable-to-connect-mysql-and-spring-boot-in-same-network
yy