├── .ruby-version ├── .ruby-gemset ├── Gemfile ├── app.rb ├── config.ru ├── Dockerfile ├── Gemfile.lock ├── puma.rb └── README.md /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.3 2 | -------------------------------------------------------------------------------- /.ruby-gemset: -------------------------------------------------------------------------------- 1 | test-puma-app 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gem 'sinatra' 3 | gem 'puma' 4 | -------------------------------------------------------------------------------- /app.rb: -------------------------------------------------------------------------------- 1 | require 'sinatra/base' 2 | 3 | class Pumatra < Sinatra::Base 4 | get '/' do 5 | "Hello World!" 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "rubygems" 4 | require "sinatra" 5 | require File.expand_path '../app.rb', __FILE__ 6 | 7 | require './app' 8 | run Pumatra 9 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ruby:2.3 2 | 3 | RUN mkdir -p /app 4 | RUN mkdir -p /var/shared 5 | 6 | WORKDIR /app 7 | ADD . /app 8 | RUN gem install bundler && bundle install 9 | 10 | CMD puma config.ru -C puma.rb 11 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | puma (3.4.0) 5 | rack (1.6.4) 6 | rack-protection (1.5.3) 7 | rack 8 | sinatra (1.4.7) 9 | rack (~> 1.5) 10 | rack-protection (~> 1.4) 11 | tilt (>= 1.3, < 3) 12 | tilt (2.0.2) 13 | 14 | PLATFORMS 15 | ruby 16 | 17 | DEPENDENCIES 18 | puma 19 | sinatra 20 | 21 | BUNDLED WITH 22 | 1.11.2 23 | -------------------------------------------------------------------------------- /puma.rb: -------------------------------------------------------------------------------- 1 | # Change to match your CPU core count 2 | workers (ENV['PUMA_WORKERS'] || 1).to_i 3 | 4 | # Min and Max threads per worker 5 | threads 1, (ENV['MAX_THREADS'] || 10).to_i 6 | 7 | shared_dir = "/var/shared" 8 | 9 | # Set up socket location 10 | bind "unix://#{shared_dir}/app.sock?umask=0000" 11 | 12 | # Logging 13 | stdout_redirect "#{shared_dir}/puma.stdout.log", "#{shared_dir}/puma.stderr.log", true 14 | 15 | # Set master PID and state locations 16 | pidfile "#{shared_dir}/puma.pid" 17 | state_path "#{shared_dir}/puma.state" 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dummy Puma Sinatra Docker app. running on a socket 2 | 3 | [![Open Thanks](https://thawing-falls-79026.herokuapp.com/images/thanks-1.svg)](https://thawing-falls-79026.herokuapp.com/r/emrbpnuk) 4 | ![Gittens](http://gittens.r15.railsrumble.com//badge/equivalent/dummy-sinatra-puma-socket-docker-app) 5 | 6 | [Docker image](https://hub.docker.com/r/equivalent/dummy-sinatra-puma-socket-docker-app/) 7 | 8 | Given your NginX server is expecting connection on unix socket `unix:///var/shared/app.sock` 9 | This docker image recipe will run small [Sinatra](http://www.sinatrarb.com/) 10 | dummy application that will run [Puma server](http://puma.io/) on this socket. 11 | 12 | For example: 13 | 14 | ``` 15 | /etc/nginx/nginx.conf 16 | # ... 17 | upstream myapp { 18 | server unix:///var/shared/app.sock; 19 | } 20 | # ... 21 | ``` 22 | 23 | ## Step 1 - Run Puma 24 | 25 | ### option 1 - Run as Docker Image 26 | 27 | ```bash 28 | docker pull equivalent/dummy-sinatra-puma-socket-docker-app 29 | 30 | # Run Docker image 31 | mkdir -p /tmp/dummy-app 32 | docker run -v /tmp/dummy-app/:/var/shared/ -d equivalent/dummy-sinatra-puma-socket-docker-app 33 | 34 | # Check 35 | docker ps 36 | ``` 37 | 38 | Now you have a Puma running on a socket `/tmp/dummy-app/app.sock` 39 | 40 | ### option 2 - Build Docker image locally and run Docker image 41 | 42 | ```bash 43 | git clone git@github.com:equivalent/dummy-sinatra-puma-socket-docker-app.git 44 | cd ./dummy-sinatra-puma-socket-docker-app 45 | 46 | # build docker image 47 | docker build -t=dummy-sinatra-puma-socket-docker-app . 48 | 49 | # Prepare and run Docker image 50 | mkdir -p /tmp/dummy-app 51 | docker run -v /tmp/dummy-app/:/var/shared/ -d dummy-sinatra-puma-socket-docker-app 52 | 53 | # Check 54 | docker ps 55 | ``` 56 | 57 | ### option 3 - Running as Ruby without Docker 58 | 59 | To launch server 60 | 61 | ```bash 62 | git clone git@github.com:equivalent/dummy-sinatra-puma-socket-docker-app.git 63 | cd ./dummy-sinatra-puma-socket-docker-app 64 | bundle install 65 | bundle exec puma config.ru -C puma.rb 66 | ``` 67 | 68 | Now you have a puma running on a socket `/tmp/dummy-app/app.sock` 69 | 70 | ## step 2 - Launch NginX 71 | 72 | Create & start NginX that is expecting connection on socket `/tmp/dummy-app/app.sock` 73 | 74 | for example: 75 | 76 | ``` 77 | /etc/nginx/nginx.conf 78 | # ... 79 | upstream myapp { 80 | server unix:///tmp/dummy-app/app.sock; 81 | } 82 | # ... 83 | ``` 84 | 85 | or build a Docker NginX image using your config 86 | 87 | 88 | ``` 89 | /etc/nginx/nginx.conf 90 | # ... 91 | upstream myapp { 92 | server unix:///var/shared/app.sock; 93 | } 94 | 95 | # ... 96 | 97 | server { 98 | # ... 99 | location / { 100 | # ... 101 | proxy_pass http://myapp; 102 | # ... 103 | } 104 | } 105 | ``` 106 | 107 | ``` 108 | cd ./your-nginx-docker-project 109 | docker build -t=testnginx . 110 | docker run -v /tmp/dummy-app/:/var/shared/ -p 80:80 -it testnginx 111 | ``` 112 | 113 | > if you are getting 504 you may be running nginx before Puma server 114 | 115 | ## Notes 116 | 117 | * This is a demo app. Be sure to fork and alter any config according to your 118 | needs 119 | * If you want some NginX example check 120 | https://gist.github.com/ctalkington/4448153#file-nginx-conf-L4 or 121 | https://www.youtube.com/watch?v=jSy1Bnf3WIk 122 | --------------------------------------------------------------------------------