Skip to content
Closed
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
9 changes: 9 additions & 0 deletions lib/generators/templates/Dockerfile.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ FROM prebuild AS <% if using_bun? %>bun<% else %>node<% end %>

<% end -%>
<% if using_bun? and (!using_execjs? || File.exist?('bun.lockb') || File.exist?('bun.lock')) -%>
<% if bun_version -%>
ARG BUN_VERSION=<%= bun_version %>
<% end -%>
<% if options.alpine? -%>
FROM oven/bun:<%= bun_version ? '${BUN_VERSION}' : '1' %>-alpine AS bun
<% else -%>
FROM oven/bun:<%= bun_version ? '${BUN_VERSION}' : '1' %>-debian AS bun
<% end -%>

<%= render partial: 'install_node', locals: {bun_version: using_execjs? ? nil : bun_version} %>

<% elsif using_node? and (!using_execjs? || File.exist?('yarn.lock')) -%>
Expand Down
10 changes: 3 additions & 7 deletions lib/generators/templates/_install_node.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<% if using_bun? -%>
# Install Bun
<% if bun_version -%>
ARG BUN_VERSION=<%= bun_version %>
<% end -%>
ENV BUN_INSTALL=/usr/local/bun
ENV PATH=/usr/local/bun/bin:$PATH
RUN curl -fsSL https://bun.sh/install | bash<% if bun_version %> -s -- "bun-v${BUN_VERSION}"<% end %>
# Copy Bun from official image
COPY --from=bun /usr/local/bin/bun /usr/local/bin/bun
ENV PATH=/usr/local/bin:$PATH
<% else -%>
<% if node_version and yarn_version -%>
# Install JavaScript dependencies
Expand Down
9 changes: 5 additions & 4 deletions test/results/bun/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential libyaml-dev pkg-config unzip && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install Bun
ARG BUN_VERSION=xxx
ENV BUN_INSTALL=/usr/local/bun
ENV PATH=/usr/local/bun/bin:$PATH
RUN curl -fsSL https://bun.sh/install | bash -s -- "bun-v${BUN_VERSION}"
FROM oven/bun:${BUN_VERSION}-debian AS bun

# Copy Bun from official image
COPY --from=bun /usr/local/bin/bun /usr/local/bin/bun
ENV PATH=/usr/local/bin:$PATH

# Install application gems
COPY Gemfile Gemfile.lock ./
Expand Down
90 changes: 90 additions & 0 deletions test/results/bun_alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# syntax=docker/dockerfile:1
# check=error=true

# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t demo .
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name demo demo

# 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=xxx
FROM ruby:$RUBY_VERSION-alpine AS base

# Rails app lives here
WORKDIR /rails

# Update gems and bundler
RUN gem update --system --no-document && \
gem install -N bundler

# Install base packages
RUN apk add --no-cache curl jemalloc sqlite tzdata

# Set production environment
ENV BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development:test" \
RAILS_ENV="production"


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

# Install packages needed to build gems
RUN apk add --no-cache build-base pkgconfig unzip yaml-dev

ARG BUN_VERSION=xxx
FROM oven/bun:${BUN_VERSION}-alpine AS bun

# Copy Bun from official image
COPY --from=bun /usr/local/bin/bun /usr/local/bin/bun
ENV PATH=/usr/local/bin:$PATH

# 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

# Install node modules
COPY package.json bun.lock ./
RUN bun install --frozen-lockfile --production

# 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
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile


# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apk add --no-cache sqlite-libs

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

# Run and own only the runtime files as a non-root user for security
RUN addgroup --system --gid 1000 rails && \
adduser --system rails --uid 1000 --ingroup rails --home /home/rails --shell /bin/sh rails && \
mkdir /data && \
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"

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

# Start server via Thruster by default, this can be overwritten at runtime
EXPOSE 80
VOLUME /data
CMD ["./bin/thrust", "./bin/rails", "server"]
12 changes: 12 additions & 0 deletions test/test_bun_alpine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require_relative "base"

class TestBunAlpine < TestBase
@rails_options = "--javascript bun"
@generate_options = "--alpine"

def test_bun_alpine
check_dockerfile
end
end
Loading