Skip to content

Commit b8a37cb

Browse files
authored
Merge pull request #121 from fly-apps/120-build-procfile-for-solidqsqlite3-combination
Build procfile for solidq/sqlite3 combination
2 parents 0cc74a3 + a275d82 commit b8a37cb

File tree

9 files changed

+150
-6
lines changed

9 files changed

+150
-6
lines changed

lib/generators/dockerfile_generator.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,23 +1212,29 @@ def dbprep_command
12121212

12131213
def procfile
12141214
if using_passenger?
1215-
{
1215+
base = {
12161216
nginx: "nginx"
12171217
}
12181218
elsif options.nginx?
1219-
{
1219+
base = {
12201220
nginx: '/usr/sbin/nginx -g "daemon off;"',
12211221
rails: "./bin/rails server -p 3001"
12221222
}
12231223
elsif using_thruster?
1224-
{
1224+
base = {
12251225
rails: "bundle exec thrust ./bin/rails server"
12261226
}
12271227
else
1228-
{
1228+
base = {
12291229
rails: "./bin/rails server"
12301230
}
12311231
end
1232+
1233+
if using_solidq? && (deploy_database == "sqlite3")
1234+
base.merge(solidq: "bundle exec rake solid_queue:start")
1235+
else
1236+
base
1237+
end
12321238
end
12331239

12341240
def using_thruster?
@@ -1248,7 +1254,11 @@ def fly_processes
12481254
if using_sidekiq?
12491255
list["sidekiq"] = "bundle exec sidekiq"
12501256
elsif using_solidq?
1251-
list["solidq"] = "bundle exec rake solid_queue:start"
1257+
if deploy_database == "sqlite3"
1258+
return if list.size <= 1
1259+
else
1260+
list["solidq"] = "bundle exec rake solid_queue:start"
1261+
end
12521262
end
12531263

12541264
list
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# syntax = docker/dockerfile:1
2+
3+
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
4+
ARG RUBY_VERSION=xxx
5+
FROM ruby:$RUBY_VERSION-slim AS base
6+
7+
# Rails app lives here
8+
WORKDIR /rails
9+
10+
# Set production environment
11+
ENV BUNDLE_DEPLOYMENT="1" \
12+
BUNDLE_PATH="/usr/local/bundle" \
13+
BUNDLE_WITHOUT="development:test" \
14+
RAILS_ENV="production"
15+
16+
# Update gems and bundler
17+
RUN gem update --system --no-document && \
18+
gem install -N bundler
19+
20+
21+
# Throw-away build stage to reduce size of final image
22+
FROM base AS build
23+
24+
# Install packages needed to build gems
25+
RUN apt-get update -qq && \
26+
apt-get install --no-install-recommends -y build-essential pkg-config
27+
28+
# Install application gems
29+
COPY Gemfile Gemfile.lock ./
30+
RUN bundle install && \
31+
bundle exec bootsnap precompile --gemfile && \
32+
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
33+
34+
# Copy application code
35+
COPY . .
36+
37+
# Precompile bootsnap code for faster boot times
38+
RUN bundle exec bootsnap precompile app/ lib/
39+
40+
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
41+
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
42+
43+
44+
# Final stage for app image
45+
FROM base
46+
47+
# Install packages needed for deployment
48+
RUN apt-get update -qq && \
49+
apt-get install --no-install-recommends -y curl libsqlite3-0 ruby-foreman && \
50+
rm -rf /var/lib/apt/lists /var/cache/apt/archives
51+
52+
RUN gem install foreman
53+
54+
# Copy built artifacts: gems, application
55+
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
56+
COPY --from=build /rails /rails
57+
58+
# Run and own only the runtime files as a non-root user for security
59+
ARG UID=xxx \
60+
GID=1000
61+
RUN groupadd -f -g $GID rails && \
62+
useradd -u $UID -g $GID rails --create-home --shell /bin/bash && \
63+
mkdir /data && \
64+
chown -R rails:rails db log storage tmp /data
65+
USER rails:rails
66+
67+
# Deployment options
68+
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
69+
70+
# Entrypoint prepares the database.
71+
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
72+
73+
# Build a Procfile for production use
74+
COPY <<-"EOF" /rails/Procfile.prod
75+
rails: ./bin/rails server
76+
solidq: bundle exec rake solid_queue:start
77+
EOF
78+
79+
# Start the server by default, this can be overwritten at runtime
80+
EXPOSE 3000
81+
VOLUME /data
82+
CMD ["foreman", "start", "--procfile=Procfile.prod"]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: "3.8"
2+
3+
services:
4+
web:
5+
build:
6+
context: .
7+
args:
8+
UID: ${UID:-1000}
9+
GID: ${GID:-${UID:-1000}}
10+
volumes:
11+
- ./log:/rails/log
12+
- ./storage:/rails/storage
13+
ports:
14+
- "3000:3000"
15+
environment:
16+
secrets:
17+
- source: master_key
18+
target: /rails/config/master.key
19+
20+
secrets:
21+
master_key:
22+
file: ./config/master.key
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[mounts]
2+
source="data"
3+
destination="/data"
4+
5+
[[statics]]
6+
guest_path = "/rails/public"
7+
url_prefix = "/"
8+

test/test_solidq.rb renamed to test/test_solidq_postgres.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
require_relative "base"
66

7-
class TestSolidQueue < TestBase
7+
class TestSolidQueuePostgres < TestBase
88
@rails_options = "--database=postgresql"
99
@generate_options = "--compose"
1010

test/test_solidq_sqlite3.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
require "fileutils"
4+
5+
require_relative "base"
6+
7+
class TestSolidQueueSqlite3 < TestBase
8+
@rails_options = "--database=sqlite3"
9+
@generate_options = "--compose"
10+
11+
def app_setup
12+
system "bundle add solid_queue"
13+
FileUtils.touch "fly.toml"
14+
IO.write "app/jobs/DummyJob.rb", "class DummyJob < ApplicationJob; end"
15+
end
16+
17+
def test_solidq
18+
check_dockerfile
19+
check_toml
20+
check_compose
21+
end
22+
end

0 commit comments

Comments
 (0)