Skip to content

Commit

Permalink
Changes for docker
Browse files Browse the repository at this point in the history
  • Loading branch information
joshfng committed Feb 19, 2021
1 parent 1c9cb55 commit 4d755a4
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 62 deletions.
30 changes: 30 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/.bundle
/.vscode
/coverage

/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep

/storage/*
!/storage/.keep

/public/assets
.byebug_history

/config/master.key

/public/packs
/public/packs-test
/node_modules
/yarn-error.log
yarn-debug.log*
.yarn-integrity

/config/credentials/development.key
/config/credentials/test.key
15 changes: 1 addition & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

/.vscode
/coverage

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep

# Ignore uploaded files in development.
/storage/*
!/storage/.keep

/public/assets
.byebug_history

# Ignore master key for decrypting credentials and more.
/config/master.key

/public/packs
Expand All @@ -38,6 +27,4 @@ yarn-debug.log*
.yarn-integrity

/config/credentials/development.key

/config/credentials/test.key
coverage
44 changes: 44 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FROM ruby:2.7.2-alpine

ENV APP_PATH /var/app
ENV BUNDLE_VERSION 2.2.10
ENV BUNDLE_PATH /usr/local/bundle/gems
ENV TMP_PATH /tmp/
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_PORT 3000

# install dependencies for application
RUN apk -U add --no-cache \
build-base \
git \
postgresql-dev \
postgresql-client \
libxml2-dev \
libxslt-dev \
nodejs \
yarn \
imagemagick \
tzdata \
less \
&& rm -rf /var/cache/apk/* \
&& mkdir -p $APP_PATH

RUN gem install bundler --version "$BUNDLE_VERSION" \
&& rm -rf $GEM_HOME/cache/*

COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

COPY Gemfile Gemfile.lock ./
RUN bundle install -j $(nproc) --retry=4

COPY . ./var/app/

RUN bundle exec rails assets:precompile

# navigate to app directory
WORKDIR $APP_PATH

EXPOSE $RAILS_PORT

ENTRYPOINT [ "bundle", "exec" ]
39 changes: 39 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
FROM ruby:2.7.2-alpine

ENV APP_PATH /var/app
ENV BUNDLE_VERSION 2.2.10
ENV BUNDLE_PATH /usr/local/bundle/gems
ENV TMP_PATH /tmp/
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_PORT 3000

# copy entrypoint scripts and grant execution permissions
COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh
COPY ./test-docker-entrypoint.sh /usr/local/bin/test-entrypoint.sh
RUN chmod +x /usr/local/bin/dev-entrypoint.sh && chmod +x /usr/local/bin/test-entrypoint.sh

# install dependencies for application
RUN apk -U add --no-cache \
build-base \
git \
postgresql-dev \
postgresql-client \
libxml2-dev \
libxslt-dev \
nodejs \
yarn \
imagemagick \
tzdata \
less \
&& rm -rf /var/cache/apk/* \
&& mkdir -p $APP_PATH

RUN gem install bundler --version "$BUNDLE_VERSION" \
&& rm -rf $GEM_HOME/cache/*

# navigate to app directory
WORKDIR $APP_PATH

EXPOSE $RAILS_PORT

ENTRYPOINT [ "bundle", "exec" ]
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gem 'pg', '~> 1.1'
gem 'puma', '~> 5.0'
gem 'rails', '~> 6.1.3'
gem 'redis', '~> 4.0'
gem 'redis-namespace'
gem 'sass-rails', '>= 6'
gem 'turbolinks', '~> 5'
gem 'webpacker', '~> 5.0'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ GEM
rb-inotify (0.10.1)
ffi (~> 1.0)
redis (4.2.5)
redis-namespace (1.8.1)
redis (>= 3.0.4)
regexp_parser (2.0.3)
responders (3.0.1)
actionpack (>= 5.0)
Expand Down Expand Up @@ -310,6 +312,7 @@ DEPENDENCIES
rack-mini-profiler (~> 2.0)
rails (~> 6.1.3)
redis (~> 4.0)
redis-namespace
rspec-rails
rubocop
rubocop-performance
Expand Down
69 changes: 69 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# https://stackoverflow.com/a/14061796/2237879
#
# This hack allows you to run make commands with any set of arguments.
#
# For example, these lines are the same:
# > make g devise:install
# > bundle exec rails generate devise:install
# And these:
# > make add-migration add_deleted_at_to_users deleted_at:datetime
# > bundle exec rails g migration add_deleted_at_to_users deleted_at:datetime
# And these:
# > make add-model Order user:references record:references{polymorphic}
# > bundle exec rails g model Order user:references record:references{polymorphic}
#
RUN_ARGS := $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS))

add-migration:
docker-compose run --rm detonate_app rails g migration $(RUN_ARGS)

add-model:
docker-compose run --rm detonate_app rails g model $(RUN_ARGS)

db-create:
docker-compose run --rm detonate_app rails db:create

db-migrate:
docker-compose run --rm detonate_app rails db:migrate

db-rollback:
docker-compose run --rm detonate_app rails db:rollback

lint-ruby:
docker-compose run --rm detonate_app rubocop -a

lint-security:
docker-compose run --rm detonate_app brakeman

ci:
docker-compose run --rm detonate_test rspec

run-console:
docker-compose run --rm detonate_app rails console

run-generate:
docker-compose run --rm detonate_app rails generate $(RUN_ARGS)

run-rails:
docker-compose up detonate_app

run-sidekiq:
docker-compose up detonate_worker

run:
docker-compose up

build:
docker-compose build

down:
docker-compose down

kill:
docker-compose kill

c: run-console

g: run-generate

s: run
36 changes: 4 additions & 32 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
default: &default
port: 5432
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= ENV['DATABASE_URL'] %>

development:
<<: *default
database: detonate_development
username: postgres
host: localhost
port: 5432
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
host: detonate_db
min_messages: notice

test:
<<: *default
username: "postgres"
password: ""
database: detonate_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: detonate_production
username: detonate
password: <%= ENV['DETONATE_DATABASE_PASSWORD'] %>
16 changes: 8 additions & 8 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# frozen_string_literal: true

schedule_file = "config/schedule.yml"
schedule_file = 'config/schedule.yml'

if File.exist?(schedule_file) && Sidekiq.server?
Sidekiq::Cron::Job.load_from_hash(YAML.load_file(schedule_file))
end

# Sidekiq.configure_server do |config|
# config.redis = { url: 'redis://localhost:6379/0', namespace: "app3_sidekiq_#{Rails.env}" }
# end
Sidekiq.configure_server do |config|
config.redis = { namespace: "detonate_sidekiq_#{Rails.env}" }
end

# Sidekiq.configure_client do |config|
# config.redis = { url: 'redis://localhost:6379/0', namespace: "app3_sidekiq_#{Rails.env}" }
# end
Sidekiq.configure_client do |config|
config.redis = { namespace: "detonate_sidekiq_#{Rails.env}" }
end

Sidekiq::Extensions.enable_delay!
Sidekiq::Extensions.enable_delay!
15 changes: 15 additions & 0 deletions dev-docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

set -e

echo "Environment: $RAILS_ENV"
echo "DATABASE_URL: $DATABASE_URL"

# install missing gems
bundle check || bundle install --jobs 20 --retry 5

# Remove pre-existing puma/passenger server.pid
rm -f $APP_PATH/tmp/pids/server.pid || true

# run passed commands
bundle exec ${@}
Loading

0 comments on commit 4d755a4

Please sign in to comment.