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!
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
- You can navigate to this URL to create a Google Cloud Project.
- Give your project a name and leave the rest of the settings as default.
- After the project is created, click ‘VIEW’ on the notification panel.
- 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.
- Click ‘+ Create Service’ in the top toolbar.
- Select Cloud Run (fully managed) as the deployment platform and choose a zone.
- Give your service a name
- Allow unauthenticated invocations, this is so that the public can access your instance.
- Click ‘Next’
- 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.
- 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.
- You can further edit settings like adding environmental variables, cloud-specific settings, machine specs, etc.
- Then finally, create!
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?