Step-by-Step Guide to Cloud Run Volume Mounts with Cloud Storage

Aryan Irani
Google Cloud - Community
6 min readNov 22, 2024

Welcome!

Cloud Run is a fully managed platform that allows you to run containerised applications without worrying about the underlying infrastructure. However, certain tasks, like handling files across services or accessing persistent storage, can become challenging in a stateless environment like Cloud Run.

Fortunately, a while ago the ability to configure Cloud Storage volume mounts, these challenges are easier to handle. This article will guide you through the setup of Cloud Storage volume mounts in Cloud Run and demonstrate their advantage through a simple use case.

If you want a video version of the blog, you can check out the video tutorial given below.

What is a Cloud Storage Volume Mount?

Cloud Storage volume mounts lets you bind a Cloud Storage bucket to your Cloud Run container file system, giving your application direct access to files stored inn the cloud without needing to download or upload them manually.

Why use Cloud Storage Volume Mounts?

  • Persistent Storage: Containers in Cloud Run are stateless, meaning their file systems are ephemeral. Volume mounts allow you to overcome this limitation by linking your service to Cloud Storage for persistent storage.
  • Improved Performance: Directly reading from and writing to Cloud Storage saves you from repeatedly uploading or downloading files, streamlining your workflow.
  • Seamless Integration: No need to modify your code significantly. Once a bucket is mounted, the service interacts with files as if they were part of the local filesystem.

Prerequisites

  1. Access to a Google Cloud Project
  2. Basic Understanding of Cloud Run
  3. Cloud Storage Setup
  4. Familiarity with Python and Flask

Simple Use case: Text File Uploader

This use case demonstrates how to upload text files directly to a Cloud Storage bucket using volume mounts in Cloud Run. By mounting the bucket as a directory in the container’s file system, the service can seamlessly write and store files persistently without managing uploads or downloads.

So lets get started.

Step 1: Create a Cloud Storage Bucket

The first step for this process would be to create a Cloud Storage bucket on your Google Cloud Console. To create a bucket on Google Cloud Storage, follow these steps:

  1. Go to the Google Cloud Console.
  2. Navigate to Storage → Buckets → Create Bucket. (Save the name of the bucket)
  3. Specify the Bucket name, region and the required details.
  4. Click Create.

Step2: Build and Deploy your Cloud Run Service

Now that we have the Cloud storage bucket created, its time to build and deploy the Cloud Run service. This step also involved configuring volume mounts for the service.

  1. Go to Google Cloud Run.
  2. Click on Deploy Container and click on Service.

3. On creating a new service, you will be asked to fill the following details.

4. Once you have filled the required details, scroll down to the Container(s), Volumes, Networking, Security section. This is where we will be configuring our Volume mounts.

5. Navigate to Volumes and click on ADD VOLUME.

6. On clicking Add Volume, you will be asked to specify the Volume Type, which in this case is Cloud Storage Bucket. On selecting the volume type, you have to select the Cloud Storage Bucket that we just created.

7. Once you create the volume mount, navigate back to CONTAINER(S) and open the Container that is already created and click on Volume Mounts.

8. On clicking Mount Volume, you will be asked to give it a name followed by the mount path. You can set it to /mnt/storage .

Understanding the Mount Path in Volume Mounts

When configuring a Cloud Storage volume mount in Cloud Run, the mount path is the location inside your container where the Cloud Storage bucket will be accessible. Think of it as creating a “virtual folder” within your container that points directly to the contents of your Cloud Storage Bucket.

For example, we have set the mount path to /mnt/storage, any file you write to /mnt/storage in your container is automatically stored in your specified Cloud Storage bucket.

9. On clicking Create, you will be directed the editor where we can now add our code and deploy the service.

This is the code that we are going to be using for this article. Lets breakdown and understand the code.

import os 
from flask import Flask, request

bucket_mount_path = os.environ.get('MOUNT_PATH', '/mnt/storage')

The bucket_mount_path is the location inside the container where the Cloud Storage bucket is mounted. It's retrieved from an environment variable (MOUNT_PATH) set during deployment. If no path is provided, it defaults to /mnt/storage. This directory acts as a bridge between the container's local file system and the bucket's contents.

def upload(request):
# Get the text from the POST request
text = request.form.get('text')

This retrieves the text from the incoming POST request. The form data is expected to contain a field called text.

# Define the file path in the mounted bucket
file_path = os.path.join(bucket_mount_path, "uploaded_file.txt")

The file_path variable defines where the uploaded file will be stored. The file is created inside the mounted bucket directory (bucket_mount_path), with the name uploaded_file.txt. This path represents the file in the Cloud Storage bucket as if it were part of the container's local file system.

if not os.path.exists(bucket_mount_path):
os.makedirs(bucket_mount_path)

Before writing the file, we check whether the mount path exists using os.path.exists(). Although Cloud Run should mount the directory automatically, this is a safety check. If the directory doesn't exist, it is created using os.makedirs().

with open(file_path, 'w') as f:
f.write(text)
return f"File successfully written to {file_path}."

The text received from the POST request is written to the file uploaded_file.txt in the mounted Cloud Storage bucket. The file is opened in write mode ('w'), and the content is saved to the bucket via the mount path.

If the file is successfully written, the function returns a success message containing the file path. If an error occurs, the exception is caught, and an error message is logged and returned to the client with an HTTP status code 500 (Internal Server Error).

Once you are done with the code, change the Function entry point to upload, and click on Save and Redeploy.

Check the Output

After redeployment, test the service using a curl command:

curl -X POST -F 'text=Hello, Cloud Storage!' https://<your-cloud-run-url>/upload

You can find your Cloud Run url from the top after deployment.

Here you can see it has printed the success message and if I check my Cloud Storage Bucket, the file has been created.

Conclusion

Cloud Run’s volume mounts with Cloud Storage simplify managing persistent data in stateless containerized environments. By seamlessly binding a Cloud Storage bucket to your container’s filesystem, you can efficiently handle files without modifying much of your existing application code.

This solution enhances both performance and usability, as it removes the need for manual uploads and downloads, allowing your application to interact with files as though they were part of the local filesystem.

--

--

Google Cloud - Community
Google Cloud - Community

Published in Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Aryan Irani
Aryan Irani

Written by Aryan Irani

I write and create on the internet :)

No responses yet