diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e868919 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +/public/assets +/app/assets/builds diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8f81f42 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,65 @@ +# syntax = docker/dockerfile:1 + +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile +ARG RUBY_VERSION=3.2.2 +FROM registry.docker.com/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" + + +# 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 git libpq-dev libvips pkg-config curl sassc libsass-dev libjemalloc2 zlib1g-dev openssl libssl-dev + +# 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 + + +# Copy application code +COPY . . + +# Precompile bootsnap code for faster boot times +RUN bundle exec bootsnap precompile app/ lib/ + +# Work around a bug in SASSC https://github.com/sass/sassc-ruby/issues/146 +ENV BUNDLE_BUILD__SASSC=--disable-march-tune-native + +RUN RAILS_ENV=production BUILD_STAGE=docker ./bin/rails tailwindcss:build +RUN RAILS_ENV=production BUILD_STAGE=docker ./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 libvips postgresql-client curl && \ + 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 --home /rails --shell /bin/bash && \ + chown -R rails:rails db log storage tmp +USER rails:rails + +# Entrypoint prepares the database. +ENTRYPOINT ["/rails/bin/docker-entrypoint"] + +# Start the server by default, this can be overwritten at runtime +EXPOSE 3000 diff --git a/Gemfile.lock b/Gemfile.lock index 64f7d59..cfdc696 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -161,6 +161,8 @@ GEM net-smtp (0.4.0) net-protocol nio4r (2.5.9) + nokogiri (1.15.4-aarch64-linux) + racc (~> 1.4) nokogiri (1.15.4-arm64-darwin) racc (~> 1.4) pg (1.5.4) @@ -225,7 +227,7 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) websocket (~> 1.0) - sidekiq (7.1.4) + sidekiq (7.1.5) concurrent-ruby (< 2) connection_pool (>= 2.3.0) rack (>= 2.2.4) @@ -240,6 +242,8 @@ GEM stimulus-rails (1.2.2) railties (>= 6.0.0) stringio (3.0.8) + tailwindcss-rails (2.0.30-aarch64-linux) + railties (>= 6.0.0) tailwindcss-rails (2.0.30-arm64-darwin) railties (>= 6.0.0) thor (1.2.2) @@ -269,6 +273,7 @@ GEM zeitwerk (2.6.12) PLATFORMS + aarch64-linux arm64-darwin-22 DEPENDENCIES diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 10eb75a..9dda359 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -21,4 +21,4 @@ .lecture-cancelled { background: repeating-linear-gradient(45deg, rgb(254 226 226/var(--tw-bg-opacity)), rgb(254 226 226/var(--tw-bg-opacity)) 10px, white 10px, white 20px); -} \ No newline at end of file +} diff --git a/bin/docker-entrypoint b/bin/docker-entrypoint new file mode 100644 index 0000000..32e7713 --- /dev/null +++ b/bin/docker-entrypoint @@ -0,0 +1,12 @@ +#!/bin/sh + +if [ $# -eq 0 ]; then + # Create new or migrate existing database + ./bin/rails db:prepare + + # Start the server by default + exec bin/rails server +else + # Allow other commands, like console or runner, to be called + exec bin/rails "$@" +fi