Sign inSign up free

835. Run Tests in Their Own Build Stage

medium 20 XP ~10 minHands-on lab

Labs have no container engine, so nothing is built: your files are checked by reading them.

The image of invoicer installs requirements-dev.txt (pytest, linters, 180 MB) and runs the tests while building, so every production image carries the test tools.

Task

Restructure /home/student/projects/invoicer/Dockerfile into three stages, so that CI can run docker build --target test . and then ship docker build .:

StageBased onDoes
basepython:3.12-slimWORKDIR /app, installs requirements.txt only, copies the source
testbaseinstalls requirements-dev.txt and runs pytest -q
runtime (the last stage)basesets the CMD; no dev requirements, no tests

The runtime stage must not depend on the test stage in any way.

About this machine

Ubuntu 24.04 with no internet access. You are student with passwordless sudo. The grader checks the final state of the machine, not the commands you typed. Values like names and ports are generated for you.

Part of Docker Fundamentals. Next step: Stop Re-Downloading Packages on Every Build

Teaching or writing about this? Embed this lab on your page

TerminalNot started

Your machine is ready to start

A fresh, isolated Ubuntu machine boots in a few seconds with the task already set up.

No account needed: 3 of 3 free tries left. Guest machines last 30 minutes.