Docker Community Forums

Share and learn in the Docker community.

Java JDBC connecting in docker can't connect to host database

i have maven container run spring boot and connect database with JDBC.

i want connect to host database

(host: Ubuntu/18.04.5 LTS (Bionic Beaver))

(database: mysql Ver 14.14 Distrib 5.7.35, for Linux (x86_64) using EditLine wrapper)

this my connection string

this.conn = DriverManager.getConnection("jdbc:mysql://172.24.0.1:3306/myDB?"+"user=xxxx&password=xxxx");

this my container IP

$ip route show
default via 172.24.0.1 dev eth0
172.24.0.0/16 dev eth0 proto kernel scope link src 172.24.0.10

i can telnet and mysql login with IP 172.24.0.1 or 172.17.0.1 in maven container

$telnet 172.24.0.1 3306
Trying 172.24.0.1...
Connected to 172.24.0.1.
Escape character is '^]'.

telnet 172.17.0.1 3306
Trying 172.17.0.1...
Connected to 172.17.0.1.
Escape character is '^]'.

but i can’t connect via JDBC with IP 172.24.0.1 or 172.17.0.1

Any help and tips are greatly appreciated!

Hello,

Are you able to login to your database from within the Docker-container using mysql-command? It might by that the user is restricted to a hostname/ip-address.

Is your code working outside the Docker-container?
And are there any messages if you use docker logs <containername|containerid> ?

Hello,

thank for your help

in my maven container, i can login to my database with mysql client command

$mysql -h 172.24.0.1 -u root -p

my code work fine outside container and in my docker-container on Macbook
but in docker-container on Linux server it doesn’t work

this is log on my maven container

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
SQLState: 08S01
VendorError: 0
java.lang.NullPointerException
.
.
.