Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bench/.kamal/secrets
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MYSQL_ROOT_PASSWORD=secret
POSTGRES_PASSWORD=secret
RAILS_MASTER_KEY=$(cat config/master.key)
HOST=$HOST
2 changes: 1 addition & 1 deletion bench/.ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.5
4.0.6
89 changes: 43 additions & 46 deletions bench/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,68 +1,65 @@
# syntax = docker/dockerfile:1
# 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
ARG RUBY_VERSION
FROM rubylang/ruby:$RUBY_VERSION-noble AS base

# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.3.5
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 postgresql-client && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
<<EOF
apt-get update -qq
apt-get install --no-install-recommends -y libjemalloc2 sqlite3
ln -s /usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2 /usr/local/lib/libjemalloc.so
rm -rf /var/lib/apt/lists /var/cache/apt/archives
EOF

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_WITHOUT="development:test:linters:deploy" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
BUNDLE_WITHOUT="development:test" \
LD_PRELOAD="/usr/local/lib/libjemalloc.so" \
RUBYOPT="--enable-frozen-string-literal"


# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git pkg-config libpq-dev && \
apt-get clean && rm -rf /var/cache/apt/archives /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libssl-dev libyaml-dev pkg-config && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install application gems
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
COPY --link Gemfile Gemfile.lock ./
RUN --mount=type=cache,target=/usr/local/bundle/cache \
<<EOF
bundle install
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
EOF

# Copy application code
COPY . .
COPY --link . .

RUN <<EOF
SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
rm -rf app/assets
EOF

RUN bundle exec bootsnap precompile app/ lib/ && \
SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile && \
rm -rf vendor/ruby/3.3.0/cache

# Final stage for app image
FROM base

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails
COPY --link --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --link --from=build /rails /rails

RUN <<EOF
mkdir -p /data /rails/storage /hist
chown -R ubuntu:ubuntu /data /hist /rails/db /rails/log /rails/storage /rails/tmp
EOF

# 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 && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER 1000:1000
USER ubuntu

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
VOLUME /data
VOLUME /hist
CMD ["./bin/thrust", "./bin/rails", "server"]
2 changes: 1 addition & 1 deletion bench/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ gem "importmap-rails"
gem "redis"
gem "bootsnap", require: false
gem "kamal", require: false
gem "tailwindcss-rails"
gem "thruster"
Loading