Updated August 2026 — full tutorial restored for this URL.
This tutorial shows how to run a Spring Boot API and MySQL together with Docker Compose — the setup I use for demos and simple VPS deploys.
See also: Multi-stage Dockerfile with Spring Boot and PostgreSQL.
- Prerequisites
- application.properties for containers
- Dockerfile
- docker-compose.yml
- Build, run, verify
1. Prerequisites
- Docker + Docker Compose installed
- A Spring Boot app that already talks to MySQL locally
2. application.properties for containers
spring.datasource.url=jdbc:mysql://mysql:3306/appdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=app
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update
server.port=8080
Hostname mysql matches the Compose service name (Docker DNS).
3. Dockerfile
FROM eclipse-temurin:17-jdk AS build
WORKDIR /app
COPY . .
RUN ./mvnw -DskipTests package
FROM eclipse-temurin:17-jre
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app/app.jar"]
4. docker-compose.yml
services:
mysql:
image: mysql:8.0
environment:
MYSQL_DATABASE: appdb
MYSQL_USER: app
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: rootsecret
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 5s
retries: 10
api:
build: .
ports:
- "8080:8080"
depends_on:
mysql:
condition: service_healthy
volumes:
mysql_data:
5. Build, run, verify
docker compose up --build
curl http://localhost:8080/actuator/health # or your own endpoint
Common fixes: wait for MySQL healthy before the app starts; match JDBC URL host to service name; persist data with a named volume.