🐳 Docker Beginner

What are restart policies in Docker?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Docker basics — a prerequisite for any developer role.

Answer

Docker restart policies control whether and how Docker automatically restarts a container when it exits. Set with --restart flag in docker run. Options: no (default): never automatically restart. Container stays stopped when it exits; on-failure[:max-retries]: restart only if the container exits with a non-zero exit code (indicating an error). Optionally limit retries: --restart on-failure:5 — retry at most 5 times with exponential backoff between retries; always: always restart the container regardless of exit code. Also restarts when the Docker daemon starts (after host reboot) — even if you manually stopped it; unless-stopped: like always but does NOT restart if you manually stopped the container with docker stop. Recommended for most production services — auto-starts after host reboot, but respects your manual stops. In Docker Compose: restart: unless-stopped. Inspect current policy: docker inspect --format "{{.HostConfig.RestartPolicy.Name}}" mycontainer. Change restart policy on running container: docker update --restart unless-stopped mycontainer. In Kubernetes, restart behavior is handled by the Pod restart policy and controllers (Deployment, DaemonSet) rather than Docker-level settings.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.