Skip to content

Commit

Permalink
Build procfile for solidq/sqlite3 combination
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Oct 19, 2024
1 parent 71f7fcc commit 04dd4bf
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 6 deletions.
20 changes: 15 additions & 5 deletions lib/generators/dockerfile_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1212,23 +1212,29 @@ def dbprep_command

def procfile
if using_passenger?
{
base = {
nginx: "nginx"
}
elsif options.nginx?
{
base = {
nginx: '/usr/sbin/nginx -g "daemon off;"',
rails: "./bin/rails server -p 3001"
}
elsif using_thruster?
{
base = {
rails: "bundle exec thrust ./bin/rails server"
}
else
{
base = {
rails: "./bin/rails server"
}
end

if using_solidq? and deploy_database == "sqlite3"
base.merge(solidq: "bundle exec rake solid_queue:start")
else
base
end
end

def using_thruster?
Expand All @@ -1248,7 +1254,11 @@ def fly_processes
if using_sidekiq?
list["sidekiq"] = "bundle exec sidekiq"
elsif using_solidq?
list["solidq"] = "bundle exec rake solid_queue:start"
if deploy_database == "sqlite3"
return if list.size <= 1
else
list["solidq"] = "bundle exec rake solid_queue:start"
end
end

list
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
82 changes: 82 additions & 0 deletions test/results/solid_queue_sqlite3/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=xxx
FROM ruby:$RUBY_VERSION-slim AS base

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development:test" \
RAILS_ENV="production"

# Update gems and bundler
RUN gem update --system --no-document && \
gem install -N bundler


# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential pkg-config

# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
bundle exec bootsnap precompile --gemfile && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git

# Copy application code
COPY . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile


# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libsqlite3-0 ruby-foreman && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

RUN gem install foreman

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
ARG UID=xxx \
GID=1000
RUN groupadd -f -g $GID rails && \
useradd -u $UID -g $GID rails --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Build a Procfile for production use
COPY <<-"EOF" /rails/Procfile.prod
rails: ./bin/rails server
solidq: bundle exec rake solid_queue:start
EOF

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
VOLUME /data
CMD ["foreman", "start", "--procfile=Procfile.prod"]
22 changes: 22 additions & 0 deletions test/results/solid_queue_sqlite3/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "3.8"

services:
web:
build:
context: .
args:
UID: ${UID:-1000}
GID: ${GID:-${UID:-1000}}
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
secrets:
- source: master_key
target: /rails/config/master.key

secrets:
master_key:
file: ./config/master.key
8 changes: 8 additions & 0 deletions test/results/solid_queue_sqlite3/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[mounts]
source="data"
destination="/data"

[[statics]]
guest_path = "/rails/public"
url_prefix = "/"

2 changes: 1 addition & 1 deletion test/test_solidq.rb → test/test_solidq_postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require_relative "base"

class TestSolidQueue < TestBase
class TestSolidQueuePostgres < TestBase
@rails_options = "--database=postgresql"
@generate_options = "--compose"

Expand Down
22 changes: 22 additions & 0 deletions test/test_solidq_sqlite3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "fileutils"

require_relative "base"

class TestSolidQueueSqlite3 < TestBase
@rails_options = "--database=sqlite3"
@generate_options = "--compose"

def app_setup
system "bundle add solid_queue"
FileUtils.touch "fly.toml"
IO.write "app/jobs/DummyJob.rb", "class DummyJob < ApplicationJob; end"
end

def test_solidq
check_dockerfile
check_toml
check_compose
end
end

0 comments on commit 04dd4bf

Please sign in to comment.