am-rails
am-rails

Reputation: 1473

How do I keep a Docker container and a node application running on EC2?

I would like to run a docker container and a separate node.js application on Amazon EC2 Ubuntu, and keep them running constantly. I had the node app running with nohup which worked fine, but I want to run both docker and the node app together so they both either work or fail at the same time. Also, they should automatically restart after a failure. (When I ran docker in screen, it failed after a while.) What is a simple way to accomplish this? I noticed tools such as Supervisor, but I wasn't sure what tool was specifically for this purpose.

Upvotes: 3

Views: 1848

Answers (2)

Aryeh Leib Taurog
Aryeh Leib Taurog

Reputation: 5598

As I wrote in the comment, it would help to have more information about what exactly you are trying to accomplish and why you must enforce a circular dependency between your processes. Circular dependencies are best avoided in most contexts. I suspect there might be a more simple solution to the underlying problem which you are trying to address. I suggest you rethink your architecture and see if you can't achieve looser coupling of components. That will greatly simplify your ops. Still, managing dependencies between processes is a reasonable enough concern, so here's a general answer to your question.

Upstart is the default init daemon on ubuntu through trusty. It is easy to configure and can restart applications when they fail. For future releases, both debian and ubuntu will be switching to systemd. Both upstart and systemd provide mechanisms for managing dependencies. Upstart's stop on stopped looks like it does what you want, although I haven't ever experimented with it myself. Systemd allows you to specify ExecStop and FailureActions for a service, one of which could be used to kill a related service under the appropriate circumstances. Systemd might provide a more appropriate control mechanism that I'm not aware of.

Another way to manage the dependencies would be have supervisord manage both node.js and your other process together inside the container. Supervisor isn't a good tool for starting and stopping containers, but it is excellent for managing processes inside a container. With a bit of coding, you can create a custom listener which would also run inside the container and would instruct supervisor to exit if either of your processes terminates. The advantage of this approach is you can portably encapsulate your dependencies within the container. Then you could use either upstart or systemd to start the container and all you'd 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 application in a separate docker container as well and deploy them together using ECS or Kubernetes.

Upvotes: 2

rollingBalls
rollingBalls

Reputation: 1878

Simple while keeping all your requirements, I do not know.

For a robust and scalable way though, here 's how I 'd do it.

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

Upvotes: -1

Related Questions