Docker Add a restart policy to a container that was already created
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Docker is an open-source platform that allows developers to automate the deployment of applications within lightweight, portable containers. One of Docker's key features is its ability to ensure that applications run smoothly across different computing environments. A significant aspect of managing Docker containers in a production environment is ensuring their resilience and ability to recover from unexpected failures. This is where Docker's restart policies come into play.
Understanding Docker Restart Policies
Docker restart policies define how containers should be treated when they exit. These policies ensure that a container can be automatically restarted if it fails during execution. The default behavior of a Docker container is to not restart unless explicitly defined otherwise. The primary restart policies available in Docker are:
no: By default, a container will not be automatically restarted when it exits.always: The container will always be restarted, regardless of the exit status.unless-stopped: The container will be restarted unless it is explicitly stopped by the user.on-failure[:max-retries]: The container will restart only if it exits with a non-zero status. An optional maximum retry count can be specified; for example,on-failure:5.
Adding a Restart Policy to an Existing Container
If you have already created a container without a restart policy, you can still apply a policy to it. This can be achieved using the docker update command, which modifies configuration options on containers that are already running.
Example: Updating a Container with a Restart Policy
Suppose we have an existing container named webapp that currently does not have a restart policy applied. We want to update the container to use the on-failure restart policy with a maximum of 5 retries. Here's how you can accomplish this:
This command modifies the webapp container, ensuring that Docker will attempt to restart it up to five times if it exits with a non-zero status.
Technical Explanation
When you use the docker update command to set a restart policy, Docker modifies the container's metadata but does not affect the running instance immediately. The policy will take effect after the next time the container is stopped and started again.
Workflow of Applying a Restart Policy
- Check Current Policy: Before applying a new policy, it's useful to know the current policy. You can inspect the container's settings:
- Update the Policy: Use the
docker updatecommand to apply the desired restart policy. - Verify Update: After updating, you can verify that the change has taken effect by inspecting the container again.
- Apply Changes by Cycling the Container: Restart the container to ensure the policy is enacted:
Docker Restart Policy Summary
| Restart Policy | Description | Suitable Use Case |
| no | Do not automatically restart the container. | Development environments. |
| always | Always restart the container unless explicitly stopped. | Critical production services. |
| unless-stopped | Restart unless the user has stopped it manually. | Production services requiring manual intervention for shutdown. |
| on-failure[:max-retries] | Restart only on failure with optional max retries. | Non-critical services, experimentation with unstable apps. |
Additional Considerations
Resource Management
Restarting containers continuously can strain system resources. It's wise to pair restart policies with resource management features like memory and CPU limits to prevent runaway containers from hogging resources and impacting other containers.
Logs and Monitoring
For containers using restart policies, it's important to have robust logging and monitoring in place. Tools such as docker logs, Prometheus, or Grafana can provide insights into container performance and failures, ensuring proactive management of restarts.
Dependency Management
For applications comprising multiple dependent services (e.g., microservices architectures), consider orchestrating container restarts with tools like Docker Compose or Kubernetes to maintain service integrity during restarts.
By thoughtfully applying restart policies, developers and DevOps teams can enhance the fault tolerance and resilience of their applications, ensuring smoother operation in both development and production environments. Docker's flexibility in managing container lifecycles is a powerful feature in automated deployment strategies.

