Skip to content

Commit

Permalink
switch to sqlite3, added docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
damianlegawiec committed Sep 12, 2024
1 parent cd4c148 commit 0fde257
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 200 deletions.
45 changes: 22 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,62 +1,61 @@
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t my-app .
# docker run -d -p 80:80 -p 443:443 --name my-app -e RAILS_MASTER_KEY=<value from config/master.key> my-app

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.3.0
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 sqlite3 libvips && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

ENV RAILS_ENV="development" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle"

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

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libpq-dev libvips pkg-config
apt-get install --no-install-recommends -y build-essential git pkg-config && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
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 libvips postgresql-client && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
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
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
USER rails:rails
USER 1000:1000

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

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
CMD ["./bin/rails", "server", "-b", "0.0.0.0"]
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ gem "rails", "~> 7.1.4"

# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
gem "mini_racer" # fixes Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable) in Docker env
gem "mini_racer", platforms: %i[ ruby jruby ] # fixes Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable) in Docker env

# Use pg as the database for Active Record
gem "pg", "~> 1.5"
# Use sqlite3 as the database for Active Record
gem "sqlite3", ">= 1.4"

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", ">= 5.0"
Expand All @@ -28,7 +28,7 @@ gem "stimulus-rails"
gem "jbuilder"

# Use Redis adapter to run Action Cable in production
gem "redis", ">= 4.0.1", group: :production
# gem "redis", ">= 4.0.1", group: :production

# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"
Expand Down
14 changes: 7 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ GEM
orm_adapter (0.5.0)
paranoia (3.0.0)
activerecord (>= 6, < 8.1)
pg (1.5.7)
popper_js (1.16.1)
psych (5.1.2)
stringio
Expand Down Expand Up @@ -348,10 +347,6 @@ GEM
i18n
rdoc (6.7.0)
psych (>= 4.0.0)
redis (5.3.0)
redis-client (>= 0.22.0)
redis-client (0.22.2)
connection_pool
regexp_parser (2.9.2)
reline (0.5.10)
io-console (~> 0.5)
Expand Down Expand Up @@ -507,6 +502,12 @@ GEM
actionpack (>= 6.1)
activesupport (>= 6.1)
sprockets (>= 3.0.0)
sqlite3 (2.0.4-aarch64-linux-gnu)
sqlite3 (2.0.4-arm-linux-gnu)
sqlite3 (2.0.4-arm64-darwin)
sqlite3 (2.0.4-x86-linux-gnu)
sqlite3 (2.0.4-x86_64-darwin)
sqlite3 (2.0.4-x86_64-linux-gnu)
state_machines (0.6.0)
state_machines-activemodel (0.9.0)
activemodel (>= 6.0)
Expand Down Expand Up @@ -567,10 +568,8 @@ DEPENDENCIES
jbuilder
mini_racer
mission_control-jobs
pg (~> 1.5)
puma (>= 5.0)
rails (~> 7.1.4)
redis (>= 4.0.1)
sassc!
selenium-webdriver
solid_cache
Expand All @@ -584,6 +583,7 @@ DEPENDENCIES
spree_i18n
spree_sample
sprockets-rails
sqlite3 (>= 1.4)
stimulus-rails
turbo-rails
tzinfo-data
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ If you want to use sample data (products, categories), you can load it using the
bin/rake spree_sample:load
```

#### Full docker setup

If you're having trouble with Ruby/etc setup and just want to get everything up and running, you can use the following command to start the docker containers.

```bash
docker pull vendoconnect/spree_starter
docker run -p 3000:3000 vendoconnect/spree_starter
```

This will start the docker containers and automatically start the rails server.

To sign in into the admin dashboard go to `https://localhost:3000/admin` and use the following credentials:

```bash
Email: [email protected]
Password: spree123
```

### Switching to PostgreSQL

```bash
bin/rails db:system:change --to=postgresql
```

### Switching to MySQL

By default, Spree Starter uses PostgreSQL. If you want to switch to MySQL, you can do so by running the following command:
Expand Down
2 changes: 1 addition & 1 deletion bin/docker-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# If running the rails server then create or migrate existing database
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
./bin/rails db:prepare
AUTO_ACCEPT=1 ./bin/rails db:prepare
fi

exec "${@}"
3 changes: 0 additions & 3 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ FileUtils.chdir APP_ROOT do
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
# Add necessary setup steps to this file.

puts "== Setup PostgreSQL and Redis =="
system! "docker-compose up -d"

puts "== Installing dependencies =="
system! "gem install bundler --conservative"
system("bundle check") || system!("bundle install")
Expand Down
79 changes: 9 additions & 70 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,86 +1,25 @@
# PostgreSQL. Versions 9.3 and up are supported.
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem "sqlite3"
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
<<: *default
database: spree_starter_development
host: <%= ENV.fetch("DB_HOST", "localhost") %>
username: <%= ENV.fetch("DB_USERNAME", "postgres") %>

# The specified database role being used to connect to PostgreSQL.
# To create additional roles in PostgreSQL see `$ createuser --help`.
# When left blank, PostgreSQL will use the default role. This is
# the same name as the operating system user running Rails.
#username: spree_starter

# The password associated with the PostgreSQL role (username).
#password:

# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost

# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432

# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public

# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
database: storage/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: spree_starter_test
host: <%= ENV.fetch("DB_HOST", "localhost") %>
username: <%= ENV.fetch("DB_USERNAME", "postgres") %>
database: storage/test.sqlite3

# 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
url: <%= ENV["DATABASE_URL"] %>
database: storage/production.sqlite3
Loading

0 comments on commit 0fde257

Please sign in to comment.