Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to automatically monitor and restart the docker container when it crashes down?

Tags:

docker

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?

like image 623
user824624 Avatar asked May 04 '15 22:05

user824624


2 Answers

You should use --restart (docs):

docker run -d -p 86:2368 --restart always --name home -e "VIRTUAL_HOST=hostname.com" ghost 
like image 72
Andy Avatar answered Oct 07 '22 13:10

Andy


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.

like image 45
Fabien Balageas Avatar answered Oct 07 '22 13:10

Fabien Balageas