How to connect to a remote mysql hosting from docker container?

Viewed 749

I have a remote mysql hosting.

I have created a simple spring boot app.

I have created a .jar file of this project.

I have created a docker image from this app.jar

I have created a container which use this hosting. Everything works ok but THE PROBLEM is:

It use the parameters supplied in my config file.

Using this works ok with the parameters supplied in class file:

docker run  --name app1 -p 8000:8080 app-image:v1

What I need to do is connect to a random hosting provided when I create a container and not be linked (or coupled) to this specific server.

Here the steps:

1.- Dockerfile

FROM openjdk:8
VOLUME /tmp
EXPOSE 8080:8080
ADD ./target/app-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]

2.- Create image

docker build -t app-image:v1 . 

3.- Create a container

docker run  --name app1 -p 8000:8080 app-image:v1

Works ok, but use the parameters in DatasourceConfig

4.- My configuration in spring boot app (v2.4.3) is like this:

@Configuration
public class DatasourceConfig {
    @Bean
    public DataSource datasource() {
        return DataSourceBuilder.create()
          .driverClassName("com.mysql.cj.jdbc.Driver")
          .url("jdbc:mysql://mysql-remote.net:8192/test-database")
          .username("root")
          .password("q")
          .build(); 
    }
}

I have tried several options to override the default options, like this:

docker run  --name app1 -p 8001:8080 -e MYSQL_USER=root -e MYSQL_PASSWORD=q  -e MYSQL_DATABASE=jdbc:mysql://mysql-remote.net:8192/test-database app-image:v1

but I get:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

Any suggestions?

Thanks

UPDATE 1: Working on local I use this configuration:

 @Bean
    public DataSource datasource() {
        return DataSourceBuilder.create()
          .driverClassName("com.mysql.cj.jdbc.Driver")
          .url("jdbc:mysql://localhost:3306/test-database?serverTimezone=UTC&CuseLegacyDatetimeCode=false")
          .username("root")
          .password("q")
          .build(); 
    }

I create docker image with this configuration. As I know passing parameters should overwrite this configuration, but no way.

I have used what Ashok wrote, but it doesn't work.

UPDATE 2 (RESOLVED)

Added this code in DatasourceConfig, using default values to work in local:

    @Value("${custom.property.url:jdbc:mysql://localhost:3306/test-database?serverTimezone=UTC&CuseLegacyDatetimeCode=false}")
    String url;

    @Value("${custom.property.username:root}")
    String username;

    @Value("${custom.property.password:q}")
    String password;
    
    @Bean
    public DataSource datasource() {
        return DataSourceBuilder.create()
          .driverClassName("com.mysql.cj.jdbc.Driver")
          .url(url)
          .username(username)
          .password(password)
          .build(); 
    }
}

Later I create the image as usually and I run the container like this (overwriting the parameters and connecting to an external mysql hosting FROM my docker container). I tested the app with several containers and it works great!!

docker run --name app1 -p 8001:8080 -e custom.property.username=root -e custom.property.password=q  -e custom.property.url=jdbc:mysql://localhost:3306/test-database?serverTimezone=UTC&CuseLegacyDatetimeCode=false app-image:v1

thanks for the help!!

3 Answers

It should be: SPRING_DATASOURCE_URL, MYSQL_USER, MYSQL_ROOT_PASSWORD.

docker run  --name app1 -p 8001:8080 -e MYSQL_USER=root -e MYSQL_ROOT_PASSWORD=q  -e SPRING_DATASOURCE_URL=jdbc:mysql://mysql-remote.net:8192/test-database app-image:v1

Try with below configurations

Docker/Linux Terminal:

docker run --name dreamsql -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=mypwd \
-e MYSQL_USER=myuser \
-e MYSQL_PASSWORD=<Password> \
-e MYSQL_DATABASE=mydata \
-d mysql:5.7

Windows CMD Prompt:

docker run --name dreamsql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypwd -e MYSQL_USER=myuser -e MYSQL_PASSWORD=<Password> -e MYSQL_DATABASE=mydata -d mysql:5.7

CHECK_BELOW should be replaced by one from below based on your requirement:

  • localhost for docker desktop

  • 192.168.99.100 for docker toolbox

  • IP address for Remote MySQL instance

    "jdbc:mysql://<CHECK_BELOW>:3306/mydata"

Note: username (MYSQL_USER): myuser and password (MYSQL_PASSWORD) as used in MySQL docker command

Related