Challenge,Β Medium, Β onΒ  Containers

The docker run command is a handy way to quickly spin up a container, but many real-world use cases require more granular control over the container lifecycle. Understanding the key states and operations on containers is essential for working with Docker (and other container runtimes like containerd or Kubernetes) efficiently.

In this challenge, you'll walk through the full lifecycle of a single container using granular commands and observing how its status, PID, and the root filesystem change throughout the state transitions.

Container lifecycle: create, start, pause, unpause, stop, restart, kill, and remove a container.

First things first - create an Nginx container named nginx-1 using the docker.io/library/nginx image (but don't start it just yet):

What is the container's status immediately after creation?

Hint: Inspecting container details πŸ’‘

If you need a refresher on how to inspect the container details, try solving this challenge.

Can you locate the container's filesystem (rootfs) on the host?

Hint: How to find the container rootfs πŸ’‘

When you create (or run) a new container, Docker extracts its future root filesystem from a container image, and stores it on the host as a regular folder, potentially uncompressing and "squashing" the image layers on the way.

To do so, Docker uses a storage driver, a.k.a. graph driver. Can you spot the graph driver-related data in the docker inspect output? Assuming the overlay2 storage driver is used, the final rootfs layer would typically be called MergedDir.

For more information on how container filesystem is created, see How Container Filesystem Works: Building a Docker-like Container From Scratch.

Start the just-created container so that it begins running:

What is the container's status now?

What is the PID of the container's topmost process (when seen from the host)?

Hint: How to find the container PID πŸ’‘

If you need a refresher on how to find the container PID, try solving this challenge.

Now, pause the running container:

Hint: How to pause a container πŸ’‘

If you need a refresher on how to pause a container, try solving this challenge.

What is the container's status while it is paused?

Unpause the container to resume execution:

What is the container's status after unpause?

Stop the container gracefully:

Hint: Pausing vs stopping πŸ’‘

When you pause a container, all container's processes get suspended, but you can still find them in the host's process list, and the container is technically considered to be still running.

However, when you stop a container, all container's processes get terminated, but its filesystem and metadata remain intact, so you can restart it again later.

If you need a refresher on how to stop a container, try solving this challenge.

What is the container's status after a graceful stop?

Restart the same nginx-1 container so that it runs again:

Hint: Restart vs. Start πŸ’‘

The docker restart command is a handy shortcut for docker stop (optional) followed by docker start.

Hypothetically, you can start a created container using docker restart (but using docker start would look more natural). And you can also restart an exited container using plain docker start.

In other words, the docker restart command is more of a syntactic sugar than a separate capability.

What is the container's status after restart?

What is the PID of the container's topmost process now? Did it change from the first time you started the container?

Where is the container filesystem stored now? Did the location change from the first time you started the container?

Kill the running container using SIGKILL to immediately terminate it:

Hint: How to signal a container πŸ’‘

If you need a refresher on how to signal a container, try solving this challenge.

What is the container's status after being forcefully terminated?

Remove the container to clean things up:

Can you find the container's filesystem on the host after it was removed? πŸ€”

Level up your Server Side game β€” Join 13,000 engineers who receive insightful learning materials straight to their inbox