Docker Compose for Spring Boot and MySQL: Setup Guide
Use a
docker-compose.yml file to define services for Spring Boot and MySQL. Configure MySQL with environment variables and link it to Spring Boot using network and ports. This setup allows both containers to run together easily with docker-compose up.Syntax
A docker-compose.yml file defines multiple services that run together. Each service has a name and configuration like image, ports, environment variables, and volumes.
- services: Lists all containers to run.
- springboot: Defines the Spring Boot app container.
- mysql: Defines the MySQL database container.
- image: Docker image to use.
- ports: Maps container ports to host ports.
- environment: Sets environment variables like database credentials.
- volumes: Persists data outside the container.
yaml
version: '3.8' services: springboot: build: . ports: - "8080:8080" environment: SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/mydb SPRING_DATASOURCE_USERNAME: user SPRING_DATASOURCE_PASSWORD: password depends_on: - mysql mysql: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: rootpassword MYSQL_DATABASE: mydb MYSQL_USER: user MYSQL_PASSWORD: password ports: - "3306:3306" volumes: - mysql-data:/var/lib/mysql volumes: mysql-data: {}
Example
This example shows a docker-compose.yml file that runs a Spring Boot app connected to a MySQL database. The Spring Boot app connects to MySQL using the service name mysql as the hostname.
yaml
version: '3.8' services: springboot: build: . ports: - "8080:8080" environment: SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/mydb SPRING_DATASOURCE_USERNAME: user SPRING_DATASOURCE_PASSWORD: password depends_on: - mysql mysql: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: rootpassword MYSQL_DATABASE: mydb MYSQL_USER: user MYSQL_PASSWORD: password ports: - "3306:3306" volumes: - mysql-data:/var/lib/mysql volumes: mysql-data: {}
Output
Creating network "project_default" with the default driver
Creating volume "project_mysql-data" with default driver
Creating project_mysql_1 ... done
Creating project_springboot_1 ... done
Starting containers:
- MySQL database is ready and listening on port 3306
- Spring Boot app is accessible on http://localhost:8080
Common Pitfalls
- Wrong database URL: Use the MySQL service name as hostname, not localhost.
- Missing depends_on: Spring Boot may start before MySQL is ready; use
depends_onto order startup. - Port conflicts: Ensure ports 8080 and 3306 are free on your host machine.
- Data loss: Without volumes, MySQL data is lost when container stops.
yaml
services:
springboot:
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/mydb # WRONG: should be 'mysql'
# Correct:
services:
springboot:
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/mydbQuick Reference
- Use
depends_onto ensure MySQL starts before Spring Boot. - Set MySQL environment variables for user, password, and database.
- Map ports to access services from your host machine.
- Use volumes to keep MySQL data persistent.
- Connect Spring Boot to MySQL using the service name as hostname.
Key Takeaways
Define Spring Boot and MySQL as services in a single docker-compose.yml file.
Use MySQL service name as hostname in Spring Boot datasource URL.
Add depends_on to start MySQL before Spring Boot.
Map ports and use volumes to persist MySQL data.
Check environment variables for database credentials carefully.