user824624
user824624

Reputation: 8080

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

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?

Upvotes: 13

Views: 22745

Answers (2)

Andy
Andy

Reputation: 38357

You should use --restart (docs):

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

Upvotes: 28

Fabien Balageas
Fabien Balageas

Reputation: 634

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.

Upvotes: 6

Related Questions