I’m running Spring Boot, React, and MySQL inside Docker on a Hostinger VPS (Ubuntu 22.04) using the following docker-compose.yml:
version: '3.8'
services:
mysql:
image: mysql:8
container_name: mysql_container
environment:
MYSQL_ROOT_PASSWORD: abcd@123
MYSQL_DATABASE: tbappmasterdb
MYSQL_USER: abcd
MYSQL_PASSWORD: abcd@123
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
networks:
- app-network
springboot:
build:
context: ./track_your_business
container_name: springboot_backend2_container
depends_on:
- mysql
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/tbappmasterdb
SPRING_DATASOURCE_USERNAME: abcd
SPRING_DATASOURCE_PASSWORD: abcd@123
command: sh -c "sleep 20 && java -jar app.jar"
ports:
- "8082:8080"
networks:
- app-network
react:
build:
context: ./trackbusiness_rct
container_name: react_frontend2_container
depends_on:
- springboot
ports:
- "3001:80"
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
mysql_data:
⚠️ Problem:
When I run:
docker compose up -d
the MySQL container stays up, but the Spring Boot container exits with:
Communications link failure: The driver has not received any packets from the server.
What I’ve Checked:
Both containers are on the same network (app-network)
MySQL port 3306 is exposed
Tried using both mysql and mysql_container as hostnames
MySQL log shows it started correctly
Question:
What’s the correct way for Spring Boot to connect to the MySQL container inside the same Docker Compose network? Is my SPRING_DATASOURCE_URL correct or am I missing something in the setup?
Update: For security reasons, I replaced the real database username and password with dummy values ("abcd", "abcd@123") in the question. In my actual docker-compose.yml, both MySQL and Spring Boot use the same credentials.