Docker, start to ship · Chapter 2 of 26

The container graveyard

By the end of this chapter you can move a container through its life (created, running, exited, removed), read its exit code, and stop or remove one that will not go quietly. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Containers
  • 5 tasks
  • Docker
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

Why you need this

Ravi runs docker ps -a on the lab machine and finds containers he has never heard of, all with Bunty's name on them. One is still running a sleep. Ravi's own menu server, chai-02-web, needs a restart, and it must stay the same container, with the same ID, because his notes and logs point at it.

So far you know docker run and docker rm. Run is not enough here: a new chai-02-web would be a different container, and docker rm refuses the one that is still running. You need the steps in between.

The lesson

Created, running, exited

docker run is two steps in one: create a container from an image, then start it. You can do them one at a time:

$ docker create --name chai-02-menu nginx
56bbfc79e238b3313a2a1b3d59522490eec8301253b6ced814c8af2a3c35e774
$ docker ps -a --filter name=chai-02-menu
CONTAINER ID  IMAGE                           COMMAND               CREATED                 STATUS      PORTS       NAMES
56bbfc79e238  docker.io/library/nginx:latest  nginx -g daemon o...  Less than a second ago  Created                 chai-02-menu
$ docker start chai-02-menu
chai-02-menu
$ docker ps --filter name=chai-02-menu
CONTAINER ID  IMAGE                           COMMAND               CREATED                 STATUS                     PORTS       NAMES
56bbfc79e238  docker.io/library/nginx:latest  nginx -g daemon o...  Less than a second ago  Up Less than a second ago              chai-02-menu

A created container has its settings and its own files, but no process. docker stop stops a running one, docker start starts it again, and docker restart does both. It stays the same container: same ID, same files. docker inspect shows it, the ID first and then when it last started:

$ docker inspect -f "{{.Id}} {{.State.StartedAt}}" chai-02-web
e6658a2efb3969c0629a4a49e07950f9449e238c06e9f666fa08b970546a7503 2026-10-06 22:50:11.926136966 +0000 UTC
$ docker restart chai-02-web
chai-02-web
$ docker inspect -f "{{.Id}} {{.State.StartedAt}}" chai-02-web
e6658a2efb3969c0629a4a49e07950f9449e238c06e9f666fa08b970546a7503 2026-10-06 22:50:12.714872341 +0000 UTC

The ID did not change; the start time moved on. Your IDs and times will differ.

Exit codes

When the main process ends, the container is exited, and Docker keeps the process's exit code: 0 means it went fine, anything else means trouble. A shell can end with any code you like:

$ docker run --name chai-02-three alpine sh -c "exit 3"
$ docker inspect -f "{{.State.Status}} {{.State.ExitCode}}" chai-02-three
exited 3
$ docker ps -a --filter name=chai-02-three
CONTAINER ID  IMAGE                            COMMAND       CREATED                 STATUS                             PORTS       NAMES
3c9386bd7c74  docker.io/library/alpine:latest  sh -c exit 3  Less than a second ago  Exited (3) Less than a second ago              chai-02-three

Remember 137: 128 plus 9, the process was killed by signal 9.

Stop or kill

Here is a sleep like Bunty's, stopped and then killed, with time in front to see how long each took:

$ docker run -d --name chai-02-nap alpine sleep 3600
8080770b20c1f072dcaf1fabf20de59e6119acf5404280919ed46587fbe28711
$ time docker stop chai-02-nap
WARN[0010] StopSignal SIGTERM failed to stop container chai-02-nap in 10 seconds, resorting to SIGKILL
chai-02-nap

real	0m10.144s
user	0m0.001s
sys	0m0.003s
$ docker start chai-02-nap
chai-02-nap
$ time docker kill chai-02-nap
chai-02-nap

real	0m0.052s
user	0m0.001s
sys	0m0.002s
$ docker inspect -f "{{.State.ExitCode}}" chai-02-nap
137

Read the warning: it names the signal docker stop sent first, and the one it used after ten seconds. A sleep running as the container's first process ignores the first one, so the full ten seconds pass. An app that handles it gets the chance to finish what it was doing. docker kill took no time at all.

Removing

docker rm removes containers that are not running, several at once if you give it several names. It refuses a running one:

$ docker rm chai-02-bunty-stuck
Error: cannot remove container ed941e1c0b816303698e4a217d93e8e533450351b536f2714ca0cdde055308db as it is running - running or paused containers cannot be removed without force: container state improper
$ time docker rm -f chai-02-bunty-stuck
chai-02-bunty-stuck

real	0m0.138s
user	0m0.002s
sys	0m0.002s

-f (force) removes it while it runs, at once. docker container prune -f removes every stopped container in one go: quick, and a mistake when one of them is a container you meant to keep.

Your turn

Your five tasks are at the top, and the terminal prints them too (lab tasks shows them again). Ravi's container must be the same one afterwards, so no new chai-02-web. Task 4 leaves a stopped container behind on purpose, so do not prune after it. The interview question asks how stop and kill differ: the output above has everything you need. lab reset starts the chapter over.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Create chai-02-menu, then start it

    Make a container and start it in two separate steps.

    1. Make a container named chai-02-menu from the nginx image with docker create. Do not use docker run.

    2. Run docker ps -a. It shows chai-02-menu as Created.

    3. Start it with docker start.

    To pass: you ran docker create for chai-02-menu in the chapter folder, and chai-02-menu runs nginx now. A command with a typing mistake or an error does not count, so type it again.

  2. Task 2

    Restart Ravi's chai-02-web

    Ravi's notes use the ID of chai-02-web, so restart this same container.

    1. Restart chai-02-web with docker restart (or docker stop, then docker start).

    2. Do not delete it. A new container with the same name gets a new ID.

    3. Run docker ps to see it running again.

    To pass: chai-02-web is the same container as before (the same ID), it was started again after the chapter began, and it is running.

  3. Task 3

    Delete Bunty's three containers

    Bunty left three containers behind. Delete all three.

    1. Run docker ps -a. Find every container whose name starts with chai-02-bunty. One of them is still running.

    2. Delete all three with docker rm. For the running one, stop it first or add -f.

    3. Do not delete chai-02-web.

    To pass: no container with a name starting with chai-02-bunty is left, and chai-02-web is still there.

  4. Task 4

    Leave behind chai-02-seven, exited with code 7

    Make a container that ends with exit code 7, and keep it.

    1. Run a container named chai-02-seven from the alpine image.

    2. Give it a command that ends with exit code 7. A shell can end with any exit code you choose.

    3. Do not add --rm, and do not delete it later.

    To pass: chai-02-seven is there, made from alpine, finished (Exited), with exit code 7. docker container prune deletes it too, so do not use prune.

  5. Task 5 · interview question

    Interview: docker stop or docker kill

    Interview question: how is docker stop different from docker kill?

    1. Say what docker stop sends first, and why.

    2. Say what docker kill does instead.

    3. Save your answer (one or two sentences) in answer.txt, in the chapter folder.

    To pass: answer.txt says both things: what stop does first, and what kill does.

What you will learn

  • docker create and docker start: the two halves of docker run.
  • docker stop, docker restart and docker kill: the same container, stopped or started again.
  • Exit codes: the number a container leaves behind when it ends.
  • docker rm -f and prune: removing a running container, and removing too much.

More in Containers

Build The container graveyard yourself

Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.