10-minute builds: Serverless Docker with CI/CD pipeline

In this blog post I will write about my favorite cloud service to date: Google Cloud Run. This service is fairly unique, as it allows you to deploy custom Docker workloads at scale, any scale, even at zero. This means that you only pay for what you actually use. If your service is not being called, you won’t be charged for a non-running Docker container. It’s basically a ‘Dockerized’ Cloud Function/AWS Lambda/Azure Function.

Google Cloud has released some features that now allow you to deploy a GitHub repository with a Dockerfile to Cloud Run. The service also automatically sets up a configurable CI/CD pipeline for your service. This is powerful! This might be the holy grail of cloud developer ecstasy.

Okay, wait let me calm down.

Now you can write your Cloud Functions in C++, Assembly, Rust, Dart, R, Brainfuck, whatever you want. As long as it fits in a Docker container.

Let me show you how!

Prerequisites

For this experiment you will need:

  • A GitHub account
  • A Google Cloud account
  • Knowledge on Docker

Step 1: Create your GitHub repository

You need some type of repository to host your code. Bitbucket is also an option.

If you’re lazy and you want to skip step 2, you can clone the one I built: https://github.com/RyanSusana/cloud-run-go

Step 2: Build your container

This is the fun part, where you write your actual code. Pick a language, pick a framework, write some logic, and pack it in a Docker container.

There is one rule: Your server must listen on the HTTP port specified with the PORT environmental variable (usually 8080). If you used my repository, the Go framework that I used does this by default.

Here are some generalities:

  • Pick a programming language/runtime with a fast cold start time
  • You are not limited to one endpoint within your containers
  • Your entire repository is cloud agnostic

Step 3: Create your Google Cloud Project

  1. You can navigate to this URL to create a Google Cloud Project.
  2. Give your project a name and leave the rest of the settings as default.
  3. After the project is created, click ‘VIEW’ on the notification panel.
  4. Next, you must enable billing.

Step 4: Deploy!

Navigate to this URL to set up a Cloud Run Service. This is a simple 2-step dialog, 4 if you want to set up the CI/CD pipeline. I won’t show images of the dialog, as I don’t think it adds to the clarity of this post.

After you complete these steps, a pipeline will be setup to redeploy your service every time you push a change.

  1. Click ‘+ Create Service’ in the top toolbar.
  2. Select Cloud Run (fully managed) as the deployment platform and choose a zone.
  3. Give your service a name
  4. Allow unauthenticated invocations, this is so that the public can access your instance.
  5. Click ‘Next’
  6. Select the “Continuously deploy new revisions from a source repository” and set it up with Cloud Build. This will trigger you to login to GitHub and select a repository. You might need to enable some APIs.
  7. Click next, in this dialog you can configure where your Dockerfile is located. The default should be fine if the Dockerfile is in the root of your master branch.
  8. You can further edit settings like adding environmental variables, cloud-specific settings, machine specs, etc.
  9. Then finally, create!
Deployed Cloud Run service

Conclusion

You now know how to continuously deploy Docker containers to the Cloud. Feel free to explore the other Cloud Run features. Here are some things to check out:

  • Manage traffic between revisions
  • Deploy Cloud Run to Kubernetes instead
  • Edit CI/CD settings, or better yet save your CI/CD as YAML code.

Connect with me

I’m usually down for a spar session. Maybe even a new code homie?

--

--

--

I like code, chess, reggae & hip-hop. 🇸🇽

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

price of making an app

Some Linux Links, please.

React Native challenges and benefits in 2021

My Visual Studio Code extensions for PowerShell development

How to get a job in IT without a degree — talking with Bartek Igielski, SD’s Head of Front-End…

How to Recognize the Antipattern

Hard-coded Mutex used in Bazar Backdoor sample

NoCode 2022 : #100DaysOfNoCode

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ryan Susana

Ryan Susana

I like code, chess, reggae & hip-hop. 🇸🇽

More from Medium

AWS CDK Example For API Gateway And SNS Integration

Amazon API Gateway HTTP Errors

Access your AWS Lambda Logs with Grafana

Running a Redis cluster on AWS with Terraform as IaC and Github Actions As CI/CD