fly-apps / dockerfile-rails

Provides a Rails generator to produce Dockerfiles and related files.
MIT License
455 stars 38 forks source link

Install Bun when it is being used instead of Node #60

Closed luizkowalski closed 9 months ago

luizkowalski commented 9 months ago

The latest jsbundling-rails release includes the ability to use bun as well. It would be nice if when the Dockerfile is being generated, it could also detect bun as the js runtime (probably via the presence of bun.lockb or bun.config.js files and use that instead of Node

rubys commented 9 months ago

Where bun was added https://github.com/rails/jsbundling-rails/pull/167

rubys commented 9 months ago

Initial support installs bun instead of yarn. Node in still installed, for now. I should be able to remove Node. Neither node nor bun will be in the deployed image unless execjs is used.

rubys commented 9 months ago

reopening as testing without node at all is going surprisingly well...

luizkowalski commented 9 months ago

hey Sam! first of all, thank you so much for the quick reply and implementation!

I'm still getting mentions of yarn.lock on my Dockerfile

This is my configuration:

# generated by dockerfile-rails

---
options:
  bin-cd: true
  cache: true
  fullstaq: true
  jemalloc: true
  parallel: true
  postgresql: true
  redis: true
  root: true
  yjit: true
  envs:
    base:
      BUNDLE_WITHOUT: development:test:linting:profiler
      GIT_REV: "${GIT_REV}"
      BUILD_DATE: "${BUILD_DATE}"
  args:
    base:
      GIT_REV: "${GIT_REV}"
      BUILD_DATE: "${BUILD_DATE}"

and this is what is being generated (relevant part)

# Throw-away build stages to reduce size of final image
FROM base as prebuild

# Install packages needed to build gems and node modules
RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
    --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
    apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential curl libpq-dev node-gyp pkg-config python-is-python3

FROM prebuild as node

# Install Node.js
ARG NODE_VERSION=20.6.1
ARG BUN_VERSION=1.0.0
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
    /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
    npm install -g bun@$BUN_VERSION && \
    rm -rf /tmp/node-build-master

# Install node modules
COPY --link package.json yarn.lock ./
RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
    YARN_CACHE_FOLDER=/root/.yarn bun install --frozen-lockfile
rubys commented 9 months ago

You are getting mentions of a yarn.lock because.... drum roll you have a yarn.lock. If you remove your yarn.lock file and regenerate your dockerfile this should go away.

More troublesome is that bun.lockb is not listed on the COPY line. That's either because you don't have that file, or this is a path that I've not yet tested.

rubys commented 9 months ago

Bun's addition to Rails: https://github.com/rails/rails/commit/274bc97d63f9e29129851a99c00fd2cefe97cfcf

luizkowalski commented 9 months ago

I'm getting an error because I do not have a yarn.lock file on my root folder but I do have a bun.lockb. I manually change yarn.lock to bun.lockb and it worked but any new dockerfile generation overrides it, obviously

rubys commented 9 months ago

That's fixed in github, but unreleased. I've also fixed other things, including some cosmetic changes like naming the stage bun not node. If you want to try it, replace the entry in your gemfile with:

gem "dockerfile-rails", "~> 1.5", :group => :development, :git => "https://github.com/fly-apps/dockerfile-rails.git"
luizkowalski commented 9 months ago

@rubys thanks! I will give it a try tonight and let you know!

luizkowalski commented 9 months ago

couple of things:

  1. now it is installing bun instead of node but it is trying to run npm install instead of bun install:
FROM prebuild as bun

# Install Bun
ARG BUN_VERSION=1.0.1
ENV BUN_INSTALL=/usr/local/bun
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -fsSL https://bun.sh/install | bash -s -- "${BUN_VERSION}"

# Install node modules
COPY --link package.json bun.lockb ./
RUN --mount=type=cache,id=bld-npm-cache,target=/root/.npm \
    npm install

I don't have a yarn.lock, not sure why it is trying to use npm

  1. bun install step is failing:
 => CANCELED [stage-4 1/3] RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt     --mount=type=cache,id=dev-apt-lib,sh  20.0s
 => ERROR [bun 1/3] RUN curl -fsSL https://bun.sh/install | bash -s -- "1.0.1"                                                                     0.6s
 => [build 1/7] COPY --link Gemfile Gemfile.lock ./                                                                                                0.0s
 => CANCELED [build 2/7] RUN --mount=type=cache,id=bld-gem-cache,sharing=locked,target=/srv/vendor     bundle config set app_config .bundle &&     0.6s
------
 > [bun 1/3] RUN curl -fsSL https://bun.sh/install | bash -s -- "1.0.1":
#0 0.619 error: unzip is required to install bun (see: https://github.com/oven-sh/bun#unzip-is-required)

it might have to do with my image maybe? (fullstaq-ruby:${RUBY_VERSION}-jemalloc-slim).

I guess fullstaq-ruby slim does not include unzip while the ruby docker image does?

edit: I manually added unzip and got this error instead

 => CANCELED [stage-4 1/3] RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt     --mount=type=cache,id=dev-apt-lib,sh  18.8s
 => ERROR [bun 1/3] RUN curl -fsSL https://bun.sh/install | bash -s -- "1.0.1"                                                                     1.0s
 => [build 1/7] COPY --link Gemfile Gemfile.lock ./                                                                                                0.0s
 => CANCELED [build 2/7] RUN --mount=type=cache,id=bld-gem-cache,sharing=locked,target=/srv/vendor     bundle config set app_config .bundle &&     1.0s
------
 > [bun 1/3] RUN curl -fsSL https://bun.sh/install | bash -s -- "1.0.1":
curl: (22) The requested URL returned error: 404
#0 0.989
#0 0.991 error: Failed to download bun from "https://github.com/oven-sh/bun/releases/download/1.0.1/bun-linux-x64.zip"

but that seems to be a mistake on bun's install script

rubys commented 9 months ago

I don't have a yarn.lock, not sure why it is trying to use npm

That should now be fixed.

but that seems to be a mistake on bun's install script

OK, switched back to installing bun via npm.

luizkowalski commented 9 months ago
 => ERROR [build 4/7] COPY --from=bun /usr/local/bun /usr/local/bun                                                                                0.0s
------
 > [build 4/7] COPY --from=bun /usr/local/bun /usr/local/bun:
------
Error: failed to fetch an image or build from source: error building: failed to solve: failed to compute cache key: "/usr/local/bun" not found: not found

I thought that COPY --from=bun /usr/local/bun /usr/local/bun is not necessary when using npm so I removed but then I got a different error

 => ERROR [build 6/6] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile                                                                      2.1s
------
 > [build 6/6] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile:
#0 2.108 rails aborted!
#0 2.108 cssbundling-rails: Command install failed, ensure bun is installed

I guess it is needed but the path is incorrect. I couldn't figure out the path exactly

luizkowalski commented 9 months ago

ok, so I got bun installed with my Dockerfile configuration. This is what I've done

FROM prebuild as bun

# Install Bun
ARG BUN_VERSION=bun-v1.0.2
ENV BUN_INSTALL=/usr/local/bun
ENV PATH=/usr/local/bun/bin:$PATH
RUN curl -fsSL https://bun.sh/install | bash -s -- "${BUN_VERSION}"

noticed that the BUN_VERSION arg should be bun-v{version} not just the version

after that, you need to copy the installer and set the path on the next step

FROM prebuild as build
# Install application gems
...

# Copy bun modules
COPY --from=bun /rails/node_modules /rails/node_modules
COPY --from=bun /usr/local/bun /usr/local/bun
ENV PATH=/usr/local/bun/bin:$PATH

I noticed you got to set the PATH for bun twice tho

rubys commented 9 months ago

Thanks! I pushed a change with basically those fixes applied, with some minor cosmetic differences (example: I put "bun-v" on the curl line). Can you give it a try?

luizkowalski commented 9 months ago

Works perfectly! The only thing I noticed was that it kept the PATH addition for node:

ENV PATH=/usr/local/bun/bin:$PATH
ENV PATH=/usr/local/node/bin:$PATH

it's not going to break anything ofc but it can be removed I believe

apart from that, it is compiling now 🎉

rubys commented 9 months ago

Fixed: https://github.com/fly-apps/dockerfile-rails/commit/56d03738ad87a14404598ac772e4b9ca1d6195e8

rubys commented 9 months ago

Released: https://github.com/fly-apps/dockerfile-rails/releases/tag/v1.5.9