Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
luizkowalski opened this issue Sep 13, 2023 · 17 comments
Closed

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

luizkowalski opened this issue Sep 13, 2023 · 17 comments

Comments

@luizkowalski
Copy link
Collaborator

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
Copy link
Collaborator

rubys commented Sep 14, 2023

Where bun was added rails/jsbundling-rails#167

@rubys rubys closed this as completed in 3ee217a Sep 14, 2023
@rubys
Copy link
Collaborator

rubys commented Sep 14, 2023

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
Copy link
Collaborator

rubys commented Sep 14, 2023

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

@rubys rubys reopened this Sep 14, 2023
@luizkowalski
Copy link
Collaborator Author

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
Copy link
Collaborator

rubys commented Sep 14, 2023

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
Copy link
Collaborator

rubys commented Sep 15, 2023

Bun's addition to Rails: rails/rails@274bc97

@luizkowalski
Copy link
Collaborator Author

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
Copy link
Collaborator

rubys commented Sep 15, 2023

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
Copy link
Collaborator Author

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

@luizkowalski
Copy link
Collaborator Author

luizkowalski commented Sep 15, 2023

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
Copy link
Collaborator

rubys commented Sep 16, 2023

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
Copy link
Collaborator Author

 => 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
Copy link
Collaborator Author

luizkowalski commented Sep 16, 2023

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
Copy link
Collaborator

rubys commented Sep 16, 2023

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
Copy link
Collaborator Author

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
Copy link
Collaborator

rubys commented Sep 16, 2023

Fixed: 56d0373

@rubys
Copy link
Collaborator

rubys commented Sep 16, 2023

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

@rubys rubys closed this as completed Sep 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants