How do I keep the Docker container and node app running on EC2?

I would like to run a docker container and a standalone node.js application on Amazon EC2 Ubuntu and have them running all the time. I had a node app running with nohup

which worked great, but I want to run docker and node app at the same time so they both work or crash at the same time. In addition, they should automatically restart after a crash. (When I ran docker on screen it didn't help after a while.) What is an easy way to do this? I noticed tools like Supervisor , but I wasn't sure which tool was specifically for this purpose.

+3


source to share


2 answers


As I wrote in a comment, this will help you get more information about what exactly you are trying to accomplish and why you should provide circular dependencies between your processes. Circular dependencies are best avoided in most contexts. I suspect there may be a simpler solution to the underlying problem you are trying to address. I suggest you rethink your architecture and see if you can achieve a weaker connection between the components. This will greatly simplify your operations. That said, managing dependencies between processes is smart enough, so here's a general answer to your question.

Upstart is the default init daemons on ubuntu via trusty. It's highly customizable and can restart apps when they fail. For future releases, both debian and ubuntu will switch to systemd . Both upstart and systemd provide mechanisms for managing dependencies. Upstart stop on stop looks like it does what you want, although I've never experimented with it myself. Systemd allows you to specify ExecStop

and FailureActions

for a service , one of which can be used to destroy the associated service as appropriate. Systemd may provide a better control mechanism that I am not aware of.



Another way to manage dependencies would be to monitor the supervisor of both node.js and your other process together inside a container. Supervisor is not a good tool for starting and stopping containers, but it is great for managing processes inside a container. With a little coding, you can create a custom listener that will also run inside the container and instruct the supervisor to exit if any of your processes terminate. The advantage of this approach is the ability to encapsulate your dependencies in a container. You can then use upstart or systemd to start the container, and all you have to do is make sure the container is restarted if it exits.

Update: Docker can restart your processes for you now , but the best approach in this situation is probably to run the node app in a separate docker container and also deploy them together using ECS ​​or Kubernetes.

+2


source


Simple, keeping all your requirements, I don't know.

For a reliable and scalable way, here's how I would do it.



  • Set up a Consul cluster (it doesn't actually have to be an actual server cluster at startup)
  • Configure service discovery (i.e. Consul will know which services to monitor)
  • Set up health (i.e. the Consul checks the status of your service)
  • Create a custom script that polls Consul for service status every X seconds (hooks coming soon, so polling will become unnecessary in the near future)
  • This script is responsible for ssh-ing and restarting both apps (docker app and node.js app)
-1


source







All Articles