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.
Practice
docker-compose with Spring Boot services?Solution
Step 1: Understand what docker-compose does
Docker Compose is a tool to run multiple containers (services) together using one YAML file.Step 2: Relate to Spring Boot services
It helps run your Spring Boot app alongside databases or other services easily.Final Answer:
To run multiple related services together with one configuration file -> Option AQuick Check:
docker-compose = run multiple services [OK]
- Thinking docker-compose compiles code
- Confusing docker-compose with testing tools
- Believing it replaces Spring Boot
docker-compose.yml file?Solution
Step 1: Recall docker-compose port syntax
Ports are listed as a YAML array with dash and quotes, likeports: - "hostPort:containerPort".Step 2: Check options
ports: - "8080:8080" matches the correct YAML syntax for port mapping.Final Answer:
ports: - "8080:8080" -> Option AQuick Check:
Port mapping uses dash and quotes in YAML [OK]
- Omitting dash for list items
- Using wrong key 'port' instead of 'ports'
- Confusing 'expose' with 'ports'
docker-compose.yml snippet:services:
app:
image: springboot-app
ports:
- "8080:8080"
depends_on:
- db
db:
image: postgres
environment:
POSTGRES_PASSWORD: exampleWhat happens when you run
docker-compose up?Solution
Step 1: Understand depends_on behavior
The app service depends on db, so docker-compose starts db first, then app.Step 2: Check environment and ports
db has required environment variable, app exposes port 8080 correctly.Final Answer:
Both app and db services start, app waits for db to start -> Option DQuick Check:
depends_on starts dependencies first [OK]
- Assuming depends_on waits for full readiness
- Thinking db is ignored without explicit start
- Confusing environment variables with ports
docker-compose.yml but your Spring Boot app fails to connect to the database:services:
app:
image: springboot-app
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://localhost:5432/mydb
db:
image: postgres
environment:
POSTGRES_PASSWORD: exampleWhat is the likely problem?
Solution
Step 1: Check datasource URL in app environment
The app uses jdbc URL with 'localhost', which inside container means itself, not db service.Step 2: Understand docker-compose networking
Services communicate by service name, so URL should use 'db' hostname, not 'localhost'.Final Answer:
The app tries to connect to localhost instead of the db service hostname -> Option CQuick Check:
Use service name, not localhost, for inter-service DB URL [OK]
- Assuming localhost works inside containers
- Forgetting to map ports for external access
- Misnaming environment variables
docker-compose. You want the database to keep data even if the container stops. Which snippet correctly adds persistent storage for the database?Solution
Step 1: Understand volume syntax for persistence
Docker volumes map a named volume to the container path where data is stored, e.g.,db-data:/var/lib/mysql.Step 2: Check volume declaration
The named volume must be declared under top-levelvolumes:with the same name.Final Answer:
volumes: - db-data:/var/lib/mysql volumes: db-data: -> Option BQuick Check:
Named volume mapped to container path with declaration [OK]
- Reversing host and container paths
- Not declaring the named volume
- Using wrong container data directory
Ready to prove it?
Pass the test and this pattern moves unknown
