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

Free lesson

Fixing an app in a container (Docker)

After this lesson: I can run and fix an app in a container.

45 min lesson8 commandsDocker basics lab: learn containers, logs and restarts by fixing a broken service

The bakery app stopped on Saturday

A small bakery takes orders through a web app. On Saturday morning the orders stop arriving. The owner switches the laptop off and on again. Nothing changes.

The app is made of three small parts, each in its own container. One of them has stopped. In this lesson you learn to find the part that stopped and read why.

By the end of this lesson

Note

I can run and fix an app in a container.

Key words

WordWhat it meansExample
Imagea package holding a program and everything it needs
Containera running copy of an image
Statuswhether a container is running, stopped or unwellExited (1)
Logthe messages a program writes while it runs
Restartstop a container and start the same one again
Volumea folder kept outside a container, so it is saved

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

Before you start

Note

Press Start the lab on the bar at the top and wait until it says it is ready. Each Run button types its command into your lab terminal. Your lab is a private copy, so nothing you do here reaches anybody else.

An app is often several containers

An image is like a recipe. It never changes. A container is a running copy of an image, like a cake made from the recipe.

Your lab app has three containers. web shows a page, cache keeps a list of jobs and worker takes jobs from that list. The first command lists every container, running or not.

List every container

Command
docker ps -a

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

Worked example: reading the list

ColumnWhat it tells youIn your lab
IMAGEthe image it was made fromnginx for web, valkey for cache and worker
STATUSrunning, stopped or unwellUp (healthy) for two, Exited (1) for one
NAMESthe name you type in commandsweb, cache, worker

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

Look inside a container that runs

docker exec runs one command inside a container that is running. It is how you look around without changing anything.

The next three Runs look at the two healthy parts. The first lists the processes inside web. The second asks web for its page. The third asks cache if it is alive.

List the processes inside web

Command
docker exec web ps

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

Ask web for its page

Command
curl -s http://web

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

Ask cache if it is alive

Command
docker exec cache valkey-cli ping

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

The log says why a program stopped

A program writes messages while it runs. Together they are its log. When a container stops, its last lines often say why.

So the order is always the same. Find the stopped container. Read its log. Fix the cause. Then restart it. Here is the log of the healthy cache, so you know what a good start looks like.

Read the cache's log

Command
docker logs cache

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

Worked example: a till app that stopped

StepWhat you doWhat you find
1. Find itdocker ps -atill-print shows Exited (1)
2. Read whydocker logs till-printcannot start, no PRINTER line in printer.conf
3. Fix the causeadd the missing line to the settings filethe file now names the printer
4. Start againdocker restart till-printUp (healthy)

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

A common mistake

Warning

Many people think a restart fixes a stopped container. A restart runs the same program with the same settings, so it stops for the same reason. Read the log first and fix the cause. Restart only after that.

Make a file inside web

Command
docker exec web touch /tmp/note.txt

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

Restart web

Command
docker restart web

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

Is the file still there?

Command
docker exec web ls /tmp

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

Going further: two kinds of stop

Exited (0) means a program finished its job and stopped on purpose. Any other number, such as Exited (1), means it stopped because something went wrong.

Change your needs_attention so that Exited (0) is left out. Add one test of your own that shows it works.

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

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.