Hands-on Challenge Day - Deploying with Docker on 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.
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.
Prepare the 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')
# 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"]
Build and Push Docker Image to Google Artifacts Registry (GCR):
tcb-cd-repo
us-central1
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