On December 7, 2023, Google released the following video on using Rust for Cloud Run applications. This is an excellent getting started video. I have been working directly with the Google Cloud Run team since the beta release. I thought I would write an article taking their video, and JK Gunnink’s repository and article and take it to the next level.

This article is Part 1 of a three-part article. The first part implements container multi-stage builds. The second part updates the application to use the latest version of the Cargo package Hyper. The third part extends the application with new features.

Note: I am very experienced with Google Cloud and Docker, but I am a beginner with the Rust language.

Objectives

  • Reduce the size of the container image by using multi-stage builds. The container built in the video is about 1.68 GB. My goal is less than 200 MB. One benefit is to reduce the number of applications, tools, libraries, etc. that must be reviewed for security vulnerabilities.

Endpoint Summary

  • / – Return the Hello, Rustless! message.

Step 1: Build the container for local testing

I will start by cloning JK Gunnink’s repository.

Build the container

Get the container image size

Run the container

Test the container

Excellent, the code from the repository works correctly.

Step 2: Multi-Stage build

In this step, I will modify the Dockerfile to implement a multi-stage build.

Original Dockerfile

Modified Dockerfile

Build the new container

Get the container image size

That is a huge decrease in container image size. The container went from 1.68 GB to 81.9 MB. That will also save us money on storage costs and network bandwidth.

In Google Cloud Container Registry, the container image size went from 553.7 MB to 29.6 MB. Container registry uses optimization to reduce container image storage.

Build and Deploy to Cloud Run

Go to the Cloud Run Service URL. In my case it is https://rustless-e2v2ixiywa-uc.a.run.app/

Summary

This article shows how to use Docker multi-stage builds to reduce the size of container images.

There are several benefits:

  • Smaller images have fewer items to scan for vulnerabilities.
  • Smaller images reduce storage costs.
  • Smaller images reduce network costs and transfer time.

In part 2 of this article, we will enhance the application to:

  1. Update the version of the hyper Cargo package used to build the Cloud Run application. The original article uses Hyper version 0.14. The latest version is 1.0.1.

In part 3 of this article, we will enhance the application to provide several improvements:

  1. Create an endpoint that returns the public IP address of the caller. This involves processing request headers. Support the query parameter ?format=json to return JSON instead of HTML.
  2. Create an endpoint that displays the container’s environment when running inside Cloud Run. This is useful to see the environment variables and more. Support the query parameter ?format=json to return JSON instead of HTML.
  3. Create an endpoint that displays the HTTP request headers that the application receives. This is useful to debug problems where headers are part of the endpoints processing. Support the query parameter ?format=json to return JSON instead of HTML.

More Information

Photography Credit

I write free articles about technology. Recently, I learned about Pexels.com which provides free images. The image in this article is courtesy of Pixabay at Pexels. Note: This is the same image used in JK Gunnink’s article.