How Discourse docker script manages to autostart upon host machine reboot?


(Pahlevi Fikri Auliya) #1

By following default installation of Discourse on Docker, the server manages to autostart upon host machine reboot. As I wish to know more about inner side of Discourse, I wonder how the docker can autostart Discourse image?


(Jeff Atwood) #2

There were some serious bugs here in previous versions of Docker, for the record… but yes, current 1.2+ Docker versions do restart containers reliably on boot!


(Kane York) #3

Docker knows what command the container is supposed to be running (I believe it’s runit?) so it just remembers that and starts it again on boot.


(Sam Saffron) #4

Read up about restart policies


(Kane York) #5

That’s about restarting if the process exits, not if the machine turns off.


(Sam Saffron) #6

That is about both from all I can tell, the restart policy changes corrected the reboot behavior, crash behavior and upgrade behavior.


(Pahlevi Fikri Auliya) #7

Interesting!

So the restart mechanism is done by docker, not by discourse image in particular nor by discourse script.


(Sam Saffron) #8

Yes, this is built in to Docker


(Jakob Borg) #9

I still don’t understand. :slight_smile: At the moment I have a Discourse installation where it should not run (a different web server is serving redirects currently, it’s part of a migration). I’ve stopped Discourse with ./launcher stop app, and absolutely nothing Discourse-related is running. But a reboot or a service restart docker promptly starts the Discourse container again. Where does this come from, so I can kill it? The restart policies linked feels like something that should only be in effect for running containers?


(Sam Saffron) #10

Sounds like a docker bug cc @SvenDowideit

Simple workaround is launcher destroy app


(Balázs Nagy) #11

@calmh @Pahlevi_Fikri_Auliya you have two options. The first one is a bit simplistic: start (docker run) instances with --restart option. This will autostart the instance when docker daemon comes up.

The second option is to restart the instance on boot using an init script (sysv init, runit, upstart, systemd, whatever).

Unfortunately you can’t change restart policy of an already started instance, therefore you need the second option to fully control your docker instance.

For example here is an upstart config to run my youtrack container:

description "Youtrack docker container"

start on runlevel [2345] and started docker
stop on runlevel [!2345]

respawn
respawn limit 10 5
exec /usr/bin/docker start -a youtrack || /usr/bin/docker run --rm --name youtrack -v /opt/youtrack:/opt/youtrack -p 8000:8000 -h localhost -u youtrack ops/youtrack /opt/youtrack/run

(Dan Dascalescu) #12

2016 update: docker wasn’t starting on boot (not sure why), and systemctl enable docker fixed that. Discourse starts automatically now if the VPS reboots :-]