Docker
container management
auto-restart
configuration
DevOps

How to disable auto-restart on a Docker container

Codemia Knowledge Hub ·

Run docker update --restart no <container>. It changes the restart policy of an existing container, running or stopped, without stopping and recreating it. For new containers you can pass --restart no to docker run, though that is already the default when no restart policy is given.

The four restart policies

Restart policies control what happens when a container exits. You set them with --restart on docker run and change them later with docker update.

bash
1# No automatic restart (default)
2docker run --restart no nginx
3
4# Always restart, regardless of exit code
5docker run --restart always nginx
6
7# Always restart, except when manually stopped
8docker run --restart unless-stopped nginx
9
10# Restart only on non-zero exit code, with optional retry limit
11docker run --restart on-failure:5 nginx
PolicyRestarts on crashRestarts on docker stopRestarts on daemon restartDefault
noNoNoNoYes
alwaysYesNo (but resumes after daemon restart)YesNo
unless-stoppedYesNoOnly if not manually stoppedNo
on-failure[:N]Yes (if exit code is non-zero)NoNoNo

The difference between always and unless-stopped shows up when the Docker daemon restarts, for example after a reboot. A container with always comes back even if you ran docker stop before the reboot. A container with unless-stopped stays stopped if you stopped it manually. People often pick always thinking it means "keep running until I stop it"; if manual stops should survive reboots, use unless-stopped.

Changing the policy on an existing container

docker update changes a container's configuration in place, which has been possible since Docker 1.11. Older tutorials that tell you to stop, remove and recreate the container predate it.

bash
docker update --restart no my-container

Check that the change took effect:

bash
docker inspect --format '{{.HostConfig.RestartPolicy.Name}}' my-container

Expected output:

text
no

This works on both running and stopped containers.

Several containers at once

Pass multiple names or IDs:

bash
docker update --restart no container1 container2 container3

Every running container:

bash
docker update --restart no $(docker ps -q)

All containers, including stopped ones:

bash
docker update --restart no $(docker ps -aq)

New containers with docker run

Without --restart, docker run uses no. Stating it explicitly is unnecessary but can make scripts and documentation clearer:

bash
docker run --restart no -d -p 8080:80 nginx

Docker Compose: quote "no"

In Compose files the policy is the restart key. Leave it out (the default is no) or set it explicitly:

yaml
1services:
2  web:
3    image: nginx
4    restart: "no"
5    ports:
6      - "8080:80"

The quotes matter. Unquoted no is parsed by YAML as boolean false, which is not a valid restart policy and is a common source of parse errors and unexpected behavior.

To change the policy of a running Compose service without recreating it:

bash
docker update --restart no $(docker compose ps -q web)

Or recreate the service with the updated file:

bash
# After changing restart: "no" in docker-compose.yml
docker compose up -d web

Checking the current policy

bash
1# Single container
2docker inspect --format '{{.HostConfig.RestartPolicy.Name}}' my-container
3
4# All containers with their restart policies
5docker ps -a --format 'table {{.Names}}\t{{.Status}}' | head -5
6docker inspect --format '{{.Name}} {{.HostConfig.RestartPolicy.Name}}' $(docker ps -aq)

For a cleaner overview:

bash
docker inspect --format '{{.Name}}: restart={{.HostConfig.RestartPolicy.Name}}, max_retries={{.HostConfig.RestartPolicy.MaximumRetryCount}}' $(docker ps -aq)

on-failure with a retry limit

on-failure takes an optional maximum retry count. Once the limit is reached, Docker stops restarting the container. It sits between always, which can cause restart loops, and no, which offers no resilience.

bash
1# Restart up to 3 times on failure
2docker run --restart on-failure:3 -d my-app
3
4# Check how many restarts have occurred
5docker inspect --format '{{.RestartCount}}' my-app

A container that hits its limit stays down silently. If you are working out why a container is not running, check its restart count and exit code with docker inspect. Fix the underlying problem, then restart it manually. The counter resets if you docker stop and docker start the container.

Swarm and Kubernetes manage restarts themselves

Docker restart policies apply to standalone containers only.

Docker Swarm uses service-level restart policies from the deploy section of a Compose file. docker update --restart does not apply to Swarm services; update the service instead:

bash
docker service update --restart-condition none my-service

Kubernetes uses the Pod restartPolicy field, which accepts Always, OnFailure or Never. Docker-level restart policies are ignored for containers running in Kubernetes pods.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design