|
| 1 | +# syntax = docker/dockerfile:1 |
| 2 | + |
| 3 | +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile |
| 4 | +ARG RUBY_VERSION=3.1.2 |
| 5 | +FROM ruby:$RUBY_VERSION-slim as base |
| 6 | + |
| 7 | +LABEL fly_launch_runtime="rails" |
| 8 | + |
| 9 | +# Rails app lives here |
| 10 | +WORKDIR /rails |
| 11 | + |
| 12 | +# Set production environment |
| 13 | +ENV RAILS_ENV="production" \ |
| 14 | + BUNDLE_WITHOUT="development:test" |
| 15 | + |
| 16 | +# Update gems and bundler |
| 17 | +RUN gem update --system --no-document && \ |
| 18 | + gem install -N bundler |
| 19 | + |
| 20 | +# Install packages needed to install nodejs |
| 21 | +RUN apt-get update -qq && \ |
| 22 | + apt-get install --no-install-recommends -y curl && \ |
| 23 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 24 | + |
| 25 | +# Install Node.js |
| 26 | +# ARG NODE_VERSION=18.16.0 |
| 27 | +# ENV PATH=/usr/local/node/bin:$PATH |
| 28 | +# RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \ |
| 29 | +# /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \ |
| 30 | +# rm -rf /tmp/node-build-master |
| 31 | + |
| 32 | +# Throw-away build stage to reduce size of final image |
| 33 | +FROM base as build |
| 34 | + |
| 35 | +# Install packages needed to build gems and node modules |
| 36 | +RUN apt-get update -qq && \ |
| 37 | + apt-get install --no-install-recommends -y build-essential default-libmysqlclient-dev git libpq-dev libvips node-gyp pkg-config python-is-python3 |
| 38 | + |
| 39 | +# Build options |
| 40 | +ENV PATH="/usr/local/node/bin:$PATH" |
| 41 | + |
| 42 | +# Install application gems |
| 43 | +COPY --link Gemfile ./ |
| 44 | +RUN sed -i "s/, path: '..\/..\/'//" Gemfile |
| 45 | +RUN bundle install && \ |
| 46 | + rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git |
| 47 | + |
| 48 | +# Install node modules |
| 49 | +# COPY --link package.json ./ |
| 50 | +# RUN npm install |
| 51 | + |
| 52 | +# Copy application code |
| 53 | +COPY --link . . |
| 54 | +RUN sed -i "s/, path: '..\/..\/'//" Gemfile |
| 55 | + |
| 56 | +# Precompiling assets for production without requiring secret RAILS_MASTER_KEY |
| 57 | +RUN sed -i "/link_tree ..\/..\/..\//d" app/assets/config/manifest.js |
| 58 | +RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile |
| 59 | + |
| 60 | + |
| 61 | +# Final stage for app image |
| 62 | +FROM base |
| 63 | + |
| 64 | +# Install packages needed for deployment |
| 65 | +RUN apt-get update -qq && \ |
| 66 | + apt-get install --no-install-recommends -y curl default-mysql-client imagemagick libsqlite3-0 libvips postgresql-client && \ |
| 67 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 68 | + |
| 69 | +# Copy built artifacts: gems, application |
| 70 | +COPY --from=build /usr/local/bundle /usr/local/bundle |
| 71 | +COPY --from=build /rails /rails |
| 72 | + |
| 73 | +# Run and own only the runtime files as a non-root user for security |
| 74 | +RUN useradd rails --create-home --shell /bin/bash && \ |
| 75 | + chown -R rails:rails db log tmp public/system public/uploads |
| 76 | +USER rails:rails |
| 77 | + |
| 78 | +# Deployment options |
| 79 | +ENV RAILS_LOG_TO_STDOUT="1" \ |
| 80 | + RAILS_SERVE_STATIC_FILES="true" |
| 81 | + |
| 82 | +# Entrypoint prepares the database. |
| 83 | +ENTRYPOINT ["/rails/bin/docker-entrypoint"] |
| 84 | + |
| 85 | +# Start the server by default, this can be overwritten at runtime |
| 86 | +EXPOSE 3000 |
| 87 | +CMD ["./bin/rails", "server"] |
0 commit comments