DEV Community

Robert Grootjen
Robert Grootjen

Posted on

GCP for beginners 2024: Build a simple web app with Cloud Run and Cloud Build through terminal

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?

  1. Auto scaling = Scales up or down depending on traffic
  2. Containerized applications = Run any stateless container
  3. Fully managed = No need to manage servers, google takes care of everything
  4. Pay per use = Pay only for the resources you use

What is Cloud Run used for?

  1. Web applications and API’s = Great for deploying microservices, Restful API’s, and as well web applications
  2. Data processing = Suitable for data transformation and ETL tasks
  3. Background jobs = Can handly simultanious jobs
  4. 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
Enter fullscreen mode Exit fullscreen mode

cd into the folder we just created

cd my-cloud-run-app
Enter fullscreen mode Exit fullscreen mode

Create a main.py file

nano main.py
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode

Create a requirements.txt with the following

Flask==2.0.1
Werkzeug==2.0.1
Enter fullscreen mode Exit fullscreen mode

Create a dockerfile

nano Dockerfile
Enter fullscreen mode Exit fullscreen mode
# 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"]
Enter fullscreen mode Exit fullscreen mode

Create a cloudbuid.yaml file

nano cloudbuild.yaml
Enter fullscreen mode Exit fullscreen mode
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'

Enter fullscreen mode Exit fullscreen mode

Now you should have something like this

files

Run cloud build to containerize the application

gcloud builds submit --config cloudbuild.yaml .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Remove container image

gcloud container images delete gcr.io/YOUR_PROJECT_ID/my-cloud-run-app --force-delete-tags

Enter fullscreen mode Exit fullscreen mode

Top comments (0)