Start learning free

LESSON 150 OF 188 · BACKEND: SHIPPING IT

A Dockerfile: The Base Image

Practice

To run the shop the same way on any server, you pack it into a Docker image: a snapshot of a small Linux system with Python, the packages and your code in it. A container is that image running. The recipe for an image is a file named Dockerfile, one instruction per line. Here you write its text as the Python string dockerfile, in pieces that end with \n, as in the requirements.txt lesson.

A Dockerfile starts with FROM, the image you build on, written name:tag: the tag after the colon is the version, and -slim marks the small variant. WORKDIR picks the folder where the next lines work and the app runs later; Docker creates it if it is missing. ENV sets an environment variable, the kind os.environ reads, written NAME=value. Here is the start of a Dockerfile for another program, built on Node.js:

dockerfile = (
    "FROM node:22-slim\n"
    "WORKDIR /srv\n"
    "ENV TZ=UTC\n"
)

One variable matters for Python in a container. When its output does not go to a screen, Python saves up what you print() and writes it in blocks, so the shop's log shows its lines late. PYTHONUNBUFFERED=1 makes Python write each line at once.

The check reads your lines the way docker build would. When they are right, it prints:

$ docker build -t shop .
[1/3] FROM python:3.13-slim: starts from a small Debian Linux with Python 3.13 installed and little else
[2/3] WORKDIR /app: makes the folder /app and works in it; the next lines and the app run there
[3/3] ENV PYTHONUNBUFFERED=1: sets PYTHONUNBUFFERED for every program in the image, so Python writes each printed line at once
The base is ready: python:3.13-slim, working in /app. The files of the shop come next.

Your task: write the string dockerfile with three lines: start FROM the official Python image, version 3.13, in its slim variant; work in the folder /app; and set PYTHONUNBUFFERED to 1.


Assignment
Follow the Coding Tutorial to write the first three lines of the shop's Dockerfile: FROM, WORKDIR and ENV.


Hint
Each piece of the string is one line of the file and ends with \n. Write the folder as a full path that starts with /, and NAME=value with no spaces around =.


STEP 1 OF 3
    Ctrl + Enter to check