Running Tasks & Consoles

Rails console

To access an interactive Rails console, run:

fly ssh console --pty -C "/rails/bin/rails console"
Loading production environment (Rails 7.0.4.2)
irb(main):001:0>

Then start using the console, but be careful! You’re in a production environment.

The --pty flag tells the SSH server to run the command in a pseudo-terminal. You will generally need this only when running interactive commands, like the Rails console.

Interactive shell

To access an interactive shell as the root user, run:

fly ssh console --pty -C '/bin/bash'
#

To access an interactive shell as the rails user, requires a tiny bit of setup:

bin/rails generate dockerfile --sudo

Accept the changes to your Dockerfile, and then rerun fly deploy.

Once this is complete, you can create an interactive shell:

fly ssh console --pty -C 'sudo -iu rails'
$

Rails tasks

In order to run other Rails tasks, a small change is needed to your Rails binstubs to set the current working directory. The following command will make the adjustment for you:

bin/rails generate dockerfile --bin-cd

Accept the changes to your Dockerfile, and then rerun fly deploy.

Once this is complete, you can execute other commands on Fly, for example:

fly ssh console -C "/rails/bin/rails db:migrate"

To list all the available tasks, run:

fly ssh console -C "/rails/bin/rails help"

Custom Rake tasks

You can create Custom Rake Tasks to automate frequently used commands. As an example, add the following into lib/tasks/fly.rake to reduce the number of keystrokes it takes to launch a console:

namespace :fly do
  task :ssh do
    sh 'fly ssh console --pty -C "sudo -iu rails"'
  end

  task :console do
    sh 'fly ssh console --pty -C "/rails/bin/rails console"'
  end

  task :dbconsole do
    sh 'fly ssh console --pty -C "/rails/bin/rails dbconsole"'
  end
end

You can run these tasks with bin/rails fly:ssh, bin/rails fly:console, and bin/rails fly:dbconsole respectively.