title: Unable to deploy Rails app into Google Cloud Run
body: <p>I have a Rails app running locally with Docker and now I'm trying to deploy it to Google Cloud Platform using Cloud Run, following this great <a href="https://medium.com/google-cloud/google-cloud-run-on-rails-a-real-life-example-part-1-preparing-the-ground-705c94ab8a7a" rel="nofollow noreferrer">tutorial</a> by Laurent Julliard.</p>
  
  <p>I'm using following GCP services:</p>
  
  <ul>
  <li><strong>Cloud Build</strong> to build the container image.</li>
  <li><strong>Cloud Run</strong> for the deploy.  </li>
  <li><strong>Cloud Storage</strong> for Rails Active Storage.</li>
  <li><strong>Cloud SQL</strong> for my Postgres DB.</li>
  <li><strong>Cloud Key Management Service</strong> for my secrets.</li>
  </ul>
  
  <p><em>Last 3 services are not relevant for this issue (I think ‍♂️).</em></p>
  
  <p><strong>My deploy process has 2 steps:</strong></p>
  
  <ol>
  <li>Build the container image using Cloud Build</li>
  <li>Deploy the image using Cloud Run</li>
  </ol>
  
  <h2>Build the container image using Cloud Build</h2>
  
  <p><strong>cloudbuild.yaml</strong> file describe the steps Cloud Build has to go through to build your container.</p>
  
  <pre><code>steps:
  
  # Decrypt Rails Master key file
  - name: gcr.io/cloud-builders/gcloud
    args: ["kms", "decrypt", "--ciphertext-file=./config/master.key.enc", 
           "--plaintext-file=./config/master.key",
           "--location=us-central1","--keyring=whale-on-rails", 
          "--key=rails_master_key"]
  
  # Decrypt Whale on Rails service account credentials
  - name: gcr.io/cloud-builders/gcloud
    args: ["kms", "decrypt", "--ciphertext-file=./config/whale_on_rails.key.enc", 
           "--plaintext-file=./config/whale_on_rails.key",
           "--location=us-central1","--keyring=whale-on-rails", 
           "--key=whale_on_rails_key"]
  
  # Build image with tag 'latest' and pass decrypted Rails DB password as argument
  - name: 'gcr.io/cloud-builders/docker'
    args: [ 'build',
      '--tag', 'gcr.io/$PROJECT_ID/whale_on_rails:latest',
      '--build-arg', 'DB_PWD',
      '--build-arg', 'RUBY_VERSION=${_RUBY_VERSION}',
      '--build-arg', 'PG_MAJOR=${_PG_MAJOR}',
      '--build-arg', 'NODE_MAJOR=${_NODE_MAJOR}',
      '--build-arg', 'YARN_VERSION=${_YARN_VERSION}',
      '--build-arg', 'BUNDLER_VERSION=${_BUNDLER_VERSION}',
      '--build-arg', 'RAILS_ENV=${_RAILS_ENV}',
      '--build-arg', 'REDIS_URL=${_REDIS_URL}',
      '--build-arg', 'DATABASE_HOST=${_DATABASE_HOST}',
      '--build-arg', 'DATABASE_USER=${_DATABASE_USER}',
      '--build-arg', 'DATABASE_NAME=${_DATABASE_NAME}',
      '.'
    ]
    secretEnv: ['DB_PWD']
    env:
      - RAILS_ENV=${_RAILS_ENV}
      - REDIS_URL=redis://redis:6379/
      - DATABASE_HOST=/cloudsql/whale-on-rails:us-central1:whale-on-rails-production
      - DATABASE_USER=postgres
      - DATABASE_NAME=whale-on-rails
  
  # Push new image to Google Cloud Registry       
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/whale_on_rails:latest']
  
  secrets:
  - kmsKeyName: projects/whale-on-rails/locations/us-central1/keyRings/whale-on-rails/cryptoKeys/db_pwd_key
    secretEnv:
      DB_PWD: "CiQArHLfBqlON9MNzM+eKp/Un/HJucmgUftgl5LkYBzvLjsXsaQSOQCBzWJBAh061i7dJrNhEQeWqJHgSkaJpRka9w9nxmbiFzHZ1fpXOm0d7FWAi/8v36EDXmWnxkYXsw=="
  
  substitutions:
    _RUBY_VERSION: '2.6.5'
    _PG_MAJOR: '11'
    _NODE_MAJOR: '12'
    _YARN_VERSION: '1.13.0'
    _BUNDLER_VERSION: '2.0.2'
    _RAILS_ENV: production
    _REDIS_URL: redis://redis:6379/
    _DATABASE_HOST: /cloudsql/whale-on-rails:us-central1:whale-on-rails-production
    _DATABASE_USER: postgres
    _DATABASE_NAME: whale-on-rails
  
  </code></pre>
  
  <p><strong>Dockerfile</strong>: this file is invoked by the ‘Build image’ step in Cloud Build.</p>
  
  <pre><code>ARG RUBY_VERSION
  FROM ruby:$RUBY_VERSION
  
  ARG PG_MAJOR
  ARG NODE_MAJOR
  ARG BUNDLER_VERSION
  ARG YARN_VERSION
  ARG RAILS_ENV
  
  # Add PostgreSQL to sources list
  RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
    &amp;&amp; echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' $PG_MAJOR &gt; /etc/apt/sources.list.d/pgdg.list
  
  # Add NodeJS to sources list
  RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -
  
  # Add Yarn to the sources list
  RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    &amp;&amp; echo 'deb http://dl.yarnpkg.com/debian/ stable main' &gt; /etc/apt/sources.list.d/yarn.list
  
  # Install dependencies
  COPY .docker/dev/Aptfile /tmp/Aptfile
  RUN apt-get update -qq &amp;&amp; DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade &amp;&amp; \
    DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
      build-essential \
      postgresql-client-$PG_MAJOR \
      nodejs \
      yarn=$YARN_VERSION-1 \
      $(cat /tmp/Aptfile | xargs) &amp;&amp; \
      apt-get clean &amp;&amp; \
      rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* &amp;&amp; \
      truncate -s 0 /var/log/*log
  
  # Create a directory for the app code
  ENV APP_HOME=/usr/src/app
  # RUN mkdir -p ${APP_HOME}
  WORKDIR ${APP_HOME}
  
  # Install production dependencies (Gems installation in
  # local vendor directory)
  COPY Gemfile Gemfile.lock ./
  ENV BUNDLE_FROZEN=true
  # Upgrade RubyGems and install required Bundler version 
  RUN gem update --system &amp;&amp; \
      gem install bundler:$BUNDLER_VERSION
  RUN bundle install
  
  ENV RAILS_ENV=$RAILS_ENV
  ENV RAILS_SERVE_STATIC_FILES=true
  ENV RAILS_LOG_TO_STDOUT=true
  
  COPY . .
  
  # Pre-compile Rails assets (master key needed)
  RUN yarn install
  RUN RAILS_ENV=production bundle exec rails assets:precompile
  
  # Set Google App Credentials environment variable with Service Account
  ENV GOOGLE_APPLICATION_CREDENTIALS=${APP_HOME}/config/whale_on_rails.key
  
  # Database environment variables
  ARG DATABASE_NAME
  ARG DATABASE_HOST
  ARG DATABASE_USER
  ARG DB_PWD
  ENV DATABASE_PASSWORD=$DB_PWD
  ENV DATABASE_NAME=$DATABASE_NAME
  ENV DATABASE_HOST=$DATABASE_HOST
  ENV DATABASE_USER=$DATABASE_USER
  
  RUN chmod +x ${APP_HOME}/.docker/script/entry
  ENTRYPOINT ["/usr/src/app/.docker/script/entry"]
  </code></pre>
  
  <p><strong>entrypoint</strong></p>
  
  <pre class="lang-sh prettyprint-override"><code>#!/usr/bin/env bash
  
  cd /usr/src/app
  
  # Create, migrate and seed the Rails production DB
  bundle exec rails db:prepare
  
  # Do some protective cleanup
  &gt; log/production.log
  rm -f tmp/pids/server.pid
  
  # Run the web service on container startup
  bundle exec rails server -e production -b 0.0.0.0 -p $PORT
  </code></pre>
  
  <p>Finally, I'm using the following command to build the container.</p>
  
  <p><code>$ gcloud builds submit --config cloudbuild.yaml</code></p>
  
  <p>This process works great. The problem comes in the next step...</p>
  
  <h2>Deploy the image using Cloud Run</h2>
  
  <pre class="lang-sh prettyprint-override"><code>$ gcloud beta run deploy whale-on-rails --image gcr.io/$PROJECT_ID/whale_on_rails \                                                                                              4m 58s Ruby 2.6.5
    --set-cloudsql-instances whale-on-rails-production \
    --region us-central1 --allow-unauthenticated --platform managed
  </code></pre>
  
  <p>In this step, I get this error in terminal:</p>
  
  <p><code>ERROR: (gcloud.beta.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.</code></p>
  
  <p>And checking out Cloud Run logs, I get this:</p>
  
  <pre><code>2019-10-24 13:03:35.448 CEST&lt;main&gt;: warning: timer_create failed: Success, signals racy
  2019-10-24 13:03:37.756 CEST=&gt; Booting Puma
  2019-10-24 13:03:37.756 CEST=&gt; Rails 6.0.0 application starting in production
  2019-10-24 13:03:37.756 CEST=&gt; Run `rails server --help` for more startup options
  2019-10-24 13:03:40.687 CESTExiting
  2019-10-24 13:03:40.687 CEST(erb):21:in `&lt;main&gt;': undefined local variable or method `“config' for main:Object (NameError)
  2019-10-24 13:03:40.687 CEST from /usr/local/lib/ruby/2.6.0/erb.rb:901:in `eval'
  2019-10-24 13:03:40.687 CEST from /usr/local/lib/ruby/2.6.0/erb.rb:901:in `result'
  2019-10-24 13:03:40.688 CEST from /usr/local/bundle/gems/activestorage-6.0.0/lib/active_storage/engine.rb:111:in `block (2 levels) in &lt;class:Engine&gt;'
  2019-10-24 13:03:40.688 CEST from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:72:in `class_eval'
  2019-10-24 13:03:40.688 CEST from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:72:in `block in execute_hook'
  ...
  2019-10-24 13:03:40.689 CEST from /usr/src/app/bin/rails:9:in `&lt;top (required)&gt;'
  2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
  2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
  2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
  2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
  2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `&lt;top (required)&gt;'
  2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
  2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `&lt;top (required)&gt;'
  2019-10-24 13:03:40.689 CEST from /usr/src/app/bin/spring:15:in `require'
  2019-10-24 13:03:40.689 CEST from /usr/src/app/bin/spring:15:in `&lt;top (required)&gt;'
  2019-10-24 13:03:40.689 CEST from bin/rails:3:in `load'
  2019-10-24 13:03:40.689 CEST from bin/rails:3:in `&lt;main&gt;'
  2019-10-24 13:03:41.492 CESTContainer called exit(1).
  </code></pre>
  
  <p>My question is, why I'm getting this error? Everything looks fine in the hole process and I've followed the tutorial step by step (the only difference is that they are using MySQL and here I'm using Postgres).</p>