63

Is there any way to stop a docker container which started with --restart=always like following

sudo docker run -it --restart=always <image_id>

Rafaf Tahsin
  • 7,652
  • 4
  • 28
  • 45
  • 1
    This is duplicate of http://stackoverflow.com/questions/26852321/docker-add-a-restart-policy-to-a-container-that-was-already-created – Rajiv Aug 22 '16 at 09:09
  • Hi, I asked a similar question here: https://stackoverflow.com/questions/76127700/docker-container-keeps-running-after-system-was-pruned I wasn't able to find a solution to my problem as `docker ps` returns nothing – cconsta1 Apr 28 '23 at 16:49

4 Answers4

115

Here's the mighty eagle that docker has recently included. :D

You can update docker container.

use sudo docker update --restart=no <container_id> to update --restart flag of the container.

Now you can stop the container.

Rafaf Tahsin
  • 7,652
  • 4
  • 28
  • 45
2

You should be able to just use docker stop and then docker rm to make sure the container doesn't restart when the daemon restarts.

Your question is an issue on the docker github and someone has made some comments about to how to solve here

I'm not sure if it's intended behavior to restart a stopped container on daemon restart... but for sure docker rm would be all that is needed, no need to remove the image.

zglin
  • 2,891
  • 2
  • 15
  • 26
  • When you mention `--restart=always`, container will automatically start if you `stop` it. – Rafaf Tahsin Aug 22 '16 at 06:20
  • @RafafTahsin, no it won't. Only if you restart the Docker daemon. – BMitch Aug 22 '16 at 11:34
  • 1
    If there is something wrong with the container, so it gets stuck in restarting, You either have to use the update trick or stop the deamon so it wont restart. – MortenB Mar 08 '18 at 11:41
1

If you use docker stop or docker kill, you're manually stopping the container so it will not restart. You can do some tests about restart policies: restarting the docker daemon, rebooting your server, using a CMD inside a container and running an exit...

See this answer for more details:

https://serverfault.com/a/884823/381420

Achraf JEDAY
  • 1,936
  • 5
  • 24
  • 33
1

TL;DR

Also check docker swarm if there are any stacks that spin up containers there. Simply run docker stack ls followed by docker rm <stack_name>.


Longer version

This is not exactly an answer to your question, but I had a very similar issue where containers kept spinning up even if I ran docker update --restart=no <container_id>, docker stop <container_id> and docker rm <container_id>. These were some old containers, so I had no clue how I generated them.

After some Googling, I realized that it was a docker swarm stack that kept spinning up containers. By running docker stack ls followed by docker rm <stack_name>, I was able to stop the auto spin-up of the containers and thus remove them entirely.

Andreas Forslöw
  • 2,220
  • 23
  • 32