How to auto restart a Docker container after a reboot in CoreOS?
Asked Answered
E

6

41

Assuming the Docker daemon is restarted automatically by whatever init.d or systemd like process when the OS is restarted, what is the preferred way to restart one or more Docker containers? For example I might have a number of web servers behind a reverse proxy or a database server.

Erdman answered 13/9, 2013 at 12:13 Comment(5)
well, I'm actually interested in the exact opposite problem this poses: I said restart=always for a container that seemed to be working, and now, upon docker daemon restart, I can't seem to find a way to turn this auto-restart feature off for this specific container!Hagerty
DOH! I read that the CoreOS team prefers that you use fleet or systemd in order to implement restarts.Erdman
Yup, seems to mean that I shouldn't be using docker's built in features for auto-restart in any case, as with any setup you plan to manage, you need flexibility in defining when a container should resart (and when the docker daemon should restart). This is a bit complicated by the fact that docker inspect command seems to imply that the restart attribute is part and parcel to the image once started (akin to ports exposed). As such, once you define an auto-restart attribute, you can't change your mind :(Hagerty
@Hagerty I use docker rm -f container to remove the container and prevent it auto restartTwinkle
@Hagerty @YiWang try docker update --restart no [CONTAINER] to change config after docker runEffrontery
C
14

CoreOS uses systemd to manage long running services:

Concent answered 13/9, 2013 at 23:4 Comment(5)
Since I'm moving all of my services to CoreOS this seems like the best way to go for now.Erdman
@Erdman Even if that's the case for you, I think you should still mark creack's solution as the best answer instead (more useful to people on other operating systems).Santoro
Mark's answer is not wrong. I was referring to CoreOS (although I did not mention it specifically) it was implied by systemd and initd. Since then there have been new update methods that work in concert.Erdman
Simply restarting containers as in @creack's answer doesn't cater for container startup order dependencies. If you have that, you need systemd service files to take care of that.Cortes
Looks like the link in this post no longer exists and you end up on the quick start guide page. I found coreos.com/os/docs/latest/getting-started-with-systemd.html to be useful.Clergy
M
34

if you start the daemon with docker -d -r, it will restart all containers that were running prior the daemon stopped. This will become the default behavior in the next release.

Microcyte answered 14/9, 2013 at 0:27 Comment(5)
so this means that the host OS was rebooted with running containers or possibly the system crashed? In the former, that means that docker is hooked into the various shutdown/reboot signals?Erdman
When the system reboots, there is a chance the container gets killed before docker. If this happens, the container will not be restarted. Docker is not hooked into shutdown/reboot signals.Microcyte
This was previously a correct answer but after review it appears to be in err. (a) it's missing the ACTION which was probably RUN, however, the '-r' is not availableErdman
This command is stil valid but is now enabled by default. There is no ACTION as it is a daemon flag.Microcyte
Can anyone point me to the documentation about the container restarting after the OS crash?Typhoeus
C
14

CoreOS uses systemd to manage long running services:

Concent answered 13/9, 2013 at 23:4 Comment(5)
Since I'm moving all of my services to CoreOS this seems like the best way to go for now.Erdman
@Erdman Even if that's the case for you, I think you should still mark creack's solution as the best answer instead (more useful to people on other operating systems).Santoro
Mark's answer is not wrong. I was referring to CoreOS (although I did not mention it specifically) it was implied by systemd and initd. Since then there have been new update methods that work in concert.Erdman
Simply restarting containers as in @creack's answer doesn't cater for container startup order dependencies. If you have that, you need systemd service files to take care of that.Cortes
Looks like the link in this post no longer exists and you end up on the quick start guide page. I found coreos.com/os/docs/latest/getting-started-with-systemd.html to be useful.Clergy
D
14

What worked for me is to add --restart='always' to the container {run -d ...} command

Darrel answered 1/9, 2014 at 8:37 Comment(3)
In line with my comment to the original question, I don't know of a way to change that on an image once started. So, if I want to temporarily disable the restart feature, and I didn't dictate 'on-failure' originally, I don't know of a good way to stop a container from restarting automatically :( This hurts when I used up a port that I need for another image. . .Hagerty
Yeah, i had an issue with the --restart='always' command, as when I finally wanted to dictate to not auto-restart that specific container, I couldn't for the life of me figure out how to turn that option off short of deleting the container!Hagerty
I don't want it to be always. Only on computer restarted. Can I do that ?Faucal
C
2

For people want to auto restart a docker container, but didn't specify --restart flag (default to 'no') while running it, you can use docker update command to add one of the following three other options:

  • on-failure
  • unless-stopped
  • always

See this post for the details. People have problem with always restart flag on, can consider using either on-failure or unless-stoppedoption.

Catamenia answered 3/6, 2016 at 21:14 Comment(0)
S
1

The only documentation I've seen is Docker's Host Integration docs which are a bit light on details, etc.

Basically, it suggests starting the daemon with -r=false and using systemd (or upstart if you're using something other than CoreOS).

Slob answered 7/8, 2014 at 19:25 Comment(2)
This link is broken since the end of 2015, here is the latest snapshot of it: web.archive.org/web/20151109053347/http://docs.docker.com/…Regeniaregensburg
Thanks. Updated to docs.docker.com/config/containers/… .Slob
C
-1

Used Restart and RestartSec to make it work:

# Restart after crash
Restart=on-failure
# Give the service 10 seconds to recover after the previous restart
RestartSec=10s

View the documentation.

Colorcast answered 10/2, 2016 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.