What is Cloud Run?
Cloud run is a fully managed compute platform managed by Google Cloud that automatically scales containers and it allows developers to deploy containerized applications in minutes
What are the key features of Cloud Run?
- Auto scaling = Scales up or down depending on traffic
- Containerized applications = Run any stateless container
- Fully managed = No need to manage servers, google takes care of everything
- Pay per use = Pay only for the resources you use
What is Cloud Run used for?
- Web applications and API’s = Great for deploying microservices, Restful API’s, and as well web applications
- Data processing = Suitable for data transformation and ETL tasks
- Background jobs = Can handly simultanious jobs
- Event-driven applications = Works perfectly with event sources like cloud storage and pub/sub
It’s a solid combination of Cloud Functions and App Engine, best of both worlds and it can be used as a PaaS or Faas.
Cloud Run in a Metaphor
Imagine you’re running a food truck business. Each containerized application you deploy is like a food truck.
You can design and customize your food truck however you like, with any equipment, recipes (code), and ingredients (dependencies).
Now, think of Cloud Run as a food truck park.
The food truck park is the managed platform where your food trucks operate.
This park offers several features:
First, it provides flexibility—you can bring any type of food truck, serving any cuisine (programming language or framework).
Second, the park has a smart system that monitors customer traffic. When there are more customers, more food trucks are called in to handle the demand. When there are no customers, the food trucks are sent away to save costs.
Lastly, you don’t have to worry about maintaining the park infrastructure (like cleaning, security, or utilities).
The park management (Google Cloud) takes care of all that, so you can focus on your food (application) and customers (users).
How to build a cloud run through cloud shell (Here's the video)
Create a dir
mkdir my-cloud-run-app
cd into the folder we just created
cd my-cloud-run-app
Create a main.py file
nano main.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, Cloud Build!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Create a requirements.txt with the following
Flask==2.0.1
Werkzeug==2.0.1
Create a dockerfile
nano Dockerfile
# Use the official Python image from the Docker Hub
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy the requirements file and install dependencies
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 8080
# Run the application
CMD ["python", "main.py"]
Create a cloudbuid.yaml file
nano cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-cloud-run-app', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/my-cloud-run-app']
images:
- 'gcr.io/$PROJECT_ID/my-cloud-run-app'
Now you should have something like this
Run cloud build to containerize the application
gcloud builds submit --config cloudbuild.yaml .
Deploy the cloud run (Make sure you put your Project ID and preferred region for example: us-central1)
gcloud run deploy my-cloud-run-app \
--image gcr.io/PROJECT_ID/my-cloud-run-app \
--platform managed \
--region YOUR_PREFERRED_REGION \
--allow-unauthenticated
After everything has been succesfully deployed, it will give you an URL to open/access your web app and it should say "Hello, Cloud Builders!"
Now make sure to remove it, after you are done! If not GCP will charge you.
Remove cloud run
gcloud run services delete my-cloud-run-app --platform managed --region YOUR_PREFERRED_REGION
Remove container image
gcloud container images delete gcr.io/YOUR_PROJECT_ID/my-cloud-run-app --force-delete-tags
Top comments (0)