10-Minute Builds: Serverless Docker With a Simple CI/CD Pipeline
In this blog post, I will write about my favorite cloud service to date: Google Cloud Run. Read on to find out more!
Join the DZone community and get the full member experience.Join For Free
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
- Very basic knowledge of 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.
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 set up 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 log in 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.
Published at DZone with permission of Ryan Susana. See the original article here.
Opinions expressed by DZone contributors are their own.