Docker · Containers in practice
Describe the container lifecycle and restart policies.
Short answer
A container moves through created, running and exited states, with paused and dead as special cases. Restart policies decide whether Docker restarts it after exit, and stop sends SIGTERM before SIGKILL.
docker create builds the container without starting it. docker start runs the process, and the container is running until the main process exits. docker stop sends SIGTERM and, after a grace period, SIGKILL. docker rm removes the container and its writable layer.
docker run -d --restart unless-stopped --name api api:1.0.0
docker stop -t 30 api
docker ps -a
docker inspect -f "{{.State.ExitCode}}" apiRestart policies
- no: never restart, the default
- on-failure: restart only on a non-zero exit, optionally with a limit
- always: restart regardless of exit status
- unless-stopped: like always, but respects a manual stop across daemon restarts
Restart policies are not a substitute for orchestration or health checks. A process that starts but is unhealthy will keep running, and a crash loop can hide a configuration error. Check the exit code and logs before assuming a restart is enough.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What signal does docker stop send first?
- 2What does exit code 137 usually indicate?
- 3How is unless-stopped different from always?
- 4Why can a restart policy mask a broken deployment?
Say it in the interview
A container goes from created to running to exited, and the main process controls that transition. I set a restart policy such as unless-stopped for a long-running service, but I treat repeated restarts as a signal to inspect logs and exit codes rather than a fix. I also make sure the app handles SIGTERM and finishes in-flight work within the stop grace period.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Docker mock interview and get scored feedback on depth, clarity and confidence.