What is docker-compose used for in a Spring Boot project?
Docker-compose helps you run multiple containers together easily. For example, you can start your Spring Boot app and a database with one command.
Click to reveal answer
beginner
In a docker-compose.yml file, what does the services section define?
The services section lists all the containers you want to run. Each service is like a mini-app or tool, such as your Spring Boot app or a database.
Click to reveal answer
intermediate
How do you link a Spring Boot service to a database service in docker-compose?
You define both services under services. Then, in the Spring Boot service, you set environment variables or network settings to connect to the database service by its name.
Click to reveal answer
intermediate
What is the purpose of the depends_on key in docker-compose?
It tells Docker to start one service only after another service is started. For example, start the database before the Spring Boot app so it can connect properly.
Click to reveal answer
beginner
How can you expose your Spring Boot app's port to your local machine using docker-compose?
Use the ports key in the Spring Boot service to map container ports to your computer's ports, like 8080:8080. This lets you access the app in a browser.
Click to reveal answer
What does the services section in docker-compose.yml do?
ASpecifies network rules only
BLists files to copy into containers
CDefines containers to run together
DSets environment variables globally
✗ Incorrect
The services section defines the containers (services) that Docker Compose will start and manage.
Which key ensures one service starts after another in docker-compose?
Adepends_on
Blinks
Cvolumes
Dnetworks
✗ Incorrect
depends_on makes sure a service starts only after the service it depends on is started.
How do you make your Spring Boot app accessible on your local machine using docker-compose?
AMap ports with <code>ports</code> key
BSet <code>environment</code> variables
CUse <code>volumes</code> to share code
DAdd <code>depends_on</code> for the app
✗ Incorrect
Mapping ports with the ports key exposes container ports to your local machine.
In docker-compose, how do you connect your Spring Boot app to a database service?
ABy sharing volumes
BBy exposing ports only
CBy using <code>depends_on</code> only
DBy setting the database service name as host
✗ Incorrect
You connect by using the database service name as the host in your Spring Boot app's configuration.
What file do you write to define multiple services for Docker Compose?
ADockerfile
Bdocker-compose.yml
Capplication.properties
Dpom.xml
✗ Incorrect
The docker-compose.yml file defines multiple services and how they work together.
Explain how you would set up a Spring Boot app and a database using docker-compose.
Think about service definitions, start order, ports, and connection settings.
You got /4 concepts.
Describe the role of the ports and depends_on keys in a docker-compose.yml file.
One is about access, the other about timing.
You got /2 concepts.
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.