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
| Word | What it means | Example |
|---|---|---|
| Container | A program running in its own sealed box with everything it needs. | |
| RAM | The fast memory a computer uses while a program runs. | A phone with 8 GB of RAM |
| Virtual machine | A whole pretend computer, running inside a real one. | |
| Operating system | The main software that runs a computer. | |
| Image | A saved package of a program and its files. | |
| Containerisation | Packing software into containers so it runs the same everywhere. | |
| Registry | An online store of images that you can download from. | |
| Orchestration | Software that starts, stops, moves and watches many containers for you. | |
| Kubernetes | A popular platform that looks after containers. Its code is public. | |
| Replica | One of several identical copies of a container. | |
| Scaling | Running more copies when there is more work, and fewer when there is less. | |
| Health check | A 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 questionsA Dockerfile, line by line
| A line in the recipe | What it tells Docker |
|---|---|
| FROM python:3.12 | Start from an image that already has Python |
| COPY . /app | Copy my files into the folder /app inside the image |
| RUN pip install flask | Install 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?
| Container | Virtual machine | |
|---|---|---|
| What it is | One program in a sealed box | A 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 MB | Large: often many GB |
| Start time | Seconds | Minutes |
| Good for | Many small services | Running 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 questionsWho 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
| Job | What it means | Example |
|---|---|---|
| Place | Choose which computer runs each container | Put web-3 on the computer with the most free memory (RAM) |
| Keep the copies | Make sure the wanted number is running | Ask for 3, always have 3 |
| Restart | Start a container again when its health check fails | web-2 stops, so a new web-2 starts |
| Scale | Add or remove copies as work changes | More copies on sale day |
| Update | Change to a new image one copy at a time | Users 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?
docker ps -aA command for the lesson’s lab terminal. It is shown here and not run.
2. Read the worker's log
docker logs workerA command for the lesson’s lab terminal. It is shown here and not run.
3. Look at the settings
docker exec web cat /settings/worker.confA command for the lesson’s lab terminal. It is shown here and not run.
4. Add the missing line
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
docker restart workerA command for the lesson’s lab terminal. It is shown here and not run.
6. Check it
docker psA 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?
| Step | Example |
|---|---|
| Requests each second | 250 |
| One container handles | 100 a second |
| Divide: 250 divided by 100 | 2.5 |
| Round UP, because 2 is not enough | 3 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.
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