Last active
December 8, 2022 22:13
-
-
Save dsandstrom/3398cb0b4013bea368536c41a6498b89 to your computer and use it in GitHub Desktop.
Dockerfile for Rails app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upstream app { | |
# Path to Puma SOCK file, as defined previously | |
server unix:/sockets/puma.sock fail_timeout=0; | |
} | |
server { | |
listen 80; | |
server_name localhost; | |
root /project/public; | |
try_files $uri/index.html $uri @app; | |
location @app { | |
proxy_pass http://app; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $http_host; | |
proxy_redirect off; | |
} | |
error_page 500 502 503 504 /500.html; | |
client_max_body_size 4G; | |
keepalive_timeout 10; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Puma can serve each request in a thread from an internal thread pool. | |
# The `threads` method setting takes two numbers a minimum and maximum. | |
# Any libraries that use thread pools should be configured to match | |
# the maximum value specified for Puma. Default is set to 5 threads for minimum | |
# and maximum, this matches the default thread size of Active Record. | |
# | |
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 3 }.to_i | |
threads 1, 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" } | |
# Specifies the number of `workers` to boot in clustered mode. | |
# Workers are forked webserver processes. If using threads and workers together | |
# the concurrency of the application would be max `threads` * `workers`. | |
# Workers do not work on JRuby or Windows (both of which do not support | |
# processes). | |
# | |
workers ENV.fetch("WEB_CONCURRENCY") { 2 } | |
# Use the `preload_app!` method when specifying a `workers` number. | |
# This directive tells Puma to first boot the application and load code | |
# before forking the application. This takes advantage of Copy On Write | |
# process behavior so workers use less memory. If you use this option | |
# you need to make sure to reconnect any threads in the `on_worker_boot` | |
# block. | |
# | |
# preload_app! | |
# The code in the `on_worker_boot` will be called if you are using | |
# clustered mode by specifying a number of `workers`. After each worker | |
# process is booted this block will be run, if you are using `preload_app!` | |
# option you will want to use this block to reconnect to any threads | |
# or connections that may have been created at application boot, Ruby | |
# cannot share connections between processes. | |
# | |
# on_worker_boot do | |
# ActiveRecord::Base.establish_connection if defined?(ActiveRecord) | |
# end | |
# Allow puma to be restarted by `rails restart` command. | |
plugin :tmp_restart |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Puma can serve each request in a thread from an internal thread pool. | |
# The `threads` method setting takes two numbers a minimum and maximum. | |
# Any libraries that use thread pools should be configured to match | |
# the maximum value specified for Puma. Default is set to 5 threads for minimum | |
# and maximum, this matches the default thread size of Active Record. | |
threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }.to_i | |
threads 1, threads_count | |
# Specifies the `port` that Puma will listen on to receive requests, default is | |
# 3000. | |
# port ENV.fetch('PORT') { 3000 } | |
# Set up socket location | |
bind 'unix:///sockets/puma.sock' | |
# Specifies the `environment` that Puma will run in. | |
environment ENV.fetch('RAILS_ENV') { 'production' } | |
# Specifies the number of `workers` to boot in clustered mode. | |
# Workers are forked webserver processes. If using threads and workers together | |
# the concurrency of the application would be max `threads` * `workers`. | |
# Workers do not work on JRuby or Windows (both of which do not support | |
# processes). | |
workers ENV.fetch('WEB_CONCURRENCY') { 2 } | |
# Use the `preload_app!` method when specifying a `workers` number. | |
# This directive tells Puma to first boot the application and load code | |
# before forking the application. This takes advantage of Copy On Write | |
# process behavior so workers use less memory. If you use this option | |
# you need to make sure to reconnect any threads in the `on_worker_boot` | |
# block. | |
preload_app! | |
# The code in the `on_worker_boot` will be called if you are using | |
# clustered mode by specifying a number of `workers`. After each worker | |
# process is booted this block will be run, if you are using `preload_app!` | |
# option you will want to use this block to reconnect to any threads | |
# or connections that may have been created at application boot, Ruby | |
# cannot share connections between processes. | |
on_worker_boot do | |
ActiveRecord::Base.establish_connection if defined?(ActiveRecord) | |
end | |
before_fork do | |
ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord) | |
end | |
# Allow puma to be restarted by `rails restart` command. | |
# plugin :tmp_restart |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use the barebones version of Ruby 2.2.3. | |
FROM ruby:2.7.6 | |
# Optionally set a maintainer name to let people know who made this image. | |
# MAINTAINER | |
# Install dependencies: | |
# - build-essential: To ensure certain gems can be compiled | |
# - nodejs: Compile assets | |
# - libpq-dev: Communicate with postgres through the postgres gem | |
# - postgresql-client-9.4: In case you want to talk directly to postgres | |
RUN apt-get update && apt-get install -qq -y build-essential nodejs libpq-dev --fix-missing --no-install-recommends | |
RUN apt-get install -qq -y nginx --fix-missing --no-install-recommends | |
RUN mkdir /sockets | |
# Set an environment variable to store where the app is installed to inside | |
# of the Docker image. | |
ENV INSTALL_PATH /project | |
RUN mkdir -p $INSTALL_PATH | |
RUN mkdir -p $INSTALL_PATH/tmp/pids | |
# This sets the context of where commands will be ran in and is documented | |
# on Docker's website extensively. | |
WORKDIR $INSTALL_PATH | |
# Ensure gems are cached and only get updated when they change. This will | |
# drastically increase build times when your gems do not change. | |
COPY Gemfile Gemfile | |
COPY Gemfile.lock Gemfile.lock | |
RUN gem install bundler --version 1.17.3 | |
RUN bundle config build.nokogiri --use-system-libraries | |
RUN bundle install --without test development | |
# Copy in the application code from your work station at the current directory | |
# over to the working directory. | |
COPY . . | |
# Copy nginx config | |
COPY ./config/nginx/default /etc/nginx/sites-available/default | |
ENV RAILS_ENV production | |
RUN bin/rails assets:precompile | |
# Expose a volume so that nginx will be able to read in assets in production. | |
VOLUME ["$INSTALL_PATH/public"] | |
# Start servers | |
CMD bundle exec puma -C config/puma/production.rb | |
# CMD service nginx restart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment