📝 Dockerfile

Dockerfile: A Dockerfile is a recipe for building your own image — each instruction is a step that gets baked into a layer.

A Dockerfile is a recipe for building your own image — each instruction is a step that gets baked into a layer. Docker Compose is the conductor who starts multiple musicians at once: instead of typing 5 separate 'docker run' commands by hand, one 'docker compose up' brings your app, database, and test runner online together, in the right order. So why not just write one giant Dockerfile that launches everything? Because a single image mixing app + database + test runner couples completely unrelated lifecycles — you'd rebuild your database image every time you fix one line of app code. It's the same reasoning behind not cramming unrelated responsibilities into one Java class: Compose's YAML is effectively dependency injection at the infrastructure level, wiring separate, independently-versioned services together. For a QA engineer, this distinction matters directly: a flaky integration test is often not your test code's fault at all, but a 'docker compose up' that started the test runner before the database container finished its health check.

Writing a Dockerfile

Dockerfile for a Python test project

Micro Lab: Dockerfile repair

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Why Does Line Order in a Dockerfile Determine Build Speed?

Docker executes the Dockerfile LINE BY…

Docker executes the Dockerfile LINE BY LINE and CACHES each line as its own LAYER — as long as a line has NOT CHANGED, that layer is NOT RE-RUN.

Copying requirements.txt BEFORE COPY . .…

Copying `requirements.txt` BEFORE `COPY . .` is DELIBERATE: every time the code changes, the `COPY . .` layer INVALIDATES, but the `pip install` layer (if requirements did NOT change) comes FROM CACHE — SKIPPING a multi-minute install.

CMD runs WHEN the container starts…

`CMD` runs WHEN the container starts — `RUN` runs ONLY ONCE during the build. CONFUSING these two causes tests to run DURING the build with the RESULT saved nowhere.

🎬 From Dockerfile to a Running Container

RUN (Dependencies) Layer