Cloud Run is an excellent platform to efficiently run and manage your applications while abstracting away infrastructure management. This practical guide will take you through the steps to deploy a service to Cloud Run directly from your source code.
Compute platform: A compute platform is a virtual environment providing essential resources such as processing power, memory, and storage, required for running applications and executing tasks.
Managed compute platform: A managed compute platform is a cloud-based service that automates the management of computing resources, including virtual machines, containers, and serverless functions, to run applications. It handles infrastructure management tasks like provisioning, scaling, and patching, allowing developers to concentrate on writing code.
Containers: Containers are portable, lightweight, and self-contained units that bundle an application, its dependencies, and configuration files. They enable developers to deploy and run applications consistently across various environments, ensuring isolation from the underlying infrastructure.
Stateless containers: Stateless containers are a type of container that do not store or maintain any internal state between requests. They process each request as an isolated transaction, making them suitable for horizontally scalable and fault-tolerant applications.
Scaling: Scaling refers to the process of adjusting the amount of compute resources allocated to an application based on its current demand. This ensures that an application can handle workload fluctuations without compromising performance or availability.
Automatic scaling: Automatic scaling is a feature offered by managed compute platforms that dynamically adjusts the number of instances or resources allocated to an application based on real-time demand. This allows applications to efficiently handle varying workloads by automatically scaling up or down as needed.
Armed with this understanding of key concepts, you're prepared to tackle the practical aspects of deploying a service to Cloud Run from your source code.
Cloud Run is a managed compute platform that allows you to run stateless containers that are automatically scaled to meet incoming requests. It offers a fast and flexible way to deploy and manage your applications without worrying about infrastructure management. This guide will walk you through the steps to deploy a service to Cloud Run from source code.
Before you get started, make sure that you have the following prerequisites in place:
- A Google Cloud Platform account
- A project created in the Google Cloud Console
- Google Cloud SDK installed on your local machine
- Docker installed on your local machine
- Source code for the application that you want to deploy
Step 1: Develop Your Application
First, create a new directory for your project and navigate to it:
mkdir simple-cloud-run-service cd simple-cloud-run-service
Create a file named
main.py and add the following content:
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello, World!" if __name__ == '__main__': app.run(host='0.0.0.0', port=8080)
requirements.txt file in the same directory with the following content:
Step 2: Containerize Your Application
The first step in deploying a service to Cloud Run is to containerize your application. This involves creating a Docker image of your application that can be run as a container. To containerize your application, you need to create a
Dockerfile in the root directory of your application.
Here's an example
# Use an official Python runtime as a parent image FROM python:3.9-slim-buster # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install the required packages RUN pip install --no-cache-dir -r requirements.txt # Make port 8080 available to the world outside this container EXPOSE 8080 # Run the application CMD ["python", "main.py"]
In this example, we're using Python as our runtime and installing the required packages specified in the
requirements.txt file. Replace
main.py with the name of your application's main file.
Step 3: Deploy Your Service to Cloud Run
Once you've created your Dockerfile, you can deploy your service by running the following command in the root directory of your application:
gcloud run deploy <SERVICE_NAME> \ --source . --region <REGION> \ --platform managed
<SERVICE_NAME> with a name for your Cloud Run service, and
<REGION> with the region where you want to deploy your service.
This command will deploy your service to Cloud Run and create a URL that you can use to access your service.
In this guide, we walked you through the steps to deploy a service to Cloud Run from source code. By following these steps, you can quickly and easily deploy your applications to Cloud Run and take advantage of its fast and flexible managed compute platform. Remember to test your service thoroughly before deploying it to production.