Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

OS 6.4 (2 of 2): Containers and schedulers

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

55 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

It works on my machine

A developer says: "It works on my machine." The tester says: "It does not work on mine."

The two computers are not the same. One has an old version of a tool. One has a missing file.

What if the program travelled with everything it needs, in one sealed box? This is the idea of a container.

Key words

WordWhat it meansExample
ContainerA program running in its own sealed box with everything it needs.
RAMThe fast memory a computer uses while a program runs.A phone with 8 GB of RAM
Virtual machineA whole pretend computer, running inside a real one.
Operating systemThe main software that runs a computer.
ImageA saved package of a program and its files.
ContainerisationPacking software into containers so it runs the same everywhere.
RegistryAn online store of images that you can download from.
OrchestrationSoftware that starts, stops, moves and watches many containers for you.
KubernetesA popular platform that looks after containers. Its code is public.
ReplicaOne of several identical copies of a container.
ScalingRunning more copies when there is more work, and fewer when there is less.
Health checkA small test that shows a container is still working.

The cells students fill in are left blank in this preview.

What is a container?

A container holds a program and the files it needs. It runs apart from other containers.

You build a container from an image. One image can make 1 container or 100. They are all the same.

The best known tool is Docker. A file called a Dockerfile is the recipe that builds an image. It is a list of steps.

Check: containers

Have a go

Check: containers

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A Dockerfile, line by line

A line in the recipeWhat it tells Docker
FROM python:3.12Start from an image that already has Python
COPY . /appCopy my files into the folder /app inside the image
RUN pip install flaskInstall a library my program needs
CMD ["python", "/app/main.py"]When a container starts, run this command

The cells students fill in are left blank in this preview.

From image to containers

flowchart LR
  dev[Developer builds an image] --> reg[(Registry)]
  reg --> host1[Computer 1]
  reg --> host2[Computer 2]
  host1 --> c1[Container web-1]
  host1 --> c2[Container web-2]
  host2 --> c3[Container web-3]

Container or virtual machine?

ContainerVirtual machine
What it isOne program in a sealed boxA whole pretend computer with its own operating system
Size (MB and GB are units of file size. 1 GB is 1000 MB)Small: often a few hundred MBLarge: often many GB
Start timeSecondsMinutes
Good forMany small servicesRunning a different operating system

The cells students fill in are left blank in this preview.

Container or virtual machine?

Have a go

Container or virtual machine?

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Who looks after 100 containers?

One container is easy. A real service may run 100. Computers fail. The number of visitors goes up and down.

A container scheduling platform looks after them. Its work is called orchestration. Kubernetes is the best known. You tell it the state you want, such as "3 copies of web". It makes it true and keeps it true.

Keep the state you asked for

flowchart TD
  want[You ask for 3 copies] --> look{How many are running?}
  look -->|3| ok[Do nothing]
  look -->|2| start[Start 1 more]
  look -->|4| stop[Stop 1]
  start --> look
  stop --> look
  ok --> look

What a scheduler does

JobWhat it meansExample
PlaceChoose which computer runs each containerPut web-3 on the computer with the most free memory (RAM)
Keep the copiesMake sure the wanted number is runningAsk for 3, always have 3
RestartStart a container again when its health check failsweb-2 stops, so a new web-2 starts
ScaleAdd or remove copies as work changesMore copies on sale day
UpdateChange to a new image one copy at a timeUsers see no change

The cells students fill in are left blank in this preview.

Match each word to its meaning

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Fix a broken container in the lab

Press Start the lab and wait. The lab has three containers: web, cache and worker. The worker is broken on purpose. It stops as soon as it starts.

Your job is to find out why, fix it and restart it. This is what a scheduler does by itself.

1. What is running?

Command
docker ps -a

A command for the lesson’s lab terminal. It is shown here and not run.

2. Read the worker's log

Command
docker logs worker

A command for the lesson’s lab terminal. It is shown here and not run.

3. Look at the settings

Command
docker exec web cat /settings/worker.conf

A command for the lesson’s lab terminal. It is shown here and not run.

4. Add the missing line

Command
docker exec web sh -c "echo QUEUE_HOST=cache >> /settings/worker.conf"

A command for the lesson’s lab terminal. It is shown here and not run.

5. Restart the worker

Command
docker restart worker

A command for the lesson’s lab terminal. It is shown here and not run.

6. Check it

Command
docker ps

A command for the lesson’s lab terminal. It is shown here and not run.

Write up the fix

Answer from the lab. 1. What did docker ps -a show for the worker before the fix? 2. Which line of the log told you what was wrong? 3. Which command did you use to fix it, and which one to start the worker again? 4. Which of these jobs of a scheduler did you do by hand: place, keep the copies, restart, scale or update? Add your docker ps -a output from before and after if your teacher asks.

A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.

Make a free account to save your work, or press Start lab to work through the whole lesson.

Worked example: how many copies?

StepExample
Requests each second250
One container handles100 a second
Divide: 250 divided by 1002.5
Round UP, because 2 is not enough3 containers

The cells students fill in are left blank in this preview.

Write a tiny scheduler

Write the four helper functions for a tiny scheduler. containers_to_change says how many copies to start or stop. failed_containers finds the failed ones to restart. copies_needed works out how many copies the work needs, rounding up. container_name makes a name such as web-2. Press Run Tests.

Code: python
def containers_to_change(desired, running):
    # desired is how many copies we want. running is how many are running now.
    # Give back a dictionary with two keys, "start" and "stop".
    # Example: desired 3, running 1 gives {"start": 2, "stop": 0}.
    return None


def failed_containers(states):
    # states is a dictionary like {"web-1": "running", "web-2": "failed"}.
    # Give back the names of the failed ones, in alphabetical order, as a list.
    return None


def copies_needed(requests_per_second, per_container):
    # Each container handles per_container requests a second.
    # Give back how many containers we need. Always round UP, and never go below 1.
    # Example: 250 requests with 100 each needs 3.
    return None


def container_name(image, number):
    # Give back text like: web-2
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.