Docker Compose
On this page 8 topics
Overview
Topics
- Spring Boot + PostgreSQL local environment
Learn first
2 h of practice (estimate)
Learning outcomes
- Define a multi-service local environment (app, PostgreSQL, Keycloak) in
compose.yaml. - Use health checks and dependency conditions so services start in a usable order.
- Supply configuration through environment files without committing secrets.
- Use Spring Boot's Docker Compose support for development.
Lessons
Lab and exercises
Lab
JNR-050-LAB1 · One-command Shop
Outcomes: 1–4 · Time: ~2 h docker compose up starts the whole stack (Shop, PostgreSQL, Keycloak) with seeded data, health checks, and env-file configuration. Write a README. Acceptance: a new teammate runs the stack in under 10 minutes using only the README.
Exercises (assessment: lab check)
| ID | Outcome | Task |
|---|---|---|
| JNR-050-EX01 | 1 | Write a compose.yaml with PostgreSQL (named volume) and the Shop app built from the Dockerfile. |
| JNR-050-EX02 | 1 | Add Keycloak and expose it on a host port; list which service names reach which. |
| JNR-050-EX03 | 2 | Add a pg_isready health check and make the app wait with service_healthy. |
| JNR-050-EX04 | 2 | Explain why the app can still fail even with service_healthy, and what the app should do about it. |
| JNR-050-EX05 | 3 | Move the database password to .env, add .env.example, and update .gitignore. |
| JNR-050-EX06 | 4 | Add the Spring Boot Docker Compose dependency and run the app from the IDE without starting Compose manually. |
Show solutions
- EX01 Services
db(image: postgres:16,volumes: [pgdata:/var/lib/postgresql/data]) andapp(build: .,ports: ["8080:8080"]), plus top-levelvolumes: pgdata:. - EX02
keycloakservice with its image andports;appreachesdb:5432andkeycloak:8080by service name; the browser uses the mapped host port. - EX03
healthcheck: test: ["CMD-SHELL","pg_isready -U postgres"]anddepends_on: db: condition: service_healthy. - EX04 Healthy means ready at that moment; the database can restart later. The app should use connection retries (and a pool that recovers).
- EX05
POSTGRES_PASSWORD: ${DB_PASSWORD};.env.examplehasDB_PASSWORD=change-me;.gitignorelists.env. - EX06 Add
spring-boot-docker-compose(verify the artifact and Boot version); starting the app launches the stack and wires the datasource. - Common mistakes (committed
.env,depends_onwithout health) are checked in the lab.
Quiz
Outcomes: O1 compose files · O2 health checks · O3 environment and secrets · O4 Spring Boot integration.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises JNR-050-EX01 to EX06 plus the lab; the assessment is the lab check. Pass: quiz 8/10, at least five exercises correct, and a teammate starts the stack from the README in under 10 minutes.
Common mistakes
depends_on without health checks; committed .env files.
Assessment
Lab check.
Career relevance
The capstone's local environment requirement.