Docker part 2: write a Dockerfile and build your own test image
Write a short Dockerfile, build an image from it, run it, and read the build output, so the same test set-up starts on any machine or pipeline.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a recipe card for a lunch box. The card lists the base box, each item to add and the note on the lid. Anyone who follows the card packs the same lunch. The kitchen shelf you may take food from is only the counter in front of you, not the whole house.
The card is the Dockerfile. Each packing step is an . The counter is the .
Tom, a tester at Daily List, a to-do list app, writes the card for his pytest setup. Leo wants the same lunch on every laptop.
What it is: A Dockerfile is a text file of steps that Docker reads when you run docker build. An image layer is one saved step, and Docker can reuse it next time. The build context is the folder of files you allow Docker to copy, usually the . you pass in. docker run is the neighbour, and it starts a container from an image that already exists. Build makes the image, and run does not.
Why it matters: Without a Dockerfile, Daily List tests depend on whatever Python happens to be on Tom's laptop. Mei and Leo get different packages and a different . The file pins the base image and installs from requirements.txt. It sets the start command to pytest, so (CI) and the laptop share one setup.
PLAIN WORDS · 2 in this section
- pytest
- A free Python tool that finds and runs checks.
- reuse
- to use something again
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB A recipe card for a lunch box.
- Card
- Pack
- Shelf
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The recipe card | The Dockerfile |
| A packing step | An image layer |
| The kitchen shelf you may take from | The build context |
| The packed lunch | The image |
WHERE THIS PICTURE STOPS
- A card is read once, but a Dockerfile is run on every build.
- Items are packed in order, but unchanged layers are used again.
- A lunch is eaten, but an image can be run again and again.
HOOK
Card, steps, shelf, lunch.
TELL A FRIEND
A recipe card for a lunch box.
OFTEN MIXED UP WITH Docker from zero: images, containers, ports and volumes for testers. That topic runs a ready-made container. This one builds your own image from a Dockerfile.
PRACTISE AND KEEP
Sources for this topic — 5 sources
- Market practice
Writing a Dockerfile (Docker documentation) ↗ — Docker · Docker docs: a Dockerfile starts with FROM to pick a base image, COPY brings in files from the build context, RUN runs commands while the image is built, CMD sets the start command, and docker build with the t option names the image
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Understanding image layers ↗ — Docker · Filesystem diffs and shared image layers; not every configuration history entry creates a layer.
Actual primary documentation body read during the complete V30 domain analysis before implementation. Live documentation access-dated; no claim this version is installed in the learner environment. No artwork copied by this content change.
- Market practice
OCI Image Specification — image configuration ↗ — Open Container Initiative · history empty_layer and metadata-only history entries including CMD example.
Actual primary documentation body read during the complete V30 domain analysis before implementation. Live documentation access-dated; no claim this version is installed in the learner environment. No artwork copied by this content change.
- Market practice
Build context ↗ — Docker · Local/remote/named contexts, ordinary local COPY and COPY --from named-context exception.
Actual primary documentation body read during the complete V30 domain analysis before implementation. Live documentation access-dated; no claim this version is installed in the learner environment. No artwork copied by this content change.
- Market practice
Docker build best practices ↗ — Docker · Moving image tags versus digest-pinned identities; controlled updates.
Actual primary documentation body read during the complete V30 domain analysis before implementation. Live documentation access-dated; no claim this version is installed in the learner environment. No artwork copied by this content change.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.