diff --git a/lib/generators/templates/Dockerfile.erb b/lib/generators/templates/Dockerfile.erb index 479ade8..2268c7d 100644 --- a/lib/generators/templates/Dockerfile.erb +++ b/lib/generators/templates/Dockerfile.erb @@ -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')) -%> diff --git a/lib/generators/templates/_install_node.erb b/lib/generators/templates/_install_node.erb index 0550d50..9aa5977 100644 --- a/lib/generators/templates/_install_node.erb +++ b/lib/generators/templates/_install_node.erb @@ -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 diff --git a/test/results/bun/Dockerfile b/test/results/bun/Dockerfile index e56e16f..f23f4a5 100644 --- a/test/results/bun/Dockerfile +++ b/test/results/bun/Dockerfile @@ -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 ./ diff --git a/test/results/bun_alpine/Dockerfile b/test/results/bun_alpine/Dockerfile new file mode 100644 index 0000000..ed9747b --- /dev/null +++ b/test/results/bun_alpine/Dockerfile @@ -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= --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"] diff --git a/test/test_bun_alpine.rb b/test/test_bun_alpine.rb new file mode 100644 index 0000000..63943dd --- /dev/null +++ b/test/test_bun_alpine.rb @@ -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