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

Free lesson

Run an application in a container (Docker)

After this lesson: I can run an application in a container and look inside it.

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

It worked on my laptop

A developer builds a small shop app on her laptop. It runs well. On the company server it fails, because the server has an older version of one library.

Nobody did anything wrong. The two machines were simply different. A container ships the app together with everything it needs, so it runs the same everywhere.

By the end of this lesson

Note

I can run an application in a container and look inside it.

Key words

WordWhat it meansExample
Imagea read-only package holding a program and what it needs
Containera running copy of an image, with its own files and processes
Taga label that names the version of an image1.27-alpine
Porta numbered door a program waits at for visitors80
Volumea folder kept outside the container, so it survives

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

Images are recipes, containers are dishes

An image is a package. It holds a program, the files it needs and a note about how to start it. An image never changes.

A container is what you get when you start an image. You can start many containers from one image, as you can bake many loaves from one recipe.

Your lab has three containers. Run the command to list them.

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 one line

ColumnWhat it tells youIn your lab
IMAGEwhich image the container started fromweb uses nginx, cache uses valkey
STATUSrunning, stopped or unwellUp, Exited or healthy
PORTSthe doors the program waits at80/tcp for web
NAMESthe name you type in other commandsweb, cache, worker

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

A container has its own view

Inside a container the program sees its own files and its own list of processes. It cannot see the other containers' files.

docker exec runs one command inside a container that is running. The next three commands look inside web, the container that serves a web page.

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 the server for its version

Command
docker exec web nginx -v

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

List the folder the page is served from

Command
docker exec web ls /usr/share/nginx/html

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

A common mistake

Warning

Many people think a container is a small virtual machine. It is not. A container is an ordinary process on the host, shown its own files and its own process list. It shares the host's kernel. Choose a virtual machine when you need a different operating system.

Which system do web's files come from?

Command
docker exec web head -n 1 /etc/os-release

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

Ask web which kernel it runs on

Command
docker exec web uname -r

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

Ask your own terminal the same

Command
uname -r

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

Ports let visitors in, volumes keep files

A web server waits at port 80. Other containers on the lab's network reach it by its name, web.

A container's own files last only as long as the container. Replace the container, or stop the lab, and they are gone.

A volume is a folder kept outside the container. The lab keeps one at /settings.

Visit the web port by name

Command
curl -s -o /dev/null -w "%{http_code}\n" http://web

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

Make a file in the container's own folder

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.

See it 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.

Look at the volume, without changing it

Command
docker exec web ls /settings

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

Going further

Count the nginx: worker process lines that docker exec web ps printed. Then run docker exec web nproc, which prints how many processors the container can see.

Compare the two numbers. Explain why nginx might choose that many workers, and whose processors they are.

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.