Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the correct way to set up puma, rails and nginx with docker?

I have a Rails application which is using Puma. I'm using nginx for load balancing. I would like to dockerize and deploy to a DigitalOcean (Docker) droplet.

After reading lots of blogs and examples (most of which are a year old and that's a long time in the Docker world), I'm still confused about 2 things. Let's say that I select a DigitalOcean box with 4 CPUs. How am I supposed to set up the Rails containers? Should I set up 4 different containers, where Puma is configured with 1 worker process? Or should I set up 1 container where Puma is configured with 4 worker processes?

And the second thing I'm confused about: should I run nginx inside the Rails container, or should I run them in separate containers?

These 2 questions allow 4 permutations that I diagramed below.

option 1 option 1

option 2 option 2

option 3 option 3

option 4 option 4

like image 832
Jenna S Avatar asked Nov 30 '16 01:11

Jenna S


People also ask

Do I need nginx with Puma?

Nginx is a web server and puma is an application server. Both have their advantages, and you need both. Some examples: Static redirects- you could setup your nginx to redirect all http traffic to the same url with https .


1 Answers

Docker likes to push the single process per container style of design. When running multiple processes in a single container there is the extra layer of a service manager in between Docker and the underlying processes which causes Docker to lose visibility of the real service status. This more often than not makes services harder to manage with Docker and it's associated tools. Puma managing workers will not be as bad as a generic service manager running multiple processes.

You may also need to consider the next step in the application, hosting across multiple droplets/hosts and how to easy it will be to move to that next step.

Option 1 and 3 follow Dockers preferred design. If you are using MRI, Puma can run in clustered mode so it just depends on whether you want to manage the Ruby processes yourself (1) or have Puma do the worker management (3). There will be differences between how nginx and Puma distribute requests between workers. Puma can also schedule zero down time updates which would require a bit of effort to get working via Docker. If you are using Rubinius or JRuby you would probably lean towards option 3 and let threads do the work.

Option 1 may allow you to more easily scale across different sized hosts with Docker tools.

Option 2 looks like it adds an unnecessary application hop and Docker no longer maintains the service state in your app tier as you need something else in the container to launch both nginx and Puma.

Option 4 might perform a bit better than others due to the local sockets, but again Docker is no longer aware of the service state.

In any case try a couple of solutions and benchmark both with something like JMeter. You will quickly get an idea of what works and what doesn't, both in performance and management.

like image 78
Matt Avatar answered Sep 21 '22 11:09

Matt