Description

Hands-on Challenge Day - Deploying with Docker on Google Cloud Run

Introduction

What is Google Cloud Run?

Google Cloud Run is a managed platform that enables you to run stateless containers that are invocable via web requests or Pub/Sub events. It is fully managed by Google Cloud, offering automatic scaling, high availability, and secure deployments. Cloud Run abstracts away all infrastructure management, so you can focus on what matters most — building great applications.

Key Features of Google Cloud Run:

Challenge Scenario

Imagine QuickServe API, a fast-growing startup that provides an API for food delivery services. As they scale, they need a solution that can handle unpredictable traffic spikes without the need for manual intervention. They also want to ensure their deployment process is as efficient and streamlined as possible to accelerate their go-to-market strategy.

Problem Statement: QuickServe API needs a scalable, managed platform to deploy their containerized application, capable of automatically adjusting to traffic patterns to ensure optimal performance and cost-effectiveness.

Your Mission: Use Google Cloud Run to deploy QuickServe API's application. Ensure the solution utilizes Cloud Run's ability to automatically scale and manage infrastructure, providing a resilient and efficient environment for the application.

Challenge: Deploying QuickServe API on Google Cloud Run

Requirements:

  1. Google Cloud Account: Access to a Google Cloud Platform account.

Steps:

  1. Prepare the Application:

    Sample Application (app.py):

    from flask import Flask, jsonify
    
    app = Flask(__name__)
    
    @app.route('/')
    def home():
        return jsonify({"message": "Welcome to QuickServe API!"})
    
    if __name__ == '__main__':
        app.run(debug=True, port=8080, host='0.0.0.0')
    
    

    Dockerfile:

    # Use an official Python runtime as a parent image
    FROM python:3.8-slim
    
    # Set the working directory in the container
    WORKDIR /app
    
    # Copy the current directory contents into the container at /app
    ADD . /app
    
    # Install any needed packages specified in requirements.txt
    RUN pip install --trusted-host pypi.python.org Flask
    
    # Make port 8080 available to the world outside this container
    EXPOSE 8080
    
    # Define environment variable
    ENV NAME QuickServeAPI
    
    # Run app.py when the container launches
    CMD ["python", "app.py"]
    
    
  2. Build and Push Docker Image to Google Artifacts Registry (GCR):

    docker build -t quickserve-api .
    gcloud config set project PROJECT_ID
    docker tag quickserve-api us-central1-docker.pkg.dev/$DEVSHELL_PROJECT_ID/tcb-cd-repo/quickserve-api:v1
    gcloud auth configure-docker
    docker push us-central1-docker.pkg.dev/$DEVSHELL_PROJECT_ID/tcb-cd-repo/quickserve-api:v1