If you're running a Rails app in Google App Engine's flexible environment, it takes a bit of setup to get to a rails console attached to your deployed environment. I wanted to document the steps for my own reference and also as an aid to others.
-
Open the Google App Engine -> instances section of the Google Cloud Platform (GCP) console.
-
Select the "SSH" drop-down for a running instance. (Which instance? Both of my instances are in the same cluster, and both are running Rails, so it didn't matter for me. YMMV.) You have a choice about how to connect via ssh.
-
Choose "Open in browser window" to open a web-based SSH session, which is convenient but potentially awkward.
-
Choose "View
gcloud
command" to view and copy agcloud
command that you can use from a terminal, which lets you use your favorite terminal app but may require the extra steps of installing thegcloud
command and authenticating thegcloud
command with GCP.
-
-
When you're in the SSH session of your choice, run
sudo docker ps
to see what docker containers are presently running. -
Identify the container of your app. Here's what my output looked like (abbreviated for easier reading). My app's container was the first one.
[email protected]:~$ sudo docker ps CONTAINER ID IMAGE COMMAND NAMES 38e......552 us.gcr.io/my-project/appengine/default... "/bin/sh -c 'exec bun" gaeapp 8c0......0ab gcr.io/google_appengine/cloud-sql-proxy "/cloud_sql_proxy -di" focused_lalande 855......f92 gcr.io/google_appengine/api-proxy "/proxy" api 7ce......0ce gcr.io/google_appengine/nginx-proxy "/var/lib/nginx/bin/s" nginx_proxy 25f......bb8 gcr.io/google_appengine/fluentd-logger "/opt/google-fluentd/" fluentd_logger
-
Note the container name of your app (
gaeapp
in my case), and runcontainer_exec <container_name> bash
. -
Add
ruby
andnode
to your environment:export PATH=$PATH:/rbenv/versions/2.3.4/bin:/rbenv/bin:/nodejs/bin
-
cd /app
to get to your application code. -
Add any necessary environment variables that your Rails application expects to your environment. For example:
export DATABASE_URL='...'
If you don't know what your app needs, you can view the full environment of the app with
cat app.yaml
. -
bin/rails console production
to start a Rails console in the Railsproduction
environment.
Thanks for posting this. I came across this looking for help and think I may have found an easier solution.
Once you're in the SSH session after step three I found these steps to be a little easier:
Once you have the container ID you can view logs by running:
Or start a shell in the container that is running your code:
$ docker exec -it [CONTAINER-ID] /bin/bash
Once you've started a shell in the container that is running your code you can run
bundle exec rails c