CodexBloom - Programming Q&A Platform

GCP Cloud Run Service scenarios to Connect to Firestore with 'scenarios to establish a new connection' scenarios

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-26
gcp cloud-run firestore Python

I'm testing a new approach and I'm converting an old project and I've been banging my head against this for hours. I'm having trouble connecting my Cloud Run service to Firestore, and I'm getting the behavior `Failed to establish a new connection: [Errno 111] Connection refused`. I've deployed my service using the Python client library `google-cloud-firestore` version 2.3.0. In my `main.py`, I initialize the Firestore client like this: ```python from google.cloud import firestore client = firestore.Client() ``` I have verified that the service account associated with Cloud Run has the necessary Firestore permissions. I have tried accessing Firestore locally, and it works fine, so I suspect it could be an scenario with the Cloud Run environment. I set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable in my Cloud Run service configuration, pointing to the JSON key file for my service account, but it seems to have no effect. Here's the relevant part of my `Dockerfile`: ```dockerfile FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["python", "main.py"] ``` I've also tried using the Firestore emulator locally, which works perfectly. When I run the Cloud Run service, I notice that it starts successfully, but any attempt to access Firestore fails with the mentioned behavior. Is there a specific configuration or networking setting that I might be missing in my Cloud Run setup? How can I debug this connectivity scenario? For context: I'm using Python on macOS. Is there a simpler solution I'm overlooking? Any examples would be super helpful.