Created
October 11, 2019 13:08
-
-
Save webchi/6715826afc10fcf90258e320b4db5090 to your computer and use it in GitHub Desktop.
Rails Postgre Docker MultiStage
This file contains 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
FROM ruby:2.5.5-alpine3.9 as builder | |
WORKDIR /app | |
ARG RAILS_MASTER_KEY | |
ENV BUNDLE_SILENCE_ROOT_WARNING=1 \ | |
BUNDLE_IGNORE_MESSAGES=1 \ | |
BUNDLE_GITHUB_HTTPS=1 \ | |
BUNDLE_FROZEN=1 \ | |
BUNDLE_WITHOUT=development:test | |
RUN apk update && apk add --no-cache \ | |
build-base postgresql-dev libxml2-dev libxslt-dev nodejs tzdata git imagemagick | |
COPY Gemfile* ./ | |
RUN gem install bundler && bundle install -j4 --retry 3 | |
# Add the Rails app | |
COPY . /app | |
RUN mv ./config/database.yml.sample ./config/database.yml && \ | |
bundle exec rake assets:precompile RAILS_ENV=production | |
FROM ruby:2.5.5-alpine3.9 | |
ARG RAILS_ENV=production | |
WORKDIR /app | |
COPY --from=builder /usr/local/bundle /usr/local/bundle | |
COPY --from=builder /app ./ | |
RUN apk update && apk add --no-cache \ | |
postgresql-client tzdata nodejs imagemagick && \ | |
addgroup -g 1000 -S appuser && \ | |
adduser -u 1000 -S appuser -G appuser && \ | |
chown appuser:appuser /app -R | |
EXPOSE 9292 | |
USER appuser | |
CMD ["ruby", "init.rb"] |
This file contains 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
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
environment = ENV['RAILS_ENV'] | |
container = ARGV[0] || 'api' | |
case container | |
when 'api' | |
%w[db:create db:migrate].each do |rake_name| | |
puts "-> rake #{rake_name}" | |
exec "bundle exec rails #{rake_name}" unless system "bundle exec rails #{rake_name}" | |
end | |
if %w[stage production].include?(environment) | |
puts '-> update whenever' | |
exec 'bundle exec whenever --update-crontab && cron -f' unless system 'bundle exec whenever' | |
end | |
exec "bundle exec puma -e #{environment}" | |
when 'sidekiq' | |
exec 'bundle exec sidekiq' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment