docker - how do you disable auto-restart on a container?

Docker

Docker Problem Overview


I can enable auto-restart with --restart=always, but after I stop the container, how do I turn off that attribute?

I normally run a webserver and typically map port 80:

docker run -d --restart=always -p 80:80 -i -t myuser/myproj /bin/bash

But there are times when I want to run a newer version of my image, but I want to keep the old container around. The problem is that if there are multiple containers with --restart=always, only one of them (random?) starts because they're all contending for port 80 on the host.

Docker Solutions


Solution 1 - Docker

You can use the --restart=unless-stopped option, as @Shibashis mentioned, or update the restart policy (this requires docker 1.11 or newer);

See the documentation for docker update and Docker restart policies.

docker update --restart=no my-container

that updates the restart-policy for an existing container (my-container)

Solution 2 - Docker

Use the below to disable ALL auto-restarting (daemon) containers.

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

Use the following to disable restart a SINGLE container.

docker update --restart=no the-container-you-want-to-disable-restart

Rational:

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. This is often very useful when Docker is running a key service.

Notes

If you are using docker-compose this might be useful to know.

> restart no is the default restart policy, and it does not restart a > container under any circumstance. When always is specified, the > container always restarts. The on-failure policy restarts a container > if the exit code indicates an on-failure error.

restart: "no"
restart: always
restart: on-failure
restart: unless-stopped

restart: always

Solution 3 - Docker

You can start your container with --restart=unless-stopped.

Solution 4 - Docker

If you have a swarm restarting the containers, the swarm will restart any containers you stop or rm, irrespective of the restart option. That's a feature, not a bug.

Make sure you are not running a service you forgot about:

docker service ls

Then, you can stop the service

docker service rm <service id discovered with previous command>

Solution 5 - Docker

Not a response to this question but to How to prevent docker from starting a container automatically on system startup?, which has been marked as a duplicate of this question.

If your container is started with restart=on-failure and has a faulty command that exits with a non-zero exit code when you stop the container with docker stop, it shows some weird behaviour: After stopping the container with docker stop, the container is stopped, but after restarting the docker daemon (or the system), it is started automatically again. To fix this, either fix the command of the container or use no or unless-stopped as the restart policy.

Solution 6 - Docker

docker update --restart=yes/no

Solution 7 - Docker

Update only actively running containers

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

Solution 8 - Docker

To change the restart policy of all docker containers...

Identify the docker containers that will start on boot

This shell script will identify any docker containers that have a restart policy other than "no".

As the root user
CONTAINERS=$(for f in  /var/lib/docker/containers/*/hostconfig.json ; 
do 
container=`echo $f | rev | cut -d '/' -f 2| rev`
jq \
 --arg container "$container" \
 --arg file "$f" '{"RestartPolicy":.RestartPolicy.Name, 'file':$file, 'container':$container} | select(.RestartPolicy != "no")' "$f" | \
  jq .container -r | tr '\n' ' '
done)

or as NON-root...
CONTAINERS=$(for f in  $(sudo sh -c "ls /var/lib/docker/containers/*/hostconfig.json"); 
do 
container=`echo $f | rev | cut -d '/' -f 2| rev`
sudo jq \
  --arg container "$container" \
  --arg file "$f" '{"RestartPolicy":.RestartPolicy.Name, 'file':$file, 'container':$container} | select(.RestartPolicy != "no")' "$f" | \
  jq .container -r | tr '\n' ' '
done)
(optionally) View the list of selected containers
echo $CONTAINERS
Sset all of the containers to "no" at once.
docker update --restart=no $CONTAINERS

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrad GrissomView Question on Stackoverflow
Solution 1 - DockerthaJeztahView Answer on Stackoverflow
Solution 2 - DockerFlyingVView Answer on Stackoverflow
Solution 3 - DockerShibashisView Answer on Stackoverflow
Solution 4 - DockernachbarView Answer on Stackoverflow
Solution 5 - DockercdauthView Answer on Stackoverflow
Solution 6 - DockerSaurav SolankiView Answer on Stackoverflow
Solution 7 - DockerMusab DoğanView Answer on Stackoverflow
Solution 8 - DockerJoe JView Answer on Stackoverflow