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

Free lesson

Docker Basics

The whole lesson is here to read. Create a free account to run it in a live lab.

Container lab6 min read6 commands

What you will do

A container is a program running in its own small, sealed box with its own files. Real systems are made of several of them working together, and when one stops, the first things you do are look at what is running, read what it said, and look around inside its neighbours. Your lab has three containers and one of them will not start. You will find out why, fix it and start it again, with the only four docker commands this terminal allows.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. The three containers are called web, cache and worker, and docker in this terminal talks only about them. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

1. See what is running, and read the log

docker ps -a lists every container, stopped ones too. Read the STATUS column: web and cache say Up and (healthy), and worker says Exited (1), so it stopped as soon as it started. Plain docker ps would not have shown it at all. A stopped container still has its log, and the second command prints it: the first line begins worker: cannot start and says its settings file has no QUEUE_HOST line.

Command
docker ps -a

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
docker logs worker

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

2. Look inside a neighbour, fix it and restart

You cannot go inside a stopped container, but web shares the settings folder with it, a named volume. docker exec runs one command inside a running container: this one prints the settings file, four lines ending with POLL_SECONDS=5. The second adds the missing line, naming the service that keeps the queue, cache. (For a shell inside, type docker exec -it web sh in your own terminal.) Then docker restart worker starts it again and prints its name.

Command
docker exec web cat /settings/worker.conf

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

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

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
docker restart worker

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

3. Check it, and see what lasts

Now worker says Up, and for a few seconds (health: starting) before (healthy): run it again if it has not changed yet. Typing docker logs worker in your own terminal now ends with worker: finished job lines. Then press Stop the lab on the lab bar and Start the lab again, and run docker ps -a: the worker comes up healthy at once, because your line is in the named volume. Anything you had written inside a container outside /settings would be gone, which is how containers behave everywhere.

Command
docker ps

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is three services and one named volume, and nothing here marks itself: the student hands in docker ps -a before and after the fix, the log line that told them what was wrong, and a sentence on what survives the lab stopping. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

About this lab and its source: Docker basics lab: learn containers, logs and restarts by fixing a broken service

Teaching a class? Set these lessons for your students with marking and a lab for every student.