<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sumiki Ohsawa</title>
    <description>The latest articles on DEV Community by Sumiki Ohsawa (@sumiki).</description>
    <link>https://dev.to/sumiki</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F431646%2F51f7f9b2-0b8c-4284-a752-0a148b6d913d.jpeg</url>
      <title>DEV Community: Sumiki Ohsawa</title>
      <link>https://dev.to/sumiki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sumiki"/>
    <language>en</language>
    <item>
      <title>Rails6 on Google Cloud Run</title>
      <dc:creator>Sumiki Ohsawa</dc:creator>
      <pubDate>Tue, 14 Jul 2020 13:50:16 +0000</pubDate>
      <link>https://dev.to/sumiki/rails6-on-google-cloud-run-5k3</link>
      <guid>https://dev.to/sumiki/rails6-on-google-cloud-run-5k3</guid>
      <description>&lt;h2&gt;
  
  
  What makes me look into it?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It works if it is containerized, no version requirements &lt;/li&gt;
&lt;li&gt;It is serverless, pay as you use, it is good for experimental projects.&lt;/li&gt;
&lt;li&gt;Gcloud offers autoscaling (fully managed).&lt;/li&gt;
&lt;li&gt;Gcloud console. I like how google implements web cloud console. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Source Code
&lt;/h2&gt;

&lt;p&gt;The source code I tried in this article is here.&lt;br&gt;
&lt;a href="https://github.com/sumiki/cloud_run_rails"&gt;https://github.com/sumiki/cloud_run_rails&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Install Rails
&lt;/h2&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rbenv install 2.7.1
gem install rails 
rails new cloud_run_rails -o
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h6&gt;
  
  
  I tried rails without a database this time just to see how cloud run works.
&lt;/h6&gt;

&lt;p&gt;Update rails to Edge (There is no requirement to ruby versions, so try edge)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Gemfile
gem 'rails', github: 'rails/rails'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Add JS
&lt;/h2&gt;

&lt;p&gt;There are no life without javascript so add some code and locally run precompile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RAILS_ENV=production bundle exec assets:precompile 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Dockerlize
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Gemfile
FROM ruby:2.7
WORKDIR /app
COPY Gemfile Gemfile.lock ./
ENV RAILS_ENV production
RUN bundle install
COPY . .
EXPOSE 3000
ENV RAILS_SERVE_STATIC_FILES true
CMD ["bin/rails", "server", "-b", "0.0.0.0"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Build docker container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t cloud_run_rails:0.0.1 .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Run it locally&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d -it -p  3000:3000  cloud_run_rails:0.0.10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploy to Cloud Run
&lt;/h2&gt;

&lt;p&gt;Push container to cloud repository&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GOOGLE_CLOUD_PROJECT=&amp;lt;your project name&amp;gt;

docker tag cloud_run_rails:0.0.1 us.gcr.io/${GOOGLE_CLOUD_PROJECT}/cloud_run_rails:0.0.1

docker push us.gcr.io/${GOOGLE_CLOUD_PROJECT}/cloud_run_rails:0.0.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud run deploy --image=us.gcr.io/${GOOGLE_CLOUD_PROJECT}/cloud_run_rails:0.0.1 --platform managed --concurrency 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is running&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TmhiW4dp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/61ouavs64343fff2hs9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TmhiW4dp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/61ouavs64343fff2hs9w.png" alt="Running Rails on Cloud Run"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  A problem I saw
&lt;/h2&gt;

&lt;p&gt;There is one thing I got stuck and could not figure out for a day.&lt;br&gt;
I was able to run rails app development mode on cloud run but not production mode.&lt;/p&gt;

&lt;p&gt;In the cloud run document there is a command to dockerize the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud builds submit --tag us.gcr.io/${GOOGLE_CLOUD_PROJECT}/cloud_run_rails:0.0.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://cloud.google.com/sdk/gcloud/reference/builds/submit"&gt;https://cloud.google.com/sdk/gcloud/reference/builds/submit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This makes rails in production mode not work on cloud run.&lt;br&gt;
This command is supposed to build a Docker container and push it to the repository.&lt;br&gt;
But it looks like it happened remotely on gcloud. It transfers the source code using git and that’s end up miscopying config/master.key and compiled assets.&lt;br&gt;
We can build container locally and use .dockerignore to prevent copying files not needed on the container.&lt;/p&gt;
&lt;h2&gt;
  
  
  Terraform
&lt;/h2&gt;

&lt;p&gt;We are also able to manage the cloud resources using terraform.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform apply -var-file=prod.tfvars
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# tf/main.tf
provider "google" {
 version = "3.5.0"

 credentials = file(var.credentials_path)

 project = var.project_name
}

resource "google_cloud_run_service" "default" {
 name     = var.cloud_run_name
 location = "us-west1"

 template {
   spec {
     containers {
       image = var.docker_image_path
     }
   }
 }

 traffic {
   percent         = 100
   latest_revision = true
 }
}

data "google_iam_policy" "noauth" {
 binding {
   role = "roles/run.invoker"
   members = [
     "allUsers",
   ]
 }
}

resource "google_cloud_run_service_iam_policy" "noauth" {
 location    = google_cloud_run_service.default.location
 project     = google_cloud_run_service.default.project
 service     = google_cloud_run_service.default.name

 policy_data = data.google_iam_policy.noauth.policy_data
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>googlecloud</category>
      <category>rails</category>
      <category>webdev</category>
      <category>terraform</category>
    </item>
  </channel>
</rss>
