Docker Compose helps you run multiple services together easily. It lets you start all parts of your app with one command.
docker-compose for services in Spring Boot
version: '3.8' services: service_name: image: image_name:tag ports: - "host_port:container_port" environment: - KEY=value depends_on: - other_service volumes: - host_path:container_path
The version defines the Docker Compose file format version.
Each service is a container with its settings like ports and environment variables.
version: '3.8' services: app: image: springboot-app:latest ports: - "8080:8080"
version: '3.8' services: app: build: . ports: - "8080:8080" environment: - SPRING_PROFILES_ACTIVE=dev db: image: postgres:15 environment: - POSTGRES_USER=user - POSTGRES_PASSWORD=pass ports: - "5432:5432"
version: '3.8' services: app: image: springboot-app:latest depends_on: - db ports: - "8080:8080" db: image: mysql:8 environment: - MYSQL_ROOT_PASSWORD=rootpass ports: - "3306:3306"
This Docker Compose file runs a Spring Boot app and a MySQL database together. The app connects to the database using environment variables. The depends_on ensures the database starts before the app.
version: '3.8' services: springboot-app: image: springboot-app:latest ports: - "8080:8080" environment: - SPRING_DATASOURCE_URL=jdbc:mysql://mysql-db:3306/mydb - SPRING_DATASOURCE_USERNAME=root - SPRING_DATASOURCE_PASSWORD=rootpass depends_on: - mysql-db mysql-db: image: mysql:8 environment: - MYSQL_ROOT_PASSWORD=rootpass - MYSQL_DATABASE=mydb ports: - "3306:3306"
Use depends_on to control startup order but it does not wait for the service to be fully ready.
Map ports carefully to avoid conflicts on your machine.
Use environment variables to pass config like database URLs and passwords securely.
Docker Compose lets you run multiple services together with one file.
It is great for running Spring Boot apps with databases or other services locally.
Use services, ports, environment, and depends_on to configure your setup.