Aleix Conchillo Flaqué
Apr 29 · 7 min read

TLDR; This article is not very deep in technical terms about how Google Cloud Run works. Also, Google Cloud Run can do much more than running a simple website. The article just details a very simple use case and my first experience with it and it hopefully helps others that might be curious but don’t know where to start (even though the already existing Quickstarts are probably all you need).


I had a problem for a month. My dedicated server hard drives completely died (fsck didn’t even work on them) and we finally decided to get rid of it after 10 years. Unfortunately, my mom’s business website was running there. That’s right! It’s just a static website with very low traffic, but it’s still a very nice to have for her.

I didn’t do anything for a month until yesterday. I usually take the opportunity to try new things when something similar happens, I didn’t want to use any service where you upload the HTML files and that’s it. The first option that came to mine was to use Firebase hosting but I already did that for another single-page site, plus I already use Firebase at work.

Fortunately, Google Next ’19 happened a couple of weeks ago. Yesterday, I was listening to the Google Cloud Platform Podcast on my commute and the episode was about a new product that was announced during Next ‘19: Google Cloud Run. When the podcast was done I saw it clear: I’m just going to use Google Cloud Run!

But what is Google Cloud Run? It’s serverless done the right way. Excuse me?With Google Cloud Functions you could write a Node.js (or Go…) HTTP request handler in a file and upload that. After deploying that function you get a URL and you are set, you could just point to it. Google Cloud Run goes to the next level. Instead of uploading your functions’ files, you now deploy containers. Why is this better? Because now you can put whatever you want in your container. Instead of waiting for your preferred language to be supported with Cloud Functions(which could never happen), now you just create a container and run whatever you want in it. All you have to do is open port 8080 in the container (more on that later). And as with Cloud Functions it all scales out of the box and you can connect your containers to other Google Cloud products. Imagine the possibilities.

As of today, I’m pretty sure there must be a bunch of articles already talking about what I’m going to explain, but the experience was so painless, beautiful, simple and fast that I thought it was worth sharing. So, here it goes!


Create a new Google Cloud Platform project

In case this is the first time you create a new GCP (Google Cloud Platform) project, below is how you do it. If you already know how, just create a new project in the console and name it, let’s say MyWebsite, and skip to the next section.

First, go to Google Cloud Console and click Select a project at the top. This will open a modal window, click New Project.

Next, just enter the name of the project, we said it was MyWebsite and click Create. There’s a very important thing to remember in this step: the Project ID is mywebsite-239106, write it down as we will use it later.

Once the project is created select it from the same Select a project drop-down that we clicked at the very beginning.

Enable billing (and about pricing)

Unfortunately, I just realized you have to go through this step. I already had my billing enabled when I first tried Google Cloud Run, so I didn’t realize this was necessary until now. I’m just going to assume you don’t have any issues with this. At the end, running a low-traffic website is going to be “almost” free. See Google Cloud Run Pricing for more details.

Enable Google Cloud Run

Assuming you have your GCP project selected, just go to Google Cloud Run and click Start Using Cloud Run.

You should end up in the Google Cloud Run console.

Install and authorize Google Cloud SDK

If you already have Google Cloud SDK installed and you know what you are doing, you probably can skip this step (remember to update Google Cloud SDK, install the Beta components and select your newly created project).

If you are using OS X it’s very likely you have Homebrew installed. If you do, just type:

$ brew cask install google-cloud-sdk

If you don’t have Homebrew I would recommend you install it. If you don’t want to install it you can follow the installation instructions from Google Cloud SDK, same if you use GNU/Linux.

So, once Google Cloud SDK is installed you need to authorize it with the Google account you used to create your project. Type the following:

$ gcloud auth login

Next, install the Google Cloud SDK Beta components:

$ gcloud components install beta

Finally, select your project (remember the Project ID we wrote down before?):

$ gcloud config set project mywebsite-239016

Install Docker

Again, in OS X if you have Homebrew just type:

$ brew cask install docker

If not, just download Docker and follow the instructions. It’s pretty straight forward.

Create the container

Finally, the interesting part. What we are going to do next is deploy a simple HTML page and serve it through Google Cloud Run. Let’s first create a new directory and organize things a bit:

$ mkdir website
$ cd website
$ mkdir html nginx

In the html directory create an index.html file with this content:

<html>
<body>Hello from Google Cloud Run!</body>
</html>

To serve this file we just need a web server. We are going to use nginx. I said that Google Cloud Run uses containers, which means we can simply use an existing nginx container. The only problem with the default nginx container is that it serves files on port 80, however Google Cloud Run expects containers to serve things on port 8080. So, we will just provide a new configuration file to nginx later.

Create a new Dockerfile inside the website directory:

$ cat Dockerfile
FROM nginx
COPY html /usr/share/nginx/htmlCOPY nginx/default.conf /etc/nginx/conf.d/default.conf

In this Dockerfile we are just telling Docker to create a new container based on the nginx container and we are adding our html directory (that contains our index.html) plus an nginx configuration file that we will create next.

In the nginx directory create a default.conf file (note how we listen on port 8080):

$ cat nginx/default.conf
server {
listen 8080;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

After all this, you should have this directory structure:

$ tree
.
├── Dockerfile
├── html
│ └── index.html
└── nginx
└── default.conf

Note that in the html directory or elsewhere you could have added more files: more HTML files, CSS files, images, etc. If it’s a big website it’s probably better to upload content to Google Cloud Storage and keep the container small.

Build and deploy the container

Now we can build the container and push it. This is done with a Google Cloud SDK command:

$ gcloud builds submit --tag gcr.io/mywebsite-239106/hello

We have specified our project mywebsite-239106 and a name for our new Google Cloud Run service hello.

Finally, we can deploy the new container to Google Cloud Run:

$ gcloud beta run deploy --image gcr.io/mywebsite-239106/hello

If everything went well you should see a message like this:

Service [hello] revision [hello-00001] has been deployed and is serving traffic at https://hello-7ss2wutasq-uc.a.run.app

And it seems the generated URL is up and running!

You can also take a look at the Google Cloud Run console for more details:

After this you can make changes to your website and keep building and deploying the new container. Google Cloud Run will list all the revisions that you have deployed.

Assign a domain

The next thing you can do is assign a domain to the new Google Cloud Run service. I’m not going to go through this here because it requires you to update your domain DNS records and that really depends on the service you are using, but I have to say the process is very straight forward. Once the deployment is done you just need to click Manage Custom Domains (see image above).

Oh! And the best part about this is that certificates are already managed for you. I was happily surprised that without doing anything a Let’s Encrypt certificate was already setup for my domain and HTTP and HTTPS were both working like a charm.


And that was it!

By the way, I mentioned something about Firebase and work. We use Firebase and other Google Cloud Platform stuff at work. I just wanted to share a new project we’ve been working on for a few months now. It’s a pretty cool new concept for screen sharing and has a curious name too. Check it out: https://www.rumpus.co/

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Aleix Conchillo Flaqué

Written by

Doing stuff at Oblong Industries. Wrote software for LISA Pathfinder. Co-founded Scytl.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade