CompCode Academy

Lesson 2 of 4 · Free to read

Exercises

Exercises — Containerization with Docker and Compose

Watch & learn

Course video

Open on YouTube
Docker Tutorial for Beginners (freeCodeCamp) — Images, containers, and local parity.

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

  1. cd code-examples/python/compose_lab/.
  2. Edit Dockerfile (currently single-stage) to multi-stage.
  3. 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

  1. Add Redis to docker-compose.yml.
  2. Add healthchecks for pg and redis.
  3. App depends_on: { pg: { condition: service_healthy }, redis: { condition: service_healthy } }.
  4. docker compose up and 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 separate compose.override.yml for 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

  1. git apply broken.patch.
  2. docker compose up; observe failure.
  3. Diagnose using docker logs and docker inspect. Write 2-line root cause.
  4. 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.

New free lessons and course updates. Unsubscribe from any email.