Sign inSign up free

836. A Slim Python Image With a Virtualenv

medium 20 XP ~12 minHands-on lab

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

The image of promo-api is built on the full python:3.12 image (compilers and all, 1.1 GB), runs as root, and its logs show up minutes late.

Task

Rewrite /home/student/projects/promo-api/Dockerfile with two stages:

  • build on python:3.12: creates a virtualenv in /opt/venv, copies requirements.txt before installing, and installs it into the venv with --no-cache-dir.
  • final stage on python:3.12-slim:
    • copies /opt/venv from build and puts /opt/venv/bin first in PATH,
    • sets PYTHONDONTWRITEBYTECODE=1 and PYTHONUNBUFFERED=1,
    • copies the application into /app and works there,
    • runs as user ID 10001,
    • starts with CMD ["python", "main.py"]. It installs nothing itself.

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: The Build Passed, the Agent Is Missing

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.