Coder Social home page Coder Social logo

docker-rails's Introduction

Docker-Rails

[Circle CI] (https://circleci.com/gh/ComboStrikeHQ/docker-rails)

An opinionated docker image for running Rails apps in production.

Uses Puma, Sidekiq, Clockwork and rails_migrate_mutex. Everything is optional.

Quick Example

  • Create a new Rails app ($ rails new my_blog)
  • Create a Dockerfile in the app root with this content: FROM combostrikehq/docker-rails:latest
  • Add gem 'puma' to the Gemfile and run bundle install
  • Build the docker container: $ docker build -t my_blog .
  • Run the container: $ docker run -p 8080:8080 -e RAILS_ENV=development my_blog
  • Open http://localhost:8080
  • Enjoy!

This example is for demo purposes only. Docker-Rails is intended for production use, not local development. Please read the whole README before using.

Docker Image

The image is built by us, squashed into a single layer and pushed to Docker Hub.

To use it in your app, create a Dockerfile with the following content:

FROM combostrikehq/docker-rails:<VERSION>

and replace <VERSION> with the current version number. You can also use the latest version tag which would give you the latest unreleased version. This might be useful if you want to test against the bleeding edge in your testing/beta environment.

From version 3.0 docker-rails uses ruby 2.5. If you still use ruby 2.4 you have to use docker-rails versions 2.x. We also provide ruby-2.5 and ruby-2.4 tags that will point to the latest released docker-rails version for that ruby version.

Add more docker steps if needed (usually not), build and deploy to your infrastructure.

We use eb_deployer to deploy our apps to AWS Elastic Beanstalk.

Contents

Rails App Server / Puma

Puma is used as the web server for your app. For this to work properly, please add the following gems to your Gemfile:

gem 'puma'
gem 'rack-timeout', group: :production
gem 'rails_12factor', group: :production  # only for Rails < 5

You can configure Puma:

Environment Variable Description Default Value
PUMA_WORKERS Number of worker processes [number of CPU cores]
PUMA_MAX_THREADS Maximum number of threads per process 16
PUMA_MIN_THREADS Minimum number of threads per process PUMA_MAX_THREADS

You can also create a config/puma.rb file in your app that contains additional configuration.

Have a look at the Puma configuration file for more details.

Sidekiq

Sidekiq is used for processing background jobs. A sidekiq process will be automatically started when the sidekiq gem is present in your app. Using sidekiq is optional.

You can configure sidekiq:

Environment Variable Description Default Value
SIDEKIQ_THREADS Number of threads 16

You can also create a config/sidekiq.yml file in your app that contains additional configuration.

The default configuration enables the default and mailers queues.

Clockwork

Clockwork is used for scheduling background jobs. A clockwork process will be automatically started when the clockwork gem is present in your app. Using clockwork is optional.

You need to create a config/clockwork.rb file using the following schema:

module Clockwork
  every(1.hour, 'my_job') { MyWorker.perform_async }
end

Note that the clockwork process is started in every container. We recommend using sidekiq-unique-jobs to ensure that jobs are scheduled only once.

Custom services

You can run additional services by putting their executable files in the application bin/services folder.

Rails Migrations / rails_migrate_mutex

rails_migrate_mutex is used to run Rails migrations. If you have the gem installed in you app, migrations are automatically run on container startup. Using rails_migrate_mutex is optional.

Ruby / Packages

The latest ruby version is included in the container. It is compiled from source and uses jemalloc. The latest rubygems and bundler versions are installed as well.

The following tools are installed: git, nodejs, imagemagick, curl, bower. Development packages for compiling the following gems are included: sqlite3, pg, mysql2, nokogiri.

Have a look at the Base Dockerfile for more details.

App Installation / Docker ONBUILD

The final docker image has ONBUILD hooks that set up your application. It installs all gems except those in the development and test groups.

If the sprockets gem is present (default for Rails apps), it also precompiles assets. If you need any environment variables set for compiling assets, you can specify them in a .env file in your app root. This file will be sourced before running the task. Newer versions of sprockets will also automatically created gzipped versions of the assets. Rails then uses these static gzip files to serve assets.

Have a look at the ONBUILD Dockerfile for more details.

Environment Variables

All environment variables are passed on the application. Both RAILS_ENV and RACK_ENV are set to production and should not be changed.

Both RAILS_SERVE_STATIC_FILES and RAILS_LOG_TO_STDOUT are set, so you don't need to include the rails_12factor gem anymore if you're using Rails 5 or above and have updated your config/environments/production.rb file.

License

MIT, see LICENSE.txt

Contributing

Please create a new GitHub Issue if you encounter a bug or have a feature request.

Feel free to fork and submit pull requests!

docker-rails's People

Contributors

alanwilhelm avatar dziemba avatar gaurish avatar guigs avatar hwartig avatar ndreynolds avatar rkushnir avatar tsujigiri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

docker-rails's Issues

syslog-ng stops

Don't know why but syslog-ng stops logging after some time. Any idea?

Sidekiq is not running

Hi, I'm using this image for my project but sidekiq seems not being running.

I'm deploying the project at Beanstalk and it runs fine, also, it is queueing all messages to redis but they are not being consumed

Instance:
64bit Amazon Linux 2016.09 v2.5.1 running Docker 1.12.6

DockerImage:
FROM ad2games/docker-rails:2.5.0
EXPOSE 8080

issue with HTTP request

https://registry-1.docker.io/v2/ad2games/docker-rails/blobs/sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4

this is giving me a 401 error.

I'm using rkt, which may be the problem. when i go to the link in my browser i get the same error.

Unable to install packages because /var/lib/dpkg is deleted

In the cleanup /var/lib/dpkg is deleted. This means that you cannot do any apt-get commands like update or install. We are unable to install any other dependencies. Is there a reason this is done?

It shows up with this error:

E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

Unknown MySQL server host 'noop'

In a new project I get this error and cannot find any reason.

Step 1/1 : FROM ad2games/docker-rails:latest
# Executing 5 build triggers...
Step 1/1 : COPY Gemfile Gemfile.lock /home/app/webapp/
 ---> Using cache
Step 1/1 : COPY vendor/ /home/app/webapp/vendor/
 ---> Using cache
Step 1/1 : RUN chown -R app:app Gemfile Gemfile.lock vendor/ &&   chpst -u app bundle install --deployment --jobs 4 --without development test &&   find vendor/bundle -name *.gem -delete
 ---> Using cache
Step 1/1 : COPY . /home/app/webapp/
Step 1/1 : RUN mkdir -p db public/assets log tmp vendor &&   chown -R app:app app db public log tmp vendor && chpst -u app /opt/rails-assets.sh
 ---> Running in f3820f857ac3
/home/app/webapp/vendor/bundle/ruby/2.3.0/gems/sprockets-3.7.0
Could not find gem 'pg'.
/home/app/webapp/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5
WARNING: Skipping key "SECRET_KEY_BASE". Already set in ENV.
rake aborted!
**Mysql2::Error: Unknown MySQL server host 'noop' (25)**
/home/app/webapp/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5/lib/mysql2/client.rb:89:in `connect'
/home/app/webapp/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5/lib/mysql2/client.rb:89:in `initialize'
/home/app/webapp/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/mysql2_adapter.rb:25:in `new'

Volume addition

Hello there. Love your image!
I know you mention that it is intended for production use but I would really like to use it in development as well. The bad thing is that i've tried but couldn't make it to copy the folder I'm working on at the host to the container whenever I change something.
Actually, through a docker-compose.yml I've added a volume - .:/myapp but this isn't the working folder.
Whenever I change it to use -.:/home/app/webapp/ puma just won't start.
Do you have any ideas why this is happening?
Thank you very much in advance!

mysql support

How could I add mysql-support?
I tried to build image form this dockerfile:

# Dockerfile
FROM ad2games/docker-rails:latest
RUN apt-get install -qq -y mysql-client libmysqlclient-dev 

but this does not work.
Also adding new packages by ENV APT_PACKAGES from the docker command does not resolve.

rails-assets.sh failing due to missing RAILS_MASTER_KEY

When using encrypted secrets on a Rails 5.1 app, deployment fails with this message:

Rails::Secrets::MissingKeyError: Missing encryption key to decrypt secrets with. Ask your team for your master key and put it in ENV["RAILS_MASTER_KEY"]

I have RAILS_MASTER_KEY configured as environment variable but it's not picked up.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.