skip to content

Built-In Subcommands

Daily Rails work runs through bin/rails: server, console with --sandbox, dbconsole, runner and stats. Interviewers ask which command fits a task and how RAILS_ENV and -e pick the environment.

part ofRuby on Railsread it →
on this pageshow

explore

questions

5

In a Rails 8.1 app, what do bin/rails server, console, dbconsole and runner each start, and when do you reach for each?

level: juniorimportance: must knowfreq 68%

answer

  1. four entry points, one application
  2. s, c, db and r aliases
  3. Puma on port 3000, localhost in development
  4. the database's own client, not Ruby
  5. code string, file path or - for stdin

basics

~20 s

bin/rails server boots the app under Puma on port 3000; console opens IRB with the app loaded; dbconsole opens the database's own client (psql, mysql, sqlite3) from database.yml; runner boots the app, runs one Ruby string or file, and exits.

solid answer

~50 s

All four live in `railties/lib/rails/commands` and share short aliases: `s`, `c`, `db` and `r`. `bin/rails server` boots the application under Puma, on port 3000 and bound to `localhost` in development; `-p` changes the port, `-b` the address, `-e` the environment. `bin/rails console` boots the same app inside IRB so you can call models, jobs and helpers by hand. `bin/rails dbconsole` reads `config/database.yml` and execs the adapter's own client (`psql`, `mysql`, `sqlite3`), so you type SQL, not Ruby; `--db` picks one of several databases and `-p` passes the password along. `bin/rails runner` boots the app, evaluates a code string, a `.rb` file or stdin (`-`), and exits, which makes it the fit for a one-off script or a cron line. Choose by what you need: HTTP traffic, a Ruby prompt, a SQL prompt, or a non-interactive script.

code

bash · 5 lines
bash
bin/rails s -p 4000 -b 0.0.0.0          # serve on all interfaces, port 4000
bin/rails c                              # IRB with the app loaded
bin/rails db --db queue                  # sqlite3/psql on the queue database
bin/rails r 'puts Rental.overdue.count'  # run once and exit
bin/rails r script/close_season.rb 2026  # 2026 lands in ARGV

go deeper

for a junior

Know the four commands, their aliases s, c, db and r, and that server defaults to Puma on port 3000. Be clear that console is a Ruby prompt and dbconsole a SQL prompt.

for a middle

Explain the flags you actually use: -p, -b and -e on server, --db and -p on dbconsole, and runner's three input forms. Know why a development server is unreachable from another device.

for a senior

Show you pick runner for scheduled one-off work, know what the executor wrapper means for a long script, and avoid opening dbconsole against production when a scoped console query would do.

for a principal

Frame these commands as the team's operational surface: which ones are allowed against production, how access is audited, and when a recurring runner script should become a Rake task or a job.

## One executable, many subcommands Every Rails app carries `bin/rails`, a three-line script that sets `APP_PATH`, loads `config/boot.rb` and hands the arguments to `rails/commands`. That file expands a handful of **aliases** before dispatching: | Alias | Command | What it starts | |---|---|---| | `s` | `server` | the web server, serving HTTP | | `c` | `console` | an IRB session with the app loaded | | `db` | `dbconsole` | the database's own command-line client | | `r` | `runner` | a non-interactive Ruby script, then exit | (`g`, `d` and `t` exist too, for generators, destroy and tests.) All four commands below boot the application, so they see the same models, initializers and credentials the web process sees. ## bin/rails server `server` builds a `Rails::Server` and runs the app through a Rack handler, which is **Puma** in a generated app. Defaults worth knowing: - **Port 3000**, unless `-p` / `--port` or the `PORT` environment variable says otherwise. - **Bind address**: `localhost` in development and `0.0.0.0` in every other environment, unless `-b` / `--binding` or `BINDING` overrides it. That is why a dev server inside a VM or container is unreachable from the host until you pass `-b 0.0.0.0`. - **Environment**: development unless `-e` or `RAILS_ENV` names another. - In development it writes its PID to `tmp/pids/server.pid` and logs to stdout. In production the same command is what the container runs behind Thruster; Puma's own settings live in `config/puma.rb`, which is a separate topic. ## bin/rails console `console` boots the app and opens **IRB** with Rails' helpers registered: `reload!`, `app` (an integration session), `helper` (the view helpers) and `controller`. The prompt shows the app name and a coloured environment tag (`dev`, `test`, `prod`). `--sandbox` (`-s`) wraps the session's database work in a transaction that is never committed. This is where you poke at a model, try a query or enqueue a job by hand. ## bin/rails dbconsole `dbconsole` does **not** start Ruby for you to type into. It finds the database configuration for the current environment and execs that adapter's client with the host, port, user and database filled in: - `psql` for PostgreSQL, `mysql` for MySQL and MariaDB, `sqlite3` for SQLite; - `--db` / `--database` chooses one entry when the environment has several (a Rails 8.1 production `database.yml` has `primary`, `cache`, `queue` and `cable`); - `-p` / `--include-password` passes the password from `database.yml` instead of prompting; - `--mode` and `--header` tune the SQLite client's output. Use it when SQL is the right tool: `\d rentals` in psql, an `EXPLAIN`, a quick count without loading models. ## bin/rails runner `runner` boots the app, runs code, and exits. It accepts three forms: 1. a **code string**: `bin/rails runner 'puts Rental.overdue.count'`; 2. a **file path**: `bin/rails runner script/close_season.rb`, with any further arguments left in `ARGV`; 3. **`-`** to read the script from stdin. By default the code runs inside `Rails.application.executor.wrap`, the same unit-of-work wrapper a request gets; `--skip-executor` (`-w`) opts out for long-running scripts. Its USAGE text even shows runner as a shebang line. Reach for it for cron jobs and one-off maintenance that does not deserve a Rake task. ## Choosing between them - Need to **serve requests**? `server`. - Need to **explore or fix data interactively in Ruby**? `console`, ideally `--sandbox` while you experiment. - Need **raw SQL** with the database's own tooling? `dbconsole`. - Need to **run Ruby once, unattended**? `runner`. - Need to **boot and exit**, only to prove the app loads? `bin/rails boot` exists for exactly that and runs no code of yours. The common confusions are mixing up `console` and `dbconsole` (Ruby prompt vs SQL prompt) and expecting `runner` to leave a prompt open; it never does. ## Mistakes interviewers listen for - **Using `dbconsole` to call models.** It is the database's client; `Rental.count` there is a SQL syntax error. Use `console`. - **Scripting the console.** Piping commands into `bin/rails console` is fragile; `runner` is the supported non-interactive path, and an exception in the script ends the process with a failing status that cron or CI can see. - **Forgetting the environment.** All four commands default to development unless `RAILS_ENV` or `-e` says otherwise, so a script on a server needs the environment stated. - **Forgetting `--db` on a multi-database app.** Without it, `dbconsole` opens the first configuration listed for the environment (normally `primary`), so the Solid Queue tables are not there; pass `--db queue`. - **Assuming `bin/rails server` and production boot differ.** The server command is a thin wrapper over the same Rack app the container's Puma serves; behaviour differences come from the environment, not the command.

  • Why can't a phone on the same Wi-Fi reach bin/rails server running on your laptop in development?
    In development `bin/rails server` binds to `localhost` unless told otherwise, so only the laptop itself can connect. Pass `-b 0.0.0.0` (or set `BINDING`) to listen on every interface. Other environments already default to `0.0.0.0`. Development's default `config.hosts` admits any IP address, so browsing to the laptop's IP works; a custom host name such as `laptop.local` must be added to `config.hosts`.
  • What does bin/rails runner's --skip-executor flag turn off?
    By default runner wraps the script in `Rails.application.executor.wrap`, the same unit-of-work wrapper each request gets, so autoloading and framework hooks behave as they do in a request; Active Record, for example, enables its query cache for the duration. A script that runs for hours is one huge unit of work under that wrapper, so `--skip-executor` (`-w`) runs it unwrapped and leaves the script to manage such things itself.

  • bin/rails dbconsole opens an IRB session with the models loaded
  • bin/rails runner leaves an interactive prompt open after the script ends
  • bin/rails server listens on 0.0.0.0 by default in development
  • The -p flag of bin/rails dbconsole sets the database port
  • bin/rails boot runs a script after loading the application
open as a page

In Rails 8.1, how do RAILS_ENV and the -e flag decide which environment bin/rails console, runner and server boot into?

level: middleimportance: must knowfreq 55%

basics

~20 s

Without a flag, bin/rails uses RAILS_ENV, then RACK_ENV, then development. The -e flag on server, console, runner and dbconsole overrides that for one command; Rake tasks such as db:migrate have no -e, so they need RAILS_ENV=production in front.

open as a page

In Rails 8.1, how does bin/rails tell its own commands from Rake tasks, and how do bin/rails --help and bin/rails -T differ?

level: middleimportance: should knowfreq 30%

basics

~20 s

bin/rails first looks for a Rails command class with that name (server, console, stats, notes, about); if none exists it hands the name to Rake. --help lists commands and framework tasks but hides the app's own lib/tasks; -T lists every described Rake task.

open as a page

In the Rails 8.1 console, what do reload!, app and helper give you, and what catches people out with each?

level: middleimportance: should knowfreq 42%

basics

~20 s

reload! reloads changed application code, but objects already in variables keep their old class; app is an integration session that can call route helpers and send requests without a server; helper is ApplicationController's view-helper proxy for trying formatting helpers.

open as a page

When you must inspect live data in a ski-rental Rails 8.1 app, what does bin/rails console --sandbox protect against, and what does it leave exposed?

level: seniorimportance: should knowfreq 38%

basics

~20 s

--sandbox opens a transaction on every database connection the console checks out and never commits it, so row changes vanish on exit. Locks, after_commit side effects, emails, HTTP calls and heavy reads still reach production.

open as a page