Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Docker Containers
23%

Starting Containers Automatically

If you are running a container that needs to always be available, you can configure it to restart automatically under certain conditions using Docker's restart policies. By default, the container will not restart automatically when it stops. This behavior can be modified.

You can configure Docker to automatically restart containers under specific conditions. These restart policies define how Docker reacts when a container stops, exits with an error, or when the Docker daemon itself restarts. You can also disable automatic restarts entirely.

Docker provides a small set of restart policies that cover these scenarios. The following table, adapted from the official documentation, summarizes the available options:

FlagDescription
noDo not automatically restart the container. This is the default behavior.
on-failure[:max-retries]Restart the container only if it exits with a non-zero exit code. You can optionally specify a maximum number of restart attempts using :max-retries. This policy doesn't restart the container if the Docker daemon restarts.
alwaysAlways restart the container when it stops, regardless of the exit code. If the container is manually stopped, it will be restarted automatically only when the Docker daemon restarts or when the container is manually started again.
unless-stoppedSimilar to always, but if the container is manually stopped, it will not be restarted even after the Docker daemon restarts.

These policies control container restarts at the Docker Engine level and are evaluated independently for each container. They're useful for basic resilience on a single host, but they do not replace full orchestration features such as health checks, rescheduling, or distributed failover.

You can specify a restart policy when creating or running a container using the --restart

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More