272

Docker starts a container on every system startup (debian) but I didn't create a service to do so. How can I prevent docker from doing that?

nnnmmm
  • 7,964
  • 4
  • 22
  • 41
Techradar
  • 3,506
  • 3
  • 18
  • 28
  • 15
    I disagree with this being marked as a duplicate. The other question asks about how to disable restarting for a container that has been intentionally configured to restart using `restart=always`, while this question is about how to disable restarting for a conainer that has been **unintentionally** configured to restart. `restart=always` is only one possible reason why a container is restarting. – cdauth Jan 11 '21 at 14:21
  • 1
    In fact, I would like to give an answer for another scenario where a container configured with `restart=on-failure` unintentionally restarts on every system boot. That answer would fit here, but it doesn't fit to the other question. – cdauth Jan 11 '21 at 14:22
  • 1
    I’ve posted a response to this question on [here](https://stackoverflow.com/a/65894195/242365). – cdauth Jan 26 '21 at 00:12
  • See docs for [start containers automatically](https://docs.docker.com/config/containers/start-containers-automatically/) it works for `docker run` and `docker update` – Ricardo Dec 09 '21 at 02:53
  • There's another interpretation of this question which is, how do I prevent docker from restarting containers on boot even if the container has `restart=always`. [I've made a new question here](https://stackoverflow.com/questions/75230964/how-to-prevent-all-docker-containers-from-automatically-restarting-on-boot) to answer this variant. – Matthew D. Scholefield Jan 25 '23 at 07:47

4 Answers4

544

Docker will autostart any container with a RestartPolicy of 'always' when the docker service initially starts. You won't find any evidence of this within cron or any other normal system startup scripts; you'll have to dig into the container configuration to find it.

docker inspect my-container (Look for RestartPolicy in the output)

I've mostly had this situation occur when a container was created with --restart always, and the situation later changed such that I no longer wanted this to happen.

After docker 1.11, this is easy to fix

docker update --restart=no my-container

Original answer is here: docker - how do you disable auto-restart on a container?

Ricardo
  • 3,696
  • 5
  • 36
  • 50
user2628688
  • 5,544
  • 1
  • 14
  • 3
  • 32
    great answer. In order to quickly find the RestartPolicy config, you can use `docker inspect my-container | grep -A 3 RestartPolicy`. The `-A n` grep option shows n lines after the match. – Tarek Feb 28 '20 at 14:42
  • That did the trick, however there wasn't a "RestartPolicy" in my output (used grep). Worked nonetheless. Thanks! – Vippy May 22 '20 at 18:41
  • Did you check that you don't have a stack created ? e.g. I had executed once the following code and forget... docker stack deploy --compose-file stack.yml vossibility This makes that automatically containers are created and started everytime those containers defined on compose file stop. I was reading more about it on ofitial documentation and the esasy one was remove the stack. https://docs.docker.com/engine/reference/commandline/stack/ – MadMad666 Jun 24 '21 at 04:28
30

The info is in /var/lib/docker/containers/<hash>/hostconfig.json (having root permissions)

check for RestartPolicy.Name = "always" in any of those files.

U.V.
  • 572
  • 4
  • 11
-5
  1. You can also take a look in System->Preferences->Startup Applications. (Search startup application app)
  2. use this command : 'Crontab -l' (check any cronjob is set or not)
  3. grep docker in /etc/init.d and if you find you can remove from there with sudo permission ( Make sure you dont change other files or anything else, do it on your risk )

  4. sudo docker run --restart=no redis (replace redis with your container name)

Let me know if you still find any problem.

  • 1
    1. Its okay if docker starts on system startup, so I don't think that a specific container would appear here (anyway: I am using awesome wm so I dont have that menu) 2. Dont have any cronjobs listed 3. No service in /etc/init.d Anymore ideas? – Techradar Nov 09 '16 at 18:33
  • sudo docker run --restart=no redis – Kalrav J Parsana Nov 09 '16 at 18:41
  • replace redis with your container name – Kalrav J Parsana Nov 09 '16 at 18:41
  • 2
    Got it being removed with removing the container (therefore removed all with: docker rm `docker ps --no-trunc -aq`). But I sure accept your answer because there are few notes which could help. Thanks! – Techradar Nov 09 '16 at 18:52
-8

Launch Docker if not running. Click the Docker icon in the menu bar. Select Preferences... (or press Cmd-comma). Deselect 'Start Docker when you log in' on the General tab. Close the Preferences window. Quit Docker (Cmd-Q or use the menu).

  • 1
    Adrian, this question is not asking how to stop docker from running on startup, but how to avoid containers starting automatically when you start docker. – lcjury Mar 06 '23 at 19:38