End-to-End Project: Deploy Python Flask App to Cloud Run using gcloud CLI¶
In this project, we will walk through the complete process of deploying a containerized Python Flask application to Google Cloud Run. We will start by creating the application, testing it locally, pushing the Docker image to Artifact Registry, and finally deploying it to Cloud Run.
Prerequisites¶
- Google Cloud Project: A GCP project with billing enabled.
- gcloud CLI: Installed and initialized (
gcloud init). - Docker: Installed and running on your local machine.
Step 1: Create the Python Flask Application¶
First, let's create the application files.
-
Create
main.py:# main.py import os from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): name = os.environ.get("NAME", "World") return f"Hello, {name} from Cloud Run!" if __name__ == "__main__": app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080))) -
Create
requirements.txt:Flask==3.0.3 -
Create
Dockerfile:# Use an official Python runtime as a parent image FROM python:3.12-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 8080 available to the world outside this container EXPOSE 8080 # Run main.py when the container launches CMD ["python", "main.py"]
Step 2: Build and Test Locally¶
Before deploying to the cloud, it's best practice to verify the application works locally.
-
Build the Docker image:
docker build -t python-flask-app . -
Run the container:
docker run -p 8080:8080 python-flask-app -
Verify: Open your browser to
http://localhost:8080or run:You should see:curl localhost:8080Hello, World from Cloud Run! -
Stop the container: Press
Ctrl+Cto stop the running container.
Step 3: Create Artifact Registry Repository¶
Now we need a place to store our Docker image in Google Cloud.
-
Enable the Artifact Registry API:
gcloud services enable artifactregistry.googleapis.com -
Create the repository:
gcloud artifacts repositories create my-docker-repo \ --repository-format=docker \ --location=us-central1 \ --description="My Docker Repository"
Step 4: Build and Push Image to Artifact Registry¶
-
Configure Docker authentication:
gcloud auth configure-docker us-central1-docker.pkg.dev -
Set your Project ID:
export PROJECT_ID=$(gcloud config get-value project) -
Build the image for Cloud Run (Linux/AMD64): Note: We use
--platform linux/amd64to ensure compatibility with Cloud Run, especially if you are building on an Apple Silicon (M1/M2/M3) machine.docker build --platform linux/amd64 -t us-central1-docker.pkg.dev/$PROJECT_ID/my-docker-repo/python-flask-app:v1 . -
Push the image:
docker push us-central1-docker.pkg.dev/$PROJECT_ID/my-docker-repo/python-flask-app:v1
Step 5: Deploy to Cloud Run¶
Now deploy the image as a serverless service.
-
Enable Cloud Run API:
gcloud services enable run.googleapis.com -
Deploy the service:
gcloud run deploy python-flask-service \ --image=us-central1-docker.pkg.dev/$PROJECT_ID/my-docker-repo/python-flask-app:v1 \ --allow-unauthenticated \ --region=us-central1 \ --platform=managed \ --port=8080
Step 6: Verify the Deployment¶
Once the deployment finishes, gcloud will output a Service URL.
-
Access the URL: Click the link or run curl:
# Replace with your actual Service URL curl https://python-flask-service-wdq23423-uc.a.run.appYou should see the same "Hello, World from Cloud Run!" message, now served from Google Cloud.
Step 7: Cleanup¶
To avoid incurring charges, delete the resources you created.
-
Delete the Cloud Run service:
gcloud run services delete python-flask-service --region=us-central1 --quiet -
Delete the Artifact Registry repository:
gcloud artifacts repositories delete my-docker-repo --location=us-central1 --quiet
Conclusion¶
You have successfully completed an end-to-end DevOps workflow: coding a Python app, containerizing it, pushing it to a cloud registry, and deploying it to a serverless platform using the command line.