Stopping Unwanted Docker Restarts

In infraSeptember 22, 20232 min read

If you've been using Docker Desktop on Mac, you may have encountered a frustrating issue where Docker containers seem to restart themselves even after you've explicitly stopped them. This behavior can be disorienting, especially when you're managing multiple container instances for development or testing.

Why Do My Docker Containers Keep Restarting?

Docker employs auto-restart policies to ensure that containerized services maintain high availability and fault tolerance. By default, containers may or may not restart automatically based on their configuration or the policy set during their creation. Here's a quick overview of common restart policies:

  • no: Do not automatically restart the container when it exits.
  • always: Always restart the container, irrespective of the exit status.
  • unless-stopped: Restart the container unless it has been manually stopped.
  • on-failure: Restart the container only if it exits with a non-zero exit status.

These policies serve different purposes:

  • High Availability: Ensures that essential services are always up and running.
  • Fault Tolerance: Facilitates automatic recovery from transient errors or failures.
  • Scheduled Restarts: Helps in situations where a container might need regular restarts, perhaps due to memory leaks or other issues.

How to fix it?

If you find yourself in a situation where you need to disable the auto-restart feature for all containers, you can use the following command:

docker update --restart=no $(docker ps -a -q)

Here's what it does:

  • docker update --restart=no: This updates the configuration of a container to set its restart policy to "no," meaning the container won't restart automatically.
  • $(docker ps -a -q): This is a shell command substitution that fetches the IDs of all containers, both running and stopped. By running this command, you're setting the restart policy of all containers on your system to "no," thereby preventing them from restarting automatically.