Hands-on Exam: CI/CD Pipelines and Docker
- You need: Docker, Python 3.12, git, bash, curl, and PyYAML (
pip install pyyaml). - Make sure nothing is listening on ports 8000 or 18000 (
docker psshould be empty). - Official documentation for Docker, Trivy, GitHub Actions and Python is allowed. Do not share work with others.
- Work only inside the lab folder. Your deliverables are listed in the final section.
| Part | Task | Time | Points |
|---|---|---|---|
| 1 | Set up the lab and confirm Docker works | 0:00 to 0:10 | 5 |
| 2 | Fix the Dockerfile, build and run the image | 0:10 to 0:35 | 25 |
| 3 | Scan the image for vulnerabilities and report | 0:35 to 1:00 | 25 |
| 4 | Write the CI pipeline | 1:00 to 1:25 | 25 |
| 5 | Troubleshoot and fix an unsafe pipeline | 1:25 to 1:45 | 10 |
| 6 | Build hygiene: .dockerignore and image check | 1:45 to 2:00 | 10 |
How to use the grader (read first)
The grader, grade.py, checks your work in each part and prints a score out of 100. You run it yourself as often as you like.
- Check Python 3.12 and Docker are installed:
python3 --versionanddocker --version. - Install the YAML library:
pip install pyyaml. - Put
grade.pyin the folder that containswms-lab, not inside it. Your exam pack includes the file, and Appendix A at the end of this paper has the same code.
python3 grade.py wms-lab
- Each check prints
PASSorFAIL, its points in brackets, and a short reason for failures. - The last line shows your score. The maximum is 100.
- The grader does not change your files. It reads your work and, for Parts 2, 3 and 6, runs Docker. It starts and removes a container from your image, and it runs a scanner container.
- Run it after each part. A failed check is a hint, not a penalty: fix it and run the grader again.
- Make sure no container is holding ports 8000 or 18000:
docker ps. Remove any leftovers withdocker rm -f <name>. - Docker must be running. If Part 1 fails, nothing else can pass.
- The Part 2 and Part 6 image checks only pass after you build
wms-api:1.0.0.
| Output | Meaning and what to do |
|---|---|
FAIL ... image wms-api:1.0.0 exists | Build it: docker build -t wms-api:1.0.0 . in wms-lab. |
FAIL ... scan target is wms-api:1.0.0 | The report was made from another image or is from an old scan. Rerun the Trivy command above. |
FAIL ... ci.yml parses | The YAML is invalid. Indentation is the usual cause. |
FAIL ... image contains no .git directory | You edited .dockerignore but did not rebuild the image. |
FAIL ... no step echoes a secret | A run: line prints a secret. Pass secrets only through env:. |
Setup (do this first, not timed beyond Part 1)
Save the script below as setup.sh in an empty folder, then run bash setup.sh. It creates the lab folder wms-lab with a small Flask app, a broken Dockerfile, a broken pipeline, and a smoke test script.
#!/usr/bin/env bash
set -euo pipefail
mkdir -p wms-lab/{app,tests,scripts,broken,fixes,.github/workflows}
cd wms-lab
touch app/__init__.py
cat > app/web.py <<'EOF'
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.get("/health")
def health():
return jsonify(status="ok", version="1.0.0")
@app.get("/add")
def add():
a = float(request.args.get("a", 0))
b = float(request.args.get("b", 0))
return jsonify(result=a + b)
EOF
cat > requirements.txt <<'EOF'
flask==3.0.3
gunicorn==22.0.0
EOF
cat > requirements-dev.txt <<'EOF'
pytest==8.3.3
EOF
cat > tests/test_web.py <<'EOF'
from app.web import app
def test_health_ok():
response = app.test_client().get("/health")
assert response.status_code == 200
assert response.get_json()["status"] == "ok"
EOF
cat > Dockerfile <<'EOF'
FROM python:latest
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD gunicorn -b 0.0.0.0:8000 app.web:app
EOF
cat > broken/ci.yml <<'EOF'
name: broken
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: docker build -t acme/wms-api:latest .
- run: docker push acme/wms-api:latest
deploy:
runs-on: ubuntu-latest
steps:
- run: echo "key is ${{ secrets.DEPLOY_KEY }}"
- run: ./scripts/deploy.sh staging
EOF
cat > scripts/smoke_test.sh <<'EOF'
#!/usr/bin/env bash
# usage: smoke_test.sh BASE_URL [ATTEMPTS] [DELAY_SECONDS]
set -euo pipefail
URL="$1"; N="${2:-20}"; D="${3:-15}"
for i in $(seq 1 "$N"); do
if curl -fsS "$URL/health" | grep -q '"status": *"ok"'; then
echo "healthy on attempt $i"; exit 0
fi
echo "attempt $i failed, retrying in $D s"; sleep "$D"
done
echo "not healthy after $N attempts" >&2; exit 1
EOF
chmod +x scripts/smoke_test.sh
echo "Lab created in $(pwd)"
Part 1: Environment (5 points, 0:00 to 0:10)
- Run
docker infoand confirm the daemon responds. - Run
docker pull aquasec/trivy:0.56.2. It is the scanner used in Part 3, so pulling it now saves time later. - Run
python3 -m pytest -qfromwms-labafterpip install -r requirements.txt -r requirements-dev.txt. The test should pass before you change anything.
Part 2: Dockerfile and image (25 points, 0:10 to 0:35)
Edit Dockerfile so that it:
- pins the base image to a Python 3.12 tag (for example
python:3.12-slim) instead oflatest; - copies
requirements.txtand installs dependencies before copying the rest of the source; - creates and switches to a non-root user, for example
appuser; - declares
EXPOSE 8000and keeps the gunicorn command on port 8000.
Then:
- Build the image:
docker build -t wms-api:1.0.0 . - Run it detached with host port 8000 mapped to container port 8000.
- Check
curl -s http://localhost:8000/healthreturns"status":"ok". - Stop and remove the container before Part 3:
docker rm -f <name>. It must not hold port 8000.
Part 3: Image security scan (25 points, 0:35 to 1:00)
Scan the image you built in Part 2 for known vulnerabilities and record the results. You will use Trivy, a scanner that runs as a container and reads the local Docker image.
- Create a folder
reportsinwms-lab. - Scan the image and write a JSON report to
reports/trivy.json:docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v "$PWD/reports:/out" aquasec/trivy:0.56.2 image --format json --output /out/trivy.json wms-api:1.0.0
- Count the findings by severity:
python3 -c "import json,collections; d=json.load(open('reports/trivy.json')); print(dict(collections.Counter(v['Severity'] for r in d.get('Results',[]) for v in (r.get('Vulnerabilities') or []))))" - Write
reports/SUMMARY.mdwith a lineCRITICAL: nand a lineHIGH: nusing your counts (write 0 if there are none). Then add one sentence naming the finding you would fix first and how, for example a newer base image or an OS package upgrade.
Part 4: CI pipeline (25 points, 1:00 to 1:25)
Create .github/workflows/ci.yml with three jobs:
- test: checks out the code, installs requirements (including dev), and runs
python -m pytest -q. - build:
needs: test; builds the image and pushes it tagged with${{ github.sha }}toghcr.io/acme/wms-api. Do not use alatesttag. - smoke:
needs: build; runs the container from the image tag and then runs./scripts/smoke_test.sh http://localhost:8000 20 15.
Part 5: Troubleshoot an unsafe pipeline (10 points, 1:25 to 1:45)
Read broken/ci.yml. It has three defects:
- the image is pushed with a mutable
latesttag; - the
deployjob has noneeds, so it can run before the build finishes; - the deploy job echoes a secret into the log.
Write the corrected workflow to fixes/ci.yml. Tag images with ${{ github.sha }}, make deploy need build, and pass the secret only through an env: block to the script, never through echo.
Part 6: Build hygiene (10 points, 1:45 to 2:00)
The image currently copies everything in wms-lab, including git metadata and caches. Fix that:
- Create
.dockerignoreinwms-labwith the entries.git,__pycache__and.venv, one per line. - Rebuild the image with
docker build -t wms-api:1.0.0 .so the change takes effect. - Check the image:
docker run --rm --entrypoint ls wms-api:1.0.0 -a /appmust not list.gitor__pycache__. If it does, the rebuild did not pick up the ignore file.
Deliverables and grading
| File (in wms-lab) | Part |
|---|---|
Dockerfile | 2 |
reports/trivy.json and reports/SUMMARY.md | 3 |
.github/workflows/ci.yml | 4 |
fixes/ci.yml | 5 |
.dockerignore | 6 |
Run the grader from the folder that contains grade.py:
python3 grade.py wms-lab
The grader prints PASS or FAIL for each check with a total score out of 100. Parts 2 and 3 run Docker, so keep Docker running and ports 8000 and 18000 free while it runs.