From 01fd503a92131a04f62a41f9da262590e5ba9e61 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 15 Dec 2025 22:27:47 -0600 Subject: [PATCH] Add Run Rails Console With Remote Dokku App as a Rails TIL --- README.md | 3 +- ...run-rails-console-with-remote-dokku-app.md | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 rails/run-rails-console-with-remote-dokku-app.md diff --git a/README.md b/README.md index cacb5e8..4bae849 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1707 TILs and counting..._ +_1708 TILs and counting..._ See some of the other learning resources I work on: @@ -1166,6 +1166,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Rounding Numbers With Precision](rails/rounding-numbers-with-precision.md) - [Run A Rake Task Programmatically](rails/run-a-rake-task-programmatically.md) - [Run Commands With Specific Rails Version](rails/run-commands-with-specific-rails-version.md) +- [Run Rails Console With Remote Dokku App](rails/run-rails-console-with-remote-dokku-app.md) - [Run Some Code Whenever Rails Console Starts](rails/run-some-code-whenever-rails-console-starts.md) - [Scaffold Auth Functionality With Rails 8 Generator](rails/scaffold-auth-functionality-with-rails-8-generator.md) - [Schedule Sidekiq Jobs Out Into The Future](rails/schedule-sidekiq-jobs-out-into-the-future.md) diff --git a/rails/run-rails-console-with-remote-dokku-app.md b/rails/run-rails-console-with-remote-dokku-app.md new file mode 100644 index 0000000..d46f60f --- /dev/null +++ b/rails/run-rails-console-with-remote-dokku-app.md @@ -0,0 +1,49 @@ +# Run Rails Console With Remote Dokku App + +Whenever I want to `rails console` into the _staging_ server of an app I'm +working on, I first have to `ssh` into server and then I have to come up with +the [`dokku`](https://dokku.com/) command to run `rails console` against the app +on that server. + +```bash +local> ssh app-staging # app-staging is an SSH alias +staging> dokku run my-app rails console +``` + +I figured out how to reduce the friction of this by collapsing it into a single +command that I can run locally. I can remotely run the `dokku` command with +`ssh` using an interactive session (`-t`). + +```bash +local> ssh -t app-staging dokku run my-app rails console +``` + +That will open up a `rails console` session directly in the current shell +session via a remote SSH connection. The `-t` flag is important because that +makes the session interactive so that I can interact with the REPL. + +I've even packaged this up into a bin script (`bin/staging-console`) with a +couple checks to enhance the DX. I won't put the whole thing here, but the gist +of it is: + +```bash +#!/usr/bin/env bash + +set -e + +if [ -z "$DOKKU_STAGING_SSH_ALIAS" ]; then + echo "Error: DOKKU_STAGING_SSH_ALIAS environment variable is not set." + echo "" + # echo more help details here ... + exit 1 +fi + +# Check if SSH alias exists +# ... + +# Check if we can reach the server +# ... + +# Run the console +ssh -t "$DOKKU_STAGING_SSH_ALIAS" dokku run my-app rails console "$@" +```