study.

Run and Inspect Your First Container · Docker

A stopped container does not mean its image disappeared.

← Docker

Learn with progress and quizzes

First, understand the concepts.

Images and containers

An image supplies files and default settings. Multiple containers can use one image, each with its own writable layer.

Main process lifetime

A container follows its main process. A short command such as echo can exit successfully, so Exited alone is not evidence of failure.

Inspect before cleanup

ps shows running containers; ps -a also shows stopped ones. Read logs and the exit code before removing the specific container.

Why it matters

One-shot commands and long-running servers have different expected lifetimes. An echo container exiting immediately is not equivalent to a server crashing.

Work through the reasoning

Follow the lifecycle

run creates and starts a container. start restarts an existing one; it does not rebuild an image. Compare IDs with ps -a.

Connect output with state

logs shows standard output and error. Pair it with inspect Status and ExitCode to explain why printing a message and exiting with zero represents success.

Learn by doing.

Use Docker Engine or Docker Desktop with Linux containers, Compose v2, a POSIX shell, and curl on a personal practice host. Clean up only the study-prefixed resources you create. Pulling images requires network access.

Run a BusyBox container that prints a message and inspect its exit. If the name already exists, review and clean up the previous practice container first.

docker version
docker run --name study-lifecycle busybox:1.36 echo hello-study
docker ps -a --filter name=study-lifecycle
docker logs study-lifecycle
docker inspect --format '{{.State.Status}} {{.State.ExitCode}}' study-lifecycle

How to check your result

Expect hello-study in the logs and exited 0 from inspect. After cleanup, explain why docker image ls busybox still lists the image. If the daemon cannot be reached, check Docker and its current context.

Clean up practice resources

Run after checking the result. Confirm these resources belong to your practice run and contain no data you need to keep.

docker rm study-lifecycle

Apply what you learned

If you use start -a on the exited hello-study container, does it receive a new ID?

Reveal the reasoning after trying

No: the existing container runs again with the same ID. Distinguish this from run creating another container. Its writable layer also remains; a new container gives a clearer fresh environment.

Ready to check your understanding?

Which exit code normally indicates successful completion?

  1. 0
  2. 1
  3. Always 137
  4. Always -1
Reveal the reasoning after trying

0

Zero indicates successful completion. Exited alone is not a failure; inspect the logs and exit code together.

Official documentation · Run and Inspect Your First Container