First Launch Attempt: => ERROR [build 10/10] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile

This is my first attempt with fly.io and I am trying to port an application that I currently have hosted on Heroic.
Rails 7.1.2
Ruby 3.2.2
The full section of logs related to the error is:

=> ERROR [build 10/10] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile                                                                                2.4s
------
 > [build 10/10] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile:
2.319 bin/rails aborted!
2.320 ArgumentError: the :bucket option is nil (ArgumentError)
2.320
2.320         raise ArgumentError, "the :bucket option is nil" unless bucket
2.320               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2.320 /rails/config/initializers/shrine.rb:28:in `new'
2.320 /rails/config/initializers/shrine.rb:28:in `<main>'
2.320 /rails/config/environment.rb:5:in `<main>'
2.320 Tasks: TOP => environment
2.320 (See full trace by running task with --trace)
------
Error: failed to fetch an image or build from source: error building: failed to solve: process "/bin/sh -c SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile" did not complete successfully: exit code: 1

Seems that it may have something to do with shrine gem or AWS S3 bucket but I can’t interpret what is being indicated. Both Shrine and S3 work perfectly on Heroic. May Gemfile contains:
gem “shrine”, ‘~> 3.4’
gem “aws-sdk-s3”, “~> 1.114”
gem ‘image_processing’, ‘~> 1.12’ #1.7.1
gem ‘fastimage’, ‘~> 2.1’

My credentials contain for AWS:
aws:
s3:
id: ABCD etc
secret: RvqL etc
region: ap-southeast-2
bucket: shrine

My shrine.rb file contains:
s3_options = {
access_key_id: Rails.application.credentials.dig(:aws, :s3, :id),
secret_access_key: Rails.application.credentials.dig(:aws, :s3, :secret),
region: Rails.application.credentials.dig(:aws, :s3, :region),
bucket: Rails.application.credentials.dig(:aws, :s3, :bucket)
}

I would appreciate any help.

Here’s a link: Existing Rails Apps · Fly Docs

The net is that Rails runs your initializers in preparation for running assets:precompile, and the build machine doesn’t have access to your secrets. The reality i that you don’t need access to s3 in order to precompile your css, js, etc.

The easiest fix is to not call shrine if bucket is null. Add an if statement before line 28 in config/initializers/shrine.rb.

Not sure I fully understand as I am a beginner. Do you mean:
if Rails.application.credentials.aws
s3_options = {
access_key_id: Rails.application.credentials.dig(:aws, :s3, :id),
secret_access_key: Rails.application.credentials.dig(:aws, :s3, :secret),
region: Rails.application.credentials.dig(:aws, :s3, :region),
bucket: Rails.application.credentials.dig(:aws, :s3, :bucket)
}
end

or do I check for not null on the bucket line? if not null

Some place after you set s3_options and before line 18 put:

if s3_options[:bucket]

And some place before the end of the file put:

end

Error is now the iff statement:

 => ERROR [build 10/10] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile                                                                                              3.2s
------
 > [build 10/10] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile:
3.110 bin/rails aborted!
3.111 NoMethodError: undefined method `[]' for nil:NilClass (NoMethodError)
3.111
3.111 if s3_options[:bucket]
3.111              ^^^^^^^^^
3.111 /rails/config/initializers/shrine.rb:14:in `<main>'
3.111 /rails/config/environment.rb:5:in `<main>'
3.111 Tasks: TOP => environment
3.111 (See full trace by running task with --trace)
------
Error: failed to fetch an image or build from source: error building: failed to solve: process "/bin/sh -c SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile" did not complete successfully: exit code: 1

Here is my full shrine.rb file:

require "shrine"
require "shrine/storage/s3"
# require "shrine/storage/file_system"

if Rails.application.credentials.aws
  s3_options = {
    access_key_id:     Rails.application.credentials.dig(:aws, :s3, :id),
    secret_access_key: Rails.application.credentials.dig(:aws, :s3, :secret),
    region:            Rails.application.credentials.dig(:aws, :s3, :region),
    bucket:            Rails.application.credentials.dig(:aws, :s3, :bucket)
  }
end

if s3_options[:bucket]

Shrine.plugin :activerecord
Shrine.plugin :derivatives, versions_compatibility: true
Shrine.plugin :determine_mime_type, analyzer: :marcel
Shrine.plugin :validation
Shrine.plugin :validation_helpers
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :store_dimensions, analyzer: :mini_magick
Shrine.plugin :remove_attachment

Shrine.storages = {
	cache: Shrine::Storage::S3.new(prefix: "cache", upload_options: {acl: "public-read"}, **s3_options),
	store: Shrine::Storage::S3.new(prefix: "store", upload_options: {acl: "public-read"}, **s3_options),
}

end

Delete these two lines and you should be good to go!

That now works and the script runs but not home free yet.

WARNING The app is not listening on the expected address and will not be reachable by fly-proxy.

You can fix this by configuring your app to listen on the following addresses:

  • 0.0.0.0:3000

I read the docs. The settling in toml.fly is
[http_service]
internal_port = 3000

which I know is the localhost port

Dockerfile has:
EXPOSE 3000
CMD [“./bin/rails”, “server”]

Do I change both these to 8080 or something else?

Your port is likely fine.

To debug this further, we would need to look at the log lines that are produced. See: Existing Rails Apps · Fly Docs

The problem could be your server is failing to start, or that it is taking a while to start and the deploy process gave up waiting, or any of a number of other problems.

OK! the fly logs gives this.

2024-04-07T03:16:35Z proxy[5683d643a624e8] syd [error]instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)
2024-04-07T03:16:35Z app[5683d643a624e8] syd [info] INFO Main child exited normally with code: 1
2024-04-07T03:16:35Z app[5683d643a624e8] syd [info] INFO Starting clean up.
2024-04-07T03:16:35Z app[5683d643a624e8] syd [info][    3.252181] reboot: Restarting system
2024-04-07T03:16:35Z runner[5683d643a624e8] syd [info]machine did not have a restart policy, defaulting to restart
2024-04-07T03:16:37Z proxy[5683d643a624e8] syd [info]Starting machine
2024-04-07T03:16:37Z proxy[5683d643a624e8] syd [error]failed to change machine state: machine still active, refusing to start
2024-04-07T03:16:38Z proxy[e7842369b607e8] syd [info]Starting machine
2024-04-07T03:16:38Z proxy[e7842369b607e8] syd [error]failed to change machine state: machine still active, refusing to start

The heroku app is listening on 25961

2024-04-07T03:15:53.322928+00:00 app[web.1]: * Listening on http://0.0.0.0:25961

I just noticed another error in the logs:
2024-04-07T04:23:02Z app[5683d643a624e8] syd [info]/usr/local/bundle/ruby/3.2.0/gems/activerecord-7.1.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:19:in `exec’: ERROR: relation “taggings” does not exist (PG::UndefinedTable)
2024-04-07T04:23:02Z app[5683d643a624e8] syd [info]LINE 10: WHERE a.attrelid = ‘“taggings”’::regclass
2024-04-07T04:23:02Z app[5683d643a624e8] syd [info] ^
I wonder if it is because I haven’t transferred the heroku postgres db yet. I was making sure the app ran before I did this.

Now working - I read the docs numerous times then found I needed to run:
fly apps open

Thank you for your help.