Prod build process
This commit is contained in:
+46
-45
@@ -1,73 +1,74 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
# check=error=true
|
||||
# syntax = docker/dockerfile:1
|
||||
|
||||
# This Dockerfile is designed for production, not development.
|
||||
# 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
|
||||
# 1. Base Stage: Common dependencies
|
||||
ARG RUBY_VERSION=3.4.8
|
||||
FROM ruby:$RUBY_VERSION-slim as base
|
||||
|
||||
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
|
||||
ARG RUBY_VERSION=3.3.9
|
||||
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
|
||||
|
||||
# Rails app lives here
|
||||
WORKDIR /rails
|
||||
|
||||
# Install base packages
|
||||
RUN apt-get update -qq && \
|
||||
apt-get install --no-install-recommends -y curl libjemalloc2 libvips postgresql-client && \
|
||||
rm -rf /var/lib/apt/lists /var/cache/apt/archives
|
||||
|
||||
# Set production environment
|
||||
ENV RAILS_ENV="production" \
|
||||
BUNDLE_DEPLOYMENT="1" \
|
||||
BUNDLE_PATH="/usr/local/bundle" \
|
||||
BUNDLE_WITHOUT="development"
|
||||
BUNDLE_WITHOUT="development:test"
|
||||
# SUPERCRONIC_URL=https://github.com \
|
||||
# SUPERCRONIC=supercronic-linux-amd64 \
|
||||
# SUPERCRONIC_SHA1SUM=cd48d45327f3f3396734267468f707f43372c2fc
|
||||
|
||||
# Throw-away build stage to reduce size of final image
|
||||
FROM base AS build
|
||||
|
||||
# Install packages needed to build gems
|
||||
# Install base packages (libvips for Active Storage, curl for healthchecks)
|
||||
RUN apt-get update -qq && \
|
||||
apt-get install --no-install-recommends -y build-essential git libpq-dev pkg-config libyaml-dev && \
|
||||
apt-get install --no-install-recommends -y \
|
||||
curl \
|
||||
git \
|
||||
libvips \
|
||||
freetds-bin \
|
||||
freetds-dev \
|
||||
libpq-dev \
|
||||
libyaml-dev \
|
||||
cron \
|
||||
libjemalloc2 \
|
||||
dos2unix && \
|
||||
rm -rf /var/lib/apt/lists /var/cache/apt/archives
|
||||
|
||||
# RUN curl -fsSLO "$SUPERCRONIC_URL" \
|
||||
# && echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
|
||||
# && chmod +x "$SUPERCRONIC" \
|
||||
# && mv "$SUPERCRONIC" /usr/local/bin/supercronic
|
||||
|
||||
# 2. Build Stage: Gems and Assets
|
||||
FROM base as build
|
||||
|
||||
# Install packages needed to build gems and precompile assets
|
||||
RUN apt-get update -qq && \
|
||||
apt-get install --no-install-recommends -y build-essential pkg-config less && \
|
||||
rm -rf /var/lib/apt/lists /var/cache/apt/archives
|
||||
|
||||
# Install application gems
|
||||
RUN bundle config set --local frozen false
|
||||
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
|
||||
# Precompile assets (Tailwind is triggered here via assets:precompile)
|
||||
# SECRET_KEY_BASE_DUMMY allows precompilation without real secrets
|
||||
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
|
||||
|
||||
|
||||
# Final stage for app image
|
||||
# 3. Final Stage: Lean Runtime
|
||||
FROM base
|
||||
|
||||
# Copy built artifacts: gems, application
|
||||
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
|
||||
# Copy built artifacts: gems and precompiled assets
|
||||
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 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 1000:1000
|
||||
# Run as a non-privileged user for security
|
||||
RUN useradd -ms /bin/bash rails
|
||||
RUN chown -R rails:rails /rails
|
||||
USER rails:rails
|
||||
|
||||
# Entrypoint prepares the database.
|
||||
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
|
||||
|
||||
HEALTHCHECK --interval=15s --timeout=3s --start-period=0s --start-interval=5s --retries=3 \
|
||||
CMD curl -f http://localhost:3000/up || exit 1
|
||||
# Start the server by default, this can be overwritten at runtime
|
||||
EXPOSE 3000
|
||||
|
||||
# The default Rails Dockerfile uses `./bin/rails server`, but when using Puma,
|
||||
# they recommend using bundle exec puma. ref: https://github.com/puma/puma#rails
|
||||
# Start the server
|
||||
EXPOSE 3002
|
||||
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
|
||||
|
||||
Reference in New Issue
Block a user