Docker stop, start and restart
Quick answer
docker stop requests shutdown of a running container. docker start launches an existing stopped or created container. docker restart restarts the process in the same container object. These operations do not create a new image or replace the writable layer.
Before you run commands
Use an approved disposable Linux-container Docker lab. Confirm the effective context and permissions; use a reviewed, locally preloaded busybox:1.37 image whose digest/platform the lab owner has recorded. This tag is mutable. Do not download an image automatically; --pull=never requires it locally. The new containers use --network none, with no host mounts or published ports; the CLI still connects to the selected engine. Verify the approved image tag still resolves to the recorded image before creating containers. Check docker ps -a for all names used below. If any is occupied, choose fresh names consistently. Run commands one at a time and stop on any unexpected failure; never act on resources you did not create. Expected results are predictions, not measured execution.
How it works
On Linux, stop normally sends SIGTERM (a request to terminate) unless a different stop signal is configured. If the program does not exit in the grace period, Docker can terminate it with SIGKILL. An app should handle shutdown cleanly. The example uses --init to place a small init process in the container that forwards signals and reaps child processes. A 120-second sleep bounds the job even if you forget to stop it. Run the steps promptly; if the job already finished, start it again before observing a running state. If the sleep has already ended before the stop demonstration, record that observation and start the same owned container again before repeating the observation; do not treat a timing-dependent state as a guaranteed result.
docker run -d --init --pull=never --network none --name bm02-cycle busybox:1.37 sleep 120docker inspect --format '{{.Id}} {{.State.Status}}' bm02-cycledocker stop -t 2 bm02-cycledocker inspect --format '{{.Id}} {{.State.Status}}' bm02-cycledocker start bm02-cycledocker restart -t 2 bm02-cycledocker inspect --format '{{.Id}} {{.State.Status}}' bm02-cycledocker stop -t 2 bm02-cycledocker rm bm02-cycleExpect the same ID in each inspection: running, exited, then running if observed promptly. The timeout of two seconds is only for this disposable sleep job.
Common mistake
Restart is not a fresh deployment: it reuses the existing configuration and writable layer. Avoid repeatedly restarting a failing service without checking its logs and cause. Never use a two-second shutdown timeout as a general production recommendation.
Try it yourself
Explain why the ID remains unchanged. Predict whether a file written in the container’s writable layer survives stop/start if there are no external mounts and the container is NOT removed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker stop, start and restart — knowledge check
Apply the lifecycle concepts and interpret the lab behavior.
Which command starts an existing exited container?
What usually happens if a process ignores its configured stop signal?
What stays the same after restart?