GCP for beginners 2024: Build a simple web app with Cloud Run and Cloud Build through terminal
Published: 2024-06-23 16:37:34

**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)](https://youtu.be/rGxegu0Hsfc?si=F5EU4MXcUXEYALmX)

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

![files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8phut89esvrktz99jv21.png)



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

```