Not able to connect with MySql container via spring boot rest API application

Hello Guys,

I have installed docker on ubuntu (Windows as well).
I am using docker swarm to create containers, my use case is i have a spring boot REST API communicating with MySql db.

My app is working fine with docker compose however same app is not working with docker swarm.

I am creating mysql service first using -

docker service create --name docker-mysql --replicas 1 --network mynet --env MYSQL_ROOT_PASSWORD=root --env MYSQL_DATABASE=productservice mysql

MySql service is running fine and i am able to connect it via docker exec.

However when i create another service of REST API then this service dies because on startup i am creating a connection to MySql db but as per logs REST app is not able to connect with mysql container.

product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_171]
product-service.1.rgdom4fns1k9@linuxkit-00155db44d02 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_171]
product-service.1.rgdom4fns1k9@linuxkit-00155db44d02 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_171]
product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_171]
product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_171]
product-service.1.rgdom4fns1k9@linuxkit-00155db44d02 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_171]
product-service.1.rgdom4fns1k9@linuxkit-00155db44d02 | at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_171]
product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_171]
product-service.1.rgdom4fns1k9@linuxkit-00155db44d02 | at com.mysql.jdbc.MysqlIO.(MysqlIO.java:301) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
product-service.1.rgdom4fns1k9@linuxkit-00155db44d02 | … 67 common frames omitted
product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_171]
product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
product-service.1.rgdom4fns1k9@linuxkit-00155db44d02 |
product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | at com.mysql.jdbc.MysqlIO.(MysqlIO.java:301) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
product-service.1.smo9wovadzkq@linuxkit-00155db44d02 | … 67 common frames omitted

Note that i have created an overlay network using below command -

docker network create -d overlay mynet

Can someone help me to get out of this issue as it is causing pain to me and wasted my couple of days in debugging but no results.

Any help is appreciated

-Nishant

Hey Guys,

My issue got resolved.
I used --publish parameter to publish MySql and MyApp ports.
Everything is working fine.
If someone else is facing the same issue, please use this parameter to expose the ports.

Thanks,
Nishant