fly-apps/dockerfile-rails

`yarn: not found` when using `jsbundling-rails` with `--parallel` option

skydan opened this issue · 6 comments

skydan commented
 > [build 7/7] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile:
#21 3.323 sh: 1: yarn: not found
#21 3.323 rails aborted!
#21 3.324 jsbundling-rails: Command build failed, ensure yarn is installed and `yarn build` runs without errors
#21 3.324 /rails/vendor/ruby/3.2.0/gems/jsbundling-rails-1.1.1/lib/tasks/jsbundling/build.rake:5:in `block (2 levels) in <main>'
Dockerfile

# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.1
FROM quay.io/evl.ms/fullstaq-ruby:${RUBY_VERSION}-jemalloc-slim as base

LABEL fly_launch_runtime="rails"

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_WITHOUT="development:test" \
    BUNDLE_DEPLOYMENT="1"

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


# 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 git node-gyp pkg-config python-is-python3


FROM prebuild as node

# Install JavaScript dependencies
ARG NODE_VERSION=16.15.1
ARG YARN_VERSION=1.22.18
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 yarn@$YARN_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 yarn install --frozen-lockfile


FROM prebuild as build

# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN --mount=type=cache,id=bld-gem-cache,sharing=locked,target=/srv/vendor \
    bundle config set app_config .bundle && \
    bundle config set path /srv/vendor && \
    bundle install && \
    bundle exec bootsnap precompile --gemfile && \
    bundle clean && \
    mkdir -p vendor && \
    bundle config set path vendor && \
    cp -ar /srv/vendor .

# Copy node modules
COPY --from=node /rails/node_modules /rails/node_modules

# Copy application code
COPY --link . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Adjust binfiles to set current working directory
RUN grep -l '#!/usr/bin/env ruby' /rails/bin/* | xargs sed -i '/^#!/aDir.chdir File.expand_path("..", __dir__)'

# 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 --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 iputils-ping libsqlite3-0 net-tools procps traceroute

# Run and own the application files as a non-root user for security
RUN useradd rails --home /rails --shell /bin/bash

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build --chown=rails:rails /rails /rails

# Deployment options
ENV RUBY_YJIT_ENABLE="1"

# 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"]

Spent some time to debug it, but maybe put some warning/confirmation when jsbundling-rails is present and user is using --parallel option or add node/yarn into prebuild stage?

rubys commented

not including node/yarn in the prebuild stage is definitely a bug

What would the workaround be here? I don't see any documentation in docker or docker compose about a prebuild stage... 🤔

I'm getting:

Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/

When running

RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile

Thanks!

Ok, fixed this by removing the --parallel option and re-running the generator.

You can also fix this (without removing parallel) with this diff:

diff --git a/Dockerfile.fly b/Dockerfile.fly
--- a/Dockerfile
+++ b/Dockerfile
@@ -62,6 +62,8 @@ RUN --mount=type=cache,id=bld-gem-cache,sharing=locked,target=/srv/vendor \
 
 # Copy node modules
 COPY --from=node /rails/node_modules /rails/node_modules
+COPY --from=node /usr/local/node /usr/local/node
+ENV PATH=/usr/local/node/bin:$PATH
kzkn commented

jsbundling-rails and cssbundling-rails (will) support skipping yarn install on assets:precompile.
rails/jsbundling-rails#150
rails/cssbundling-rails#125