I created a docker compose configuration file containing a simple MySQL container and a simple Springboot application container I created. Each one works fine standalone when executed outside a docker-compose. Here is my docker-compose yaml file:
version: '3'
services:
mysql:
image: mysql
restart: always
ports:
- 3307:3306
environment:
- MYSQL_DATABASE=test
- MYSQL_USER=jonathan
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=secret
volumes:
- mysql-data:/data/mysql
my-spring-boot:
image: my-spring-boot-app:1.0
ports:
- 8081:8080
depends_on:
- mysql
volumes:
mysql-data:
driver: local
Within the springboot application, the spring.datasource.url property has the value of "jdbc:mysql://mysql:3306/test".
When starting this network, the springboot application outputs:
2021-12-03 22:06:20.591 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is 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.] with root cause
java.net.ConnectException: Connection refused (Connection refused)
What am I doing wrong?