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
| Word | What it means | Example |
|---|---|---|
| Image | a read-only package holding a program and what it needs | |
| Container | a running copy of an image, with its own files and processes | |
| Tag | a label that names the version of an image | 1.27-alpine |
| Port | a numbered door a program waits at for visitors | 80 |
| Volume | a 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
docker ps -aShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Worked example: reading one line
| Column | What it tells you | In your lab |
|---|---|---|
| IMAGE | which image the container started from | web uses nginx, cache uses valkey |
| STATUS | running, stopped or unwell | Up, Exited or healthy |
| PORTS | the doors the program waits at | 80/tcp for web |
| NAMES | the name you type in other commands | web, 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
docker exec web psShown 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
docker exec web nginx -vShown 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
docker exec web ls /usr/share/nginx/htmlShown 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?
docker exec web head -n 1 /etc/os-releaseShown 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
docker exec web uname -rShown 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
uname -rShown 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
curl -s -o /dev/null -w "%{http_code}\n" http://webShown 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
docker exec web touch /tmp/note.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
See it there
docker exec web ls /tmpShown 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
docker exec web ls /settingsShown 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.