📖 Scenario: You are building a simple Spring Boot application that needs to run alongside a database service. To make it easy to start both services together, you will use docker-compose to define and run these services.
🎯 Goal: Create a docker-compose.yml file that defines two services: a Spring Boot app and a PostgreSQL database. The Spring Boot app should connect to the database service using the correct hostname and port.
📋 What You'll Learn
Define a service named app using the Spring Boot Docker image springboot-app:latest
Define a service named db using the official PostgreSQL image postgres:15
Set environment variables for the db service: POSTGRES_USER as user, POSTGRES_PASSWORD as password, and POSTGRES_DB as mydb
Expose port 5432 on the db service
Configure the app service to depend on db
Set environment variables for the app service to connect to the database host db and port 5432
💡 Why This Matters
🌍 Real World
Docker Compose is widely used to run multi-service applications locally or in development environments. It simplifies starting and stopping related services together.
💼 Career
Understanding docker-compose is essential for developers and DevOps engineers to manage containerized applications efficiently, especially when working with microservices or databases.
Progress0 / 4 steps
1
Create the basic docker-compose structure
Create a file named docker-compose.yml and start with the version "3.8" and an empty services section.
Spring Boot
Hint
Start your docker-compose.yml with the version and services keys.
2
Add the database service configuration
Under services, add a service named db using the image postgres:15. Set environment variables POSTGRES_USER to user, POSTGRES_PASSWORD to password, and POSTGRES_DB to mydb. Expose port 5432 on the host.
Spring Boot
Hint
Use the official PostgreSQL image and set the environment variables exactly as specified.
3
Add the Spring Boot app service with dependency
Add a service named app using the image springboot-app:latest. Make it depend on the db service using depends_on. Set environment variables SPRING_DATASOURCE_URL to jdbc:postgresql://db:5432/mydb, SPRING_DATASOURCE_USERNAME to user, and SPRING_DATASOURCE_PASSWORD to password.
Spring Boot
Hint
Use depends_on to ensure the app waits for the database. Set the Spring datasource environment variables to connect to the db service.
4
Complete the docker-compose file with network and restart policy
Add a restart policy with value always to the app service. Also, define a network named app-network and connect both app and db services to this network.
Spring Boot
Hint
Use the restart key under app and define the app-network at the bottom with driver bridge. Connect both services to this network.
Practice
(1/5)
1. What is the main purpose of using docker-compose with Spring Boot services?
easy
A. To run multiple related services together with one configuration file
B. To compile Java code faster
C. To replace the Spring Boot framework
D. To write unit tests automatically
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 A
A. POSTGRES_PASSWORD is incorrect environment variable
B. Missing port mapping for db service
C. The app tries to connect to localhost instead of the db service hostname
D. The app image name is wrong
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 C
Quick Check:
Use service name, not localhost, for inter-service DB URL [OK]
Hint: Use service names, not localhost, inside docker-compose networks [OK]
Common Mistakes:
Assuming localhost works inside containers
Forgetting to map ports for external access
Misnaming environment variables
5. You want to run a Spring Boot app with a MySQL database using docker-compose. You want the database to keep data even if the container stops. Which snippet correctly adds persistent storage for the database?
hard
A. volumes:
- /var/lib/mysql:/db-data
volumes:
db-data:
B. volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
C. volumes:
- db-data:/mysql/data
volumes:
mysql-data:
D. volumes:
- /mysql/data:/db-data
volumes:
db-data:
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-level volumes: with the same name.
Final Answer:
volumes:
- db-data:/var/lib/mysql
volumes:
db-data: -> Option B
Quick Check:
Named volume mapped to container path with declaration [OK]
Hint: Map named volume to container data path and declare it below [OK]
Common Mistakes:
Reversing host and container paths
Not declaring the named volume
Using wrong container data directory
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.