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/html COPY 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.