I have an Rails 5 API which I am trying to deploy(correctly) on Elastic Beanstalk.
Here is my initial config/puma.rb
file which I use:
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count
# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
port ENV.fetch("PORT") { 3000 }
# Specifies the `environment` that Puma will run in.
environment ENV.fetch("RAILS_ENV") { "development" }
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
I get the following socket error:
2015/11/24 06:44:12 [crit] 2689#0: *4719 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream
To fix this I tried adding below lines and got it to work:
rails_env = ENV['RAILS_ENV'] || "production"
if rails_env == "production"
bind "unix:///var/run/puma/my_app.sock"
pidfile "/var/run/puma/my_app.sock"
end
My real question is, is this the right way to do it? If anyone has done it before can you point me to it? Is there a way to do this via docker containers?
You can deploy your Rails app as a Rails - Puma app to Elastic Beanstalk or Docker as well. The answer will be more general and rather points where to start than provides complete solution.
Ruby - Puma
This can be a quite tricky: If you create new Elastic Beanstalk Environment for Ruby via Console (in Web Browser), it can set Passenger platform by default, instead of Puma platform. And probably you can't change it in console:
To create new environment with Puma, use
eb cli
. Nice walkthrough here. However, before you runeb create
, you have to do one more thing - select platform:If you want to create Elastic Beanstalk's Worker instead of Web Server, run:
You can use Console (Web Browser) or
eb cli
(docs) to set other configuration.Docker
Following post maybe useful how to setup Rails + Puma + Nginx + Docker:
http://codepany.com/blog/rails-5-and-docker-puma-nginx/
This is multicontainer configuration, where Nginx is binded to port 80 and streams request to puma via socket. In your case it would be:
"unix:///var/run/puma/my_app.sock"
To upload Dockers, you can use AWS ECR to store Docker images. You have to create Dockerrun.aws.json file (is quite similar to docker-compose.yml file), which you can than deploy via AWS Console (web browser) to your environment.
EDIT
Here is the
puma.rb
configuration file:Some settings may vary, but the point is that I bind there a puma server to unix socket and it connects with NGINX. The NGINX configuration file:
The most important part in NGINX configuration file is: