Amblem
Furkan Baytekin

How Container Restart Policies Actually Work

Docker container restart policies explained

How Container Restart Policies Actually Work
138
4 minutes

Container restart policies look simple on the surface. You set one flag and expect Docker or your orchestrator to “keep things running”.

In reality, restart behavior is rule-based, event-driven, and often misunderstood. Many production bugs come from assuming containers restart when they should, not when they’re allowed to.

Let’s clear this up.


What Is a Container Restart Policy?

A restart policy defines when a container should be restarted after it stops.

Key point:

Restart policies react to container exit events, not application health.

If your process is alive but broken, the policy does nothing.


The Core Restart Policies (Docker)

no (default)

Used for:


always

Catches:

This is why docker stop + always feels “haunted”.


unless-stopped

After a daemon reboot:

This is usually what people actually want.


on-failure[:max-retries]

Does NOT restart on:

Perfect for:


What Actually Triggers a Restart?

A restart happens only when the main process exits.

Common triggers:

Not triggers:

If PID 1 is alive, the container is “healthy”.


Exit Codes Matter More Than You Think

Restart logic is driven by exit codes.

Examples:

If your app exits with 0 on fatal errors, you silently disable restarts.


Restart Policies vs Health Checks

These are not the same thing.

A container can be:

Unless your orchestrator wires health checks to restarts, nothing happens.

Docker alone will not restart an unhealthy container.


The PID 1 Problem

PID 1 behaves differently in containers.

Issues:

Result:

Solution:


Why Crash Loops Happen

Docker applies a restart backoff:

So even with always, Docker won’t hammer your machine endlessly.

Still:

Restart policies mask failures — they don’t fix them.


Common Misconceptions

“Restart policy = self-healing”

Nope. It only handles process death, not broken logic.

“Health check failure restarts the container”

Not in plain Docker.

“Always is safest”

Usually wrong. unless-stopped is safer for humans.

“OOM won’t trigger a restart”

It will. OOM is just another crash.


Production Best Practices


The Mental Model

If this sentence sticks, you’re good:

Containers restart when PID 1 exits, not when your app misbehaves.

Everything else follows from that.


Album of the blog:

Suggested Blog Posts