|
| 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 argo_b3 . |
| 6 | +# docker run -d -p 80:80 --name argo_b3 argo_b3 |
| 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.4.2 |
| 12 | +FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base |
| 13 | + |
| 14 | +# Make sure NODE_VERSION matches the Node version in .node-version |
| 15 | +ARG NODE_VERSION=22.12.0 |
| 16 | +ARG YARN_VERSION=1.22.22 |
| 17 | + |
| 18 | +# Rails app lives here |
| 19 | +WORKDIR /rails |
| 20 | + |
| 21 | +# Install base packages |
| 22 | +RUN apt-get update -qq && \ |
| 23 | + apt-get install --no-install-recommends -y curl libjemalloc2 libvips postgresql-client && \ |
| 24 | + ln -s /usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2 /usr/local/lib/libjemalloc.so && \ |
| 25 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 26 | + |
| 27 | +# Install JS runtime and package manager needed by cssbundling-rails (sass/postcss via yarn) |
| 28 | +ENV PATH=/usr/local/node/bin:$PATH |
| 29 | +RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \ |
| 30 | + /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \ |
| 31 | + npm install -g yarn@$YARN_VERSION && \ |
| 32 | + rm -rf /tmp/node-build-master |
| 33 | + |
| 34 | +# Set production environment variables and enable jemalloc for reduced memory usage and latency. |
| 35 | +ENV RAILS_ENV="production" \ |
| 36 | + BUNDLE_DEPLOYMENT="1" \ |
| 37 | + BUNDLE_PATH="/usr/local/bundle" \ |
| 38 | + BUNDLE_WITHOUT="development" \ |
| 39 | + LD_PRELOAD="/usr/local/lib/libjemalloc.so" |
| 40 | + |
| 41 | +# Throw-away build stage to reduce size of final image |
| 42 | +FROM base AS build |
| 43 | + |
| 44 | +# Install packages needed to build gems |
| 45 | +RUN apt-get update -qq && \ |
| 46 | + apt-get install --no-install-recommends -y build-essential git libvips libyaml-dev pkg-config libpq-dev && \ |
| 47 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 48 | + |
| 49 | +# Install application gems |
| 50 | +COPY vendor/* ./vendor/ |
| 51 | +COPY Gemfile Gemfile.lock ./ |
| 52 | + |
| 53 | +RUN bundle install && \ |
| 54 | + rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ |
| 55 | + # -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495 |
| 56 | + bundle exec bootsnap precompile -j 1 --gemfile |
| 57 | + |
| 58 | +# Install node modules |
| 59 | +COPY package.json yarn.lock ./ |
| 60 | +RUN yarn install --frozen-lockfile |
| 61 | + |
| 62 | +# Copy application code |
| 63 | +COPY . . |
| 64 | + |
| 65 | +# Precompile bootsnap code for faster boot times. |
| 66 | +# -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495 |
| 67 | +RUN bundle exec bootsnap precompile -j 1 app/ lib/ |
| 68 | + |
| 69 | +# Precompiling assets for production without requiring secret RAILS_MASTER_KEY |
| 70 | +RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +# Final stage for app image |
| 76 | +FROM base |
| 77 | + |
| 78 | +# Run and own only the runtime files as a non-root user for security. Matches |
| 79 | +# lyberadmin's uid:gid on the Puppet-managed deploy hosts (see config/deploy.qa.yml) |
| 80 | +# so no runtime --user override is needed there. |
| 81 | +RUN groupadd --system --gid 503 lyberadmin && \ |
| 82 | + useradd lyberadmin --uid 503 --gid 503 --create-home --shell /bin/bash |
| 83 | +USER 503:503 |
| 84 | + |
| 85 | +# Copy built artifacts: gems, application |
| 86 | +COPY --chown=lyberadmin:lyberadmin --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" |
| 87 | +COPY --chown=lyberadmin:lyberadmin --from=build /rails /rails |
| 88 | + |
| 89 | +# Entrypoint prepares the database. |
| 90 | +ENTRYPOINT ["/rails/bin/docker-entrypoint"] |
| 91 | + |
| 92 | +# Start server via Thruster by default, this can be overwritten at runtime |
| 93 | +EXPOSE 80 |
| 94 | +CMD ["./bin/thrust", "./bin/rails", "server"] |
0 commit comments