Rails App build error

Hi there,

I’m encountering with the following error when trying to deploy my App:

Error failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile]: exit code: 1

I removed my dockerfile and fly.toml and delete the App in fly.io. Then I re-launched, but couldn’t solve this problem.

Here’s my Dockerfile:

# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
FROM ruby:$RUBY_VERSION-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" \

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

# Install packages needed to install nodejs
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y curl && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install Node.js
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 && \
    rm -rf /tmp/node-build-master

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

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential git libpq-dev libvips node-gyp pkg-config python-is-python3

# Install yarn
RUN npm install -g yarn@$YARN_VERSION

# Build options
ENV PATH="/usr/local/node/bin:$PATH"

# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
    bundle exec bootsnap precompile --gemfile && \
    rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git

# Install node modules
COPY --link package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Copy application code
COPY --link . .

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

# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y imagemagick libvips postgresql-client && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

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

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

# Deployment options

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

# Start the server by default, this can be overwritten at runtime
CMD ["./bin/rails", "server"]

Here’s my fly.toml file:

# fly.toml file generated for restaurant-website on 2023-05-01T10:43:09+02:00

app = "restaurant-website"
kill_signal = "SIGINT"
kill_timeout = 5
primary_region = "mad"
processes = []


  auto_rollback = true

  http_checks = []
  internal_port = 3000
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
    hard_limit = 25
    soft_limit = 20
    type = "connections"

    force_https = true
    handlers = ["http"]
    port = 80

    handlers = ["tls", "http"]
    port = 443

    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

  guest_path = "/rails/public"
  url_prefix = "/"

Do you know what should I check to solve this issue?

Thank you very much!

Hi PabloC, thanks for your help! Tried that command and got the same error:

Error failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c SECRET_KEY_BASE=DUMMY bundle exec rails assets:precompile]: exit code: 1

There is likely more information available in the logs.

The most common reason why assets:precompile may fail on a build machine is that one of your config files accesses a secret, something that often can be easily avoided by the addition of an if statement.

Hi Rubys, thanks for looking at this!

I tried checking the logs but it seems that the App hasn’t been deployed yet, and logs remains empty

I checked my config files and the only thing I found accessing secrets is this block:

  service: Disk
  public: true
  root: <%= ENV.fetch('RAILS_STORAGE', Rails.root.join("storage")) %>

I commented this out and still got the same error. Do you know what else could I check?

Thank you very much!

Is there a stack traceback before this line? If you scroll back, you should see something like the following, and immediately following it will be the error that you need to address:

 => ERROR [build 6/6] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precom  1.4s
 > [build 6/6] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile:         
#18 1.355 rails aborted!

Thanks for helping me to go on the right direction Rubys! I could find the error which is the same as described here: Function rgb is missing argument $green. · tailwindlabs/tailwindcss · Discussion #6738 · GitHub

The solution posted there worked for me. Thanks again! :pray:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.