Docker for Kubernetes · Lesson 1 · About 20 minutes
An image is a template. A container is a running copy.
By the end, you can predict what happens to a file written inside a container when it is stopped, restarted or removed. Kubernetes will make that question very concrete for your app.
You have already run other people's images. Before you build your own in the next lesson, pin down two words you have been using loosely. Five rules cover it.
The five rules
web-1/note.txtweb-2Same image underneath (grey, shared and read-only). Each container gets its own writable layer (blue, dashed). A file written in web-1 never appears in web-2.
docker run creates a container, pulling the image first if it is missing, and starts one main process. When that process ends, the container moves to Exited. It still exists, which is why docker ps hides it and docker ps -a shows it.
Docker Docs: docker run
docker start brings back a stopped container "with all its previous changes intact." But "when the container is deleted, the writable layer is also deleted." A new container from the same image starts clean.
docker run · Storage drivers
docker stop plus docker start, which keeps them. So treat a container's own filesystem as disposable. Anything that must survive goes in a volume or an outside service, which a later lesson covers.Also, Kubernetes nodes don't run Docker Engine any more; they use a runtime such as containerd. Your images still work there: "the images produced from
docker build will work with all CRI implementations" (Kubernetes dockershim FAQ).Try it: the simulator
The simulator below is a small Docker terminal. Type real commands; the panel underneath shows each image, each container and what is in its writable layer. Before each command, say out loud what you expect to happen. The challenges tick themselves off.
Useful commands: docker run, ps -a, exec, diff, stop, start, rm, rmi. Type help for the rest. Press ↑ to recall your last command.
Stuck? Hints, one per challenge
docker run alpine:3.20 echo hello, thendocker psanddocker ps -a.sleep 3600keeps a process alive for an hour. You need-dand--name box.docker exec box sh -c 'echo "my data" > /note.txt'. Quote the whole command so the>happens inside the container.docker run --rm alpine:3.20 cat /note.txt- Stop it, start it, then
docker exec box cat /note.txt. docker rmirefuses while any container, even a stopped one, still uses the image.docker ps -ashows who.
Now on your machine
Start Docker Desktop, then run these in a terminal one block at a time. Every container here is named dk-…, so your other containers are untouched. The lines starting with # show what you should see.
docker run --name dk-once alpine:3.20 echo "hello from a container"
docker ps # nothing: it already exited
docker ps -a --filter name=dk- # dk-once Exited (0)
docker run -d --name dk-box alpine:3.20 sleep 3600
docker top dk-box # one process: sleep 3600. That is the container.
docker exec dk-box sh -c 'echo "my data" > /note.txt'
docker diff dk-box # A /note.txt (A = added in the writable layer)
docker run --rm alpine:3.20 cat /note.txt # No such file: a new container starts clean
docker stop dk-box # takes about 10 seconds. Remember this for lesson 7.
docker start dk-box
docker exec dk-box cat /note.txt # my data: it survived the stop
docker rm -f dk-box dk-once
docker run -d --name dk-box alpine:3.20 sleep 3600
docker exec dk-box cat /note.txt # No such file: removal deleted the layer
docker rm -f dk-box
Why does docker stop pause? It sends the main process SIGTERM, waits a grace period (10 seconds on Linux), then sends SIGKILL (docker stop). sleep ignores the polite signal. Kubernetes stops containers the same way, which is why your Java app will need to handle it.
Check yourself
Answer from memory without scrolling up. Getting a few wrong now is useful, because the effort of recalling is what makes it stick.
Keep going
Read next: Docker's two short concept pages, What is a container? and What is an image?. Each has a five-minute video from the Docker team.
Keep for review: the Docker CLI cheat sheet and the glossary. Later lessons use the glossary's wording.
Ask your teacher: anything unclear is worth asking about in the chat, however small. When you finish, send your quiz score and which questions you missed, plus whether you ran the lab on your machine, so the next lesson starts in the right place.