How to automatically restart a Docker container when it stops responding

Last update: 02/07/2026

  • Using Docker's native reboot policies to handle system failures.
  • Implementation of custom scripts and cron for containers with unhealthy states.
  • Advanced integration with systemd to control the startup and shutdown sequence.
  • External monitoring alternatives and tools such as Podman Quadlet.
How to automatically restart a Docker container when it stops responding

I'm sure this has happened to you: you have a service running in Docker which, for mysterious reasons, gets stuck every few days. How to automatically restart a Docker container when it stops responding? It's frustrating to try to optimize code or configurations and have nothing work, only to realize that a simple manual reboot solves the problem and keeps everything running for another week. Instead of constantly checking the time, the ideal solution is to set up a system that does this dirty work for you.

Managing container availability involves more than just ensuring they start automatically when the server is powered on. ensure that they respond correctly to the requests. Sometimes the container appears to be "alive" to Docker, but the application inside is unresponsive, forcing us to look for solutions ranging from basic daemon parameters to the creation of complex services in the operating system.

Docker native reboot policies

The simplest way to automatically restart a Docker container is by using the flag --restart when launching a container or using the command docker update If the container is already created, Docker offers several options depending on your needs. For example, the policy always This will cause the container to always restart, regardless of whether the process ended with an error or if it was the Docker daemon itself that restarted.

Exclusive content - Click Here  How can I translate a text in Google Translate?

If you prefer something more surgical, unless-stopped This is the top choice for many. This configuration is identical to the previous one, except that if you manually stop the container for maintenance, Docker will not attempt to turn it on. automatically after a server restart, respecting your manual decision.

For those who only want to act in response to critical failures, there is on-failureThis policy only triggers a restart if the container terminates with a non-zero exit code. You can even limit this by adding a maximum number of retries, preventing the system from entering an infinite restart loop if the error is persistent and critical.

How to automatically restart a Docker container when it stops responding
How to automatically restart a Docker container when it stops responding

Automation for "Unhealthy" containers

This is where things get interesting. Sometimes, a container still shows as "Up" (on), but its health status is unhealthy because it fails internal checks. Previous policies are ineffective here, as the process is not technically dead. To resolve this, we can implement the directive HEALTHCHECK in the Dockerfile or in the Docker Compose file, defining a command (such as a curl to a specific port) to verify that the app responds.

Once we have the health check set up, we can create a Bash script that runs every few minutes using the crontab from Linux. This script should filter out containers that are in a state unhealthy or those who have exited with error code 128 and run a docker restart about them.

Exclusive content - Click Here  How to install a printer without a CD

For this to work smoothly and to automatically restart a Docker container, it is essential that the user running the cron job has the necessary permissions. permissions required in the docker groupIn this way, the system monitors the environment every 5 or 10 minutes and brings up any service that has become "zombie", guaranteeing almost total availability without human intervention.

docker on windows 10
Related article:
How to install Docker on Windows 10

Advanced integration with systemd

When working in more professional environments, you might want your containers to behave like native Linux servicesIntegrate Docker Systemd allows you to manage complex dependencies, such as ensuring that the database starts before the web application. This is achieved by creating files .service in the route /etc/systemd/system/.

We can design a power off and power on sequence Controlled. Using custom scripts linked to systemd, it's possible to stop containers in reverse order of their dependencies to prevent data corruption. Using the type oneshot and configuring the targets suitable (such as multi-user.target o shutdown.target), the server handles the entire lifecycle of the container.

Another powerful option to automatically restart a Docker container when it stops responding is to use the flag --rm in combination with systemd. This allows the container to be removed upon stopping and a new one created from the base image upon startup, ensuring that the The container's condition must always be clean and unchanged.delegating data persistence solely to external volumes.

Exclusive content - Click Here  I can't uninstall a program
Use Docker Desktop with WSL
How to automatically restart a Docker container when it stops responding

External tools and modern alternatives

If you don't want to bother writing Bash scripts, there are third-party tools available. There are open-source applications specifically designed to restart containers based on a established scheduleThis is very useful for recurring tasks such as renewing SSL certificates. Likewise, tools such as Uptime Kuma They allow you to monitor the health of services from a visual dashboard and send notifications to Telegram or Gotify if something goes wrong. They are all very helpful for automatically restarting a Docker container when it stops responding.

For those looking for a lighter or native alternative, Podman It's a superb option. Thanks to its daemonless architecture and functionality of QuadletIt allows you to create simple declarative files that systemd automatically converts into optimized services, offering a much more organic integration than traditional Docker.

Having complete control over the lifecycle of your services involves combining the use of restart policies, constant surveillance through healthchecks and, in more complex cases, the support of systemd or monitoring tools so that your infrastructure is robust and doesn't depend on someone remembering to manually restart the server every week.