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

1. An image is a read-only stack of layers. It holds the files, libraries and default settings an app needs. Each layer records a set of file changes. Once an image is built, it can't be modified; to change it, you build a new one. Docker Docs: What is an image?
2. A container is a process started from an image, with one thin writable layer on top. In Docker's words, "the major difference between a container and an image is the top writable layer." Many containers can share one image, and each has its own writable layer, so their changes never meet. Docker Docs: Storage drivers · What is a container?
container web-1
writable layer: /note.txt
layer 2 (read-only)
layer 1 (read-only)
container web-2
writable layer: empty
layer 2 (read-only)
layer 1 (read-only)

Same 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.

3. A container lives exactly as long as its main process. 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
4. Stopping keeps the writable layer. Removing deletes it. 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
5. Don't patch a running container. Build a new image and replace it. Kubernetes says it directly: containers "are intended to be stateless and immutable." To make a change, "build a new image that includes the change, then recreate the container." Kubernetes Docs: Containers
Kubernetes will… restart a crashed container with a clean state: "all of the files that were created or modified during the lifetime of the container are lost" (Kubernetes Docs: Volumes). That is harsher than 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
  1. docker run alpine:3.20 echo hello, then docker ps and docker ps -a.
  2. sleep 3600 keeps a process alive for an hour. You need -d and --name box.
  3. docker exec box sh -c 'echo "my data" > /note.txt'. Quote the whole command so the > happens inside the container.
  4. docker run --rm alpine:3.20 cat /note.txt
  5. Stop it, start it, then docker exec box cat /note.txt.
  6. docker rmi refuses while any container, even a stopped one, still uses the image. docker ps -a shows 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.

Related ideas