I have an api that i have built an image out of. I am trying to run it using docker compose with the services (database included) I keep getting Connection failure error, I have tried the other solutions I found on the internet to it but I was not able to solve it. At first the app was not connecting to the Db (running in a container) whether it was also running locally(the app) or running inside the container, but after changing the URL to match, I have been able to get the app to work. it now connects to the DB(running on a container) if I run the API locally but when I use docker compose to run the app(running inside a conatiner and the db inside a container), it does not connect to the database.
This is my docker compose file
`
version: “2.4”
services:
trivia-quiz:
image: omotoso/trivia-quiz-api
ports:
– “8080:8080”
environment:
SPRING_PROFILES_ACTIVE: PROD
depends_on:
– mysql
mysql:
image: mysql
ports:
– “3000:3306”
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: db
MYSQL_USER: my_user
MYSQL_PASSWORD: password
volumes:
– mysql-data:/var/lib/mysql
volumes:
mysql-data:`
This the app.yaml fle fo the spring boot app
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver password: password url: jdbc:mysql://localhost:3000/db username: my_user jpa: hibernate: ddl-auto: update show-sql: false
I am expecting the app to connect to the db successfully but I am getting this error
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:815) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:438) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:189) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-5.0.1.jar!/:na]