Production Build Working
This commit is contained in:
+40
-39
@@ -1,36 +1,39 @@
|
||||
# syntax = docker/dockerfile:1
|
||||
|
||||
# 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
|
||||
# 1. Base Stage: Common dependencies
|
||||
ARG RUBY_VERSION=3.3.1
|
||||
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
|
||||
FROM ruby:$RUBY_VERSION-slim as base
|
||||
|
||||
# Rails app lives here
|
||||
WORKDIR app
|
||||
|
||||
# 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
|
||||
WORKDIR /rails
|
||||
ENV RAILS_ENV="production" \
|
||||
BUNDLE_DEPLOYMENT="1" \
|
||||
BUNDLE_PATH="/usr/local/bundle" \
|
||||
BUNDLE_WITHOUT="development"
|
||||
BUNDLE_WITHOUT="development:test"
|
||||
|
||||
# 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 && \
|
||||
apt-get install --no-install-recommends -y \
|
||||
build-essential \
|
||||
curl \
|
||||
git \
|
||||
libvips \
|
||||
freetds-bin \
|
||||
freetds-dev \
|
||||
libjemalloc2 \
|
||||
ruby-dev \
|
||||
dos2unix && \
|
||||
rm -rf /var/lib/apt/lists /var/cache/apt/archives
|
||||
|
||||
# 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 && \
|
||||
@@ -39,28 +42,26 @@ RUN bundle install && \
|
||||
# Copy application code
|
||||
COPY . .
|
||||
|
||||
# Precompile bootsnap code for faster boot times
|
||||
RUN bundle exec bootsnap precompile app/ lib/
|
||||
RUN bundle exec bootsnap precompile app/
|
||||
|
||||
|
||||
|
||||
|
||||
# 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 --from=build /app /app
|
||||
# Copy built artifacts: gems and precompiled assets
|
||||
# COPY --from=build /rails/public /rails/public
|
||||
# COPY --from=build /rails/app/javascript /rails/app/javascript
|
||||
# COPY --from=build /rails/config/importmap.rb /rails/config/importmap.rb
|
||||
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 ["/app/bin/docker-entrypoint"]
|
||||
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
|
||||
|
||||
# Start the server by default, this can be overwritten at runtime
|
||||
EXPOSE 3000
|
||||
CMD ["./bin/rails", "server"]
|
||||
# Start the server
|
||||
EXPOSE 3005
|
||||
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
|
||||
|
||||
Reference in New Issue
Block a user