0
0
Spring Bootframework~5 mins

docker-compose for services in Spring Boot

Choose your learning style9 modes available
Introduction

Docker Compose helps you run multiple services together easily. It lets you start all parts of your app with one command.

When your Spring Boot app needs a database like MySQL or PostgreSQL running alongside.
When you want to run your app and a cache service like Redis together for testing.
When you have multiple microservices that need to talk to each other locally.
When you want to share your full app setup with teammates or deploy it consistently.
When you want to avoid installing all dependencies manually on your machine.
Syntax
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.

Examples
Simple service running a Spring Boot app exposing port 8080.
Spring Boot
version: '3.8'
services:
  app:
    image: springboot-app:latest
    ports:
      - "8080:8080"
Two services: Spring Boot app built locally and a Postgres database with user and password.
Spring Boot
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"
App service waits for the database service before starting.
Spring Boot
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"
Sample Program

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.

Spring Boot
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"
OutputSuccess
Important Notes

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.

Summary

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.