Springboot application visits database. Error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any
enter the container where the SpringBoot application is located. Myapp-in-net can ping the mysql-test container:
ping mysql-test
PING mysql-test (172.18.0.4): 56 data bytes
64 bytes from 172.18.0.4: seq=0 ttl=64 time=0.308 ms
64 bytes from 172.18.0.4: seq=1 ttl=64 time=0.186 ms
application.properties of SpringBoot (I used a multi-data source configuration so I customized the variable name):
spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test1.url = jdbc:mysql://172.18.0.4:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = root
this is docker network information
$ sudo docker network inspect my-net
[
{
"Name": "my-net",
"Id": "9351cf7ce0dc4b2f1d7f259ed6674eeda27997b375e113648e589d9ce35939a7",
"Created": "2018-02-17T08:45:01.476631336Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"1bc6f9393c45f7aa405eb837d33a924b4310321cca855655247e657d1697c034": {
"Name": "redis-in-net",
"EndpointID": "af77c4b6d91698965605a42d2d8ad05e67d2671c1ff0a03a458630e5998203f3",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"39863741c672d21a715f54788bcde08d85239613b205563eb425c66894ab01a2": {
"Name": "busybox2",
"EndpointID": "34ead20723641dbd5067b82f1d2ea1f5eddccf84e7eb0272b2a622a3fd85edf2",
"MacAddress": "02:42:ac:12:00:06",
"IPv4Address": "172.18.0.6/16",
"IPv6Address": ""
},
"8cb8107bcf1a456392971eac3a3926accfe55d1ed1a428fc6d33cd1ccdeec4f5": {
"Name": "mysql-test",
"EndpointID": "6458537bce07ce59dad7c46175865c80622bb27346455e0945c163731081b94c",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"d90df0352762b29ea9381003dd48ec230ae916f2e1507b20d1f79648fd131c52": {
"Name": "myapp-in-net",
"EndpointID": "594a039bc68d31acc11d0a6988bcc75c30c84a77aa5accfa759b1e2cc89aa203",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
it is strange that there is no problem of connection failure when accessing the redis container in the same network. Configuration:
spring.redis.host=172.18.0.2
-sharp Redis
spring.redis.port=6379