13

I am currently running two virtual servers with offical ghost image and nginx-proxy image, here is my build-up.

docker run -d -p 86:2368 --name home -e "VIRTUAL_HOST=hostname.com" ghost 
docker run -d -p 85:2368 --name home-blog -e "VIRTUAL_HOST=blog.hostname.com" ghost

They are all working well, but after a while (sometimes hours or a day), one of the vitual server will break down, and I have to restart the container to make it work.

I wonder is there any solution to automatically monitor the docker container and restart it when it goes down?

rafaelc
  • 57,686
  • 15
  • 58
  • 82
user824624
  • 7,077
  • 27
  • 106
  • 183

2 Answers2

24

You should use --restart (docs):

docker run -d -p 86:2368 --restart always --name home -e "VIRTUAL_HOST=hostname.com" ghost 
Kyle Morgan
  • 660
  • 1
  • 11
  • 21
Andy
  • 35,844
  • 6
  • 43
  • 50
  • And related question if you use that option and it does not restart automatically after a crash. How to restart an existing Docker container in restart=“always” mode? http://stackoverflow.com/questions/29603504/how-to-restart-an-existing-docker-container-in-restart-always-mode – Dijkgraaf May 05 '15 at 00:18
6

In fact, it is more likely your container's main application crashed and not your container.

When the process with ID #0 stops or crashes in a container, then the container automatically stops.

About your concern, the restart option (from the docker run command) is one possibility, as stated by Andy.

Another possibility is to use supervisord as container's main process. Your application will be launched and monitored by supervisord. Supervisord will provide you with lots of options in order to handle your application crash. You have many useful options about logging, signal handling...

See https://docs.docker.com/articles/using_supervisord/ and http://supervisord.org/ for more details.

Fabien Balageas
  • 624
  • 5
  • 6
  • it depends a bit on how you launch your processes, if you launch the container with a cmdline to start just a single program for example (your daemon), and the daemon crashes, the container will exit. in that situation, I'm not sure if the restart docker option will restart the container? – BjornW Jul 15 '19 at 10:51