Atul
Atul

Reputation: 3367

Remote Mysql DB not able to connect by docker-compose spring boot application

I am containerize the spring-boot application in which the DB is Mysql-8.

The DB is hosted on different machine.

We wanted to connect it from container based service but wont be able to connect it.

The machine's IP is configured in the Mysql to allow connectivity.

Is there any setting required on docker side to allow communication? OR any changes required on Mysql side?

Same thing is working when I am running the service from my machine to connect to DB.

Any inputs, pointers really appreciated.

The configuration and steps involved are:

1.The pom.xml changes to create image of spring-boot service.

             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <image>
                        <name>abc/uvw-${project.artifactId}:${project.version}</name>
                    </image>
                    <pullPolicy>IF_NOT_PRESENT</pullPolicy>
                </configuration>
            </plugin>
  1. To build the image from above changes.

spring-boot:build-image

  1. jdbc.properties file. wsnw-pc3 is the machine in network where DB is hosted.
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://wsnw-pc3:3306/xxxx?useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
jdbc.username=<username>
jdbc.password=<password>
  1. Docker compose file changes
version: '3.8'

services:
  myservice:
    image: abc/uvw-myservice:1.0
    deploy:
      resources:
        limits:
          memory: 700m
    ports:
      - "8080:8080"

Thanks,

Atul

Upvotes: 0

Views: 570

Answers (1)

Long Nguyen
Long Nguyen

Reputation: 197

This works for me when I'm trying to connect to a DB that is run on the host machine with a containerized Spring Boot app.

version: "3.8"
services:
  app:
    network_mode: host # add this line
...

Upvotes: 1

Related Questions