|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | +# check=error=true |
| 3 | + |
| 4 | +# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand: |
| 5 | +# docker build -t railsflix . |
| 6 | +# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name railsflix railsflix |
| 7 | + |
| 8 | +# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html |
| 9 | + |
| 10 | +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version |
| 11 | +ARG RUBY_VERSION=3.3.6 |
| 12 | +FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base |
| 13 | + |
| 14 | +# Rails app lives here |
| 15 | +WORKDIR /rails |
| 16 | + |
| 17 | +# Install base packages |
| 18 | +RUN apt-get update -qq && \ |
| 19 | + apt-get install --no-install-recommends -y curl libjemalloc2 libvips cron && \ |
| 20 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 21 | + |
| 22 | +RUN curl -o /etc/apt/trusted.gpg.d/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc && \ |
| 23 | + echo "deb http://apt.postgresql.org/pub/repos/apt/ bookworm-pgdg main 16" > /etc/apt/sources.list.d/pgdg.list && \ |
| 24 | + apt-get update -qq && \ |
| 25 | + apt-get install --no-install-recommends -y postgresql-client-16 |
| 26 | + |
| 27 | +# Set production environment |
| 28 | +ENV RAILS_ENV="production" \ |
| 29 | + BUNDLE_DEPLOYMENT="1" \ |
| 30 | + BUNDLE_PATH="/usr/local/bundle" \ |
| 31 | + BUNDLE_WITHOUT="development" |
| 32 | + |
| 33 | +# Throw-away build stage to reduce size of final image |
| 34 | +FROM base AS build |
| 35 | + |
| 36 | +# Install packages needed to build gems |
| 37 | +RUN apt-get update -qq && \ |
| 38 | + apt-get install --no-install-recommends -y build-essential git pkg-config libpq-dev node-gyp python-is-python3 && \ |
| 39 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 40 | + |
| 41 | +# Install JavaScript dependencies |
| 42 | +ARG NODE_VERSION=22.9.0 |
| 43 | +ARG YARN_VERSION=1.22.22 |
| 44 | +ENV PATH=/usr/local/node/bin:$PATH |
| 45 | +RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \ |
| 46 | + /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \ |
| 47 | + npm install -g yarn@$YARN_VERSION && \ |
| 48 | + rm -rf /tmp/node-build-master |
| 49 | + |
| 50 | +# Install application gems |
| 51 | +COPY Gemfile Gemfile.lock ./ |
| 52 | +RUN bundle install && \ |
| 53 | + rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ |
| 54 | + bundle exec bootsnap precompile --gemfile |
| 55 | + |
| 56 | +# Install node modules |
| 57 | +COPY package.json yarn.lock ./ |
| 58 | +RUN yarn install --frozen-lockfile |
| 59 | + |
| 60 | +# Copy application code |
| 61 | +COPY . . |
| 62 | + |
| 63 | +# Precompile bootsnap code for faster boot times |
| 64 | +RUN bundle exec bootsnap precompile app/ lib/ |
| 65 | + |
| 66 | +# Precompiling assets for production without requiring secret RAILS_MASTER_KEY |
| 67 | +RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile |
| 68 | + |
| 69 | +RUN rm -rf node_modules |
| 70 | + |
| 71 | + |
| 72 | +# Final stage for app image |
| 73 | +FROM base |
| 74 | + |
| 75 | +# Copy built artifacts: gems, application |
| 76 | +COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" |
| 77 | +COPY --from=build /rails /rails |
| 78 | + |
| 79 | +# Run and own only the runtime files as a non-root user for security |
| 80 | +RUN groupadd --system --gid 1000 rails && \ |
| 81 | + useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ |
| 82 | + chown -R rails:rails db log storage tmp && \ |
| 83 | + chown rails:rails /etc/environment && \ |
| 84 | + chmod gu+rw /var/run && \ |
| 85 | + chmod gu+s /usr/sbin/cron |
| 86 | +USER 1000:1000 |
| 87 | + |
| 88 | +# Entrypoint prepares the database. |
| 89 | +ENTRYPOINT ["/rails/bin/docker-entrypoint"] |
| 90 | + |
| 91 | +# Start server via Thruster by default, this can be overwritten at runtime |
| 92 | +EXPOSE 80 |
| 93 | +CMD ["./bin/thrust", "./bin/rails", "server"] |
0 commit comments