Lesson 2 of 4 · Free to read
Exercises
Exercises — Containerization with Docker and Compose
Watch & learn
Course video
Keep a prompt log while you work. Speed without a trail is not a skill — it is luck.
Exercise 1 — Multi-stage build
Goal: convert a single-stage Dockerfile to multi-stage; shrink the image. Time: 25 minutes Level: foundational
Steps
cd code-examples/python/compose_lab/.- Edit
Dockerfile(currently single-stage) to multi-stage. docker build -t lab .before / after; compare sizes.
Success criteria
- Image runs.
- Multi-stage image < 60% of single-stage.
AI prompt hints
- "Convert this Dockerfile to multi-stage; output the diff only."
Solution sketch
The build stage handles deps; runtime stage installs only what's needed.
Stretch
- Switch to a distroless base; re-measure.
Exercise 2 — Compose 3 services + healthchecks
Goal: extend the Compose file to app + Postgres + Redis with healthchecks and depends_on conditions.
Time: 30 minutes
Level: intermediate
Steps
- Add Redis to
docker-compose.yml. - Add healthchecks for pg and redis.
- App
depends_on: { pg: { condition: service_healthy }, redis: { condition: service_healthy } }. docker compose upand verify only the app starts after the others are healthy.
Success criteria
- App container does not start until pg + redis are healthy.
AI prompt hints
- "Add a healthcheck block for postgres:16-alpine that uses pg_isready."
Solution sketch
The lesson: Compose can coordinate startup beyond simple ordering.
Stretch
- Add a
command:override and a separatecompose.override.ymlfor live-reload during dev.
Exercise 3 — Diagnose a broken container
Goal: a planted-broken container fails to start; diagnose using logs/inspect, propose and apply a fix. Time: 25 minutes Level: intermediate
Steps
git apply broken.patch.docker compose up; observe failure.- Diagnose using
docker logsanddocker inspect. Write 2-line root cause. - Fix; commit.
Success criteria
- Service starts.
- Root cause noted before any AI suggestion.
AI prompt hints
- (Use only after diagnosis.) "Fix this Dockerfile so the container doesn't crash on start; output the diff."
Solution sketch
Planted: wrong WORKDIR + missing env var.