From 2ddc155dec6b39d8d17a9843225bf143d1b93d71 Mon Sep 17 00:00:00 2001 From: Aaro Koivukangas Date: Tue, 5 Mar 2024 10:02:56 +0200 Subject: [PATCH] changed selfhost to use better dockerfile + bugfixed entrypoint + seeds --- Dockerfile.self-host | 75 ++++++++++++++++++++++++++++++++++++++------ db/seeds.rb | 19 ++++++----- docker-entrypoint.sh | 2 +- 3 files changed, 79 insertions(+), 17 deletions(-) diff --git a/Dockerfile.self-host b/Dockerfile.self-host index a503b67..a3e8f1e 100644 --- a/Dockerfile.self-host +++ b/Dockerfile.self-host @@ -1,15 +1,72 @@ -FROM ruby:3.1.2 +# syntax = docker/dockerfile:1 -ENV PORT 3000 -EXPOSE $PORT +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile +ARG RUBY_VERSION=3.1.4 +FROM ruby:$RUBY_VERSION-slim as base -WORKDIR /home/app +LABEL fly_launch_runtime="rails" -RUN apt-get update && apt-get install -y npm && npm install -g yarn +# Rails app lives here +WORKDIR /rails -COPY Gemfile Gemfile.lock ./ -RUN bundle install -COPY . . +# Set production environment +ENV RAILS_ENV="production" \ + BUNDLE_WITHOUT="development:test" \ + BUNDLE_DEPLOYMENT="1" -ENTRYPOINT ["docker-entrypoint.sh"] +# 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 libpq-dev pkg-config libc6 + +# Install application gems +COPY --link 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 --link . . + +# 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 ./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 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 /rails /rails + +# Run and own only the runtime files as a non-root user for security +RUN useradd rails --create-home --shell /bin/bash && \ + chown -R rails:rails db log storage tmp public +USER rails:rails + +# Deployment options +ENV RAILS_LOG_TO_STDOUT="1" \ + RAILS_SERVE_STATIC_FILES="true" + + +# Entrypoint prepares the database. +ENTRYPOINT ["/rails/docker-entrypoint.sh"] + +# Start the server by default, this can be overwritten at runtime +EXPOSE 3000 CMD ["rails", "server", "-e", "production", "-p", "3000"] diff --git a/db/seeds.rb b/db/seeds.rb index cd15204..f81d78f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -10,10 +10,15 @@ b1 = Brewery.create name: "Koff", year: 1897 b2 = Brewery.create name: "Malmgard", year: 2001 b3 = Brewery.create name: "Weihenstephaner", year: 1040 -b1.beers.create name: "Iso 3", style: "Lager" -b1.beers.create name: "Karhu", style: "Lager" -b1.beers.create name: "Tuplahumala", style: "Lager" -b2.beers.create name: "Huvila Pale Ale", style: "Pale Ale" -b2.beers.create name: "X Porter", style: "Porter" -b3.beers.create name: "Hefeweizen", style: "Weizen" -b3.beers.create name: "Helles", style: "Lager" +s1 = Style.create name: "Lager" +s2 = Style.create name: "Pale Ale" +s3 = Style.create name: "Portner" +s4 = Style.create name: "Weizen" + +b1.beers.create name: "Iso 3", style: s1 +b1.beers.create name: "Karhu", style: s1 +b1.beers.create name: "Tuplahumala", style: s1 +b2.beers.create name: "Huvila Pale Ale", style: s2 +b2.beers.create name: "X Porter", style: s3 +b3.beers.create name: "Hefeweizen", style: s4 +b3.beers.create name: "Helles", style: s1 diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 7e611b3..aa16870 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -5,7 +5,7 @@ if [ -f tmp/pids/server.pid ]; then rm tmp/pids/server.pid fi -bundle exec rails assets:precompile +bundle exec rails db:create bundle exec rails db:migrate bundle exec rails db:seed