How to Fix Docker 404 Not Found on Google Cloud Run


Troubleshooting “Docker 404 Not Found” on Google Cloud Run

When your Google Cloud Run service returns a “Docker 404 Not Found” error, it typically indicates that your containerized application is not listening on the port Cloud Run expects. Cloud Run automatically injects a PORT environment variable, defaulting to 8080, and expects your application to bind to this specific port for incoming requests.


1. The Root Cause

“Docker 404 Not Found” on Google Cloud Run usually means your containerized application isn’t listening on the port Cloud Run expects. Cloud Run expects applications to bind to the port specified by the PORT environment variable, which defaults to 8080.


2. Quick Fix (CLI)

If your application is listening on a specific, non-8080 port (e.g., 3000, 5000), you can explicitly tell Cloud Run which port your container uses during deployment. This temporary fix allows traffic to be routed correctly without modifying your application code.

gcloud run deploy YOUR_SERVICE_NAME \
    --image gcr.io/YOUR_PROJECT_ID/YOUR_IMAGE_NAME \
    --platform managed \
    --region YOUR_REGION \
    --port YOUR_APP_PORT_NUMBER
  • Replace YOUR_SERVICE_NAME with your Cloud Run service name.
  • Replace gcr.io/YOUR_PROJECT_ID/YOUR_IMAGE_NAME with the full path to your container image.
  • Replace YOUR_REGION with your service’s Google Cloud region (e.g., us-central1).
  • Replace YOUR_APP_PORT_NUMBER with the exact port your application listens on inside the container (e.g., 3000).

3. Configuration Check

The most robust solution is to ensure your application code within the Docker image explicitly listens on the PORT environment variable provided by Cloud Run. This makes your container portable and resilient to changes in deployment environments.

File to Edit: Your application’s main entry point file (e.g., server.js, app.py, main.go).

What to Change: Modify your application to read the PORT environment variable and bind to it, providing a fallback default if PORT is not set (though it will always be set by Cloud Run).

Example (Node.js):

const port = process.env.PORT || 8080; // Cloud Run sets PORT
app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

Example (Python Flask):

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, World!"

if __name__ == '__main__':
    # Cloud Run sets PORT environment variable
    app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))

After modifying your application code, rebuild your Docker image and redeploy your Cloud Run service without the --port flag:

docker build -t gcr.io/YOUR_PROJECT_ID/YOUR_IMAGE_NAME .
docker push gcr.io/YOUR_PROJECT_ID/YOUR_IMAGE_NAME
gcloud run deploy YOUR_SERVICE_NAME \
    --image gcr.io/YOUR_PROJECT_ID/YOUR_IMAGE_NAME \
    --platform managed \
    --region YOUR_REGION

4. Verification

After applying the fix, verify your service by checking its status, logs, and making a direct request.

  1. Get Service URL:

    gcloud run services describe YOUR_SERVICE_NAME \
        --platform managed \
        --region YOUR_REGION \
        --format "value(status.url)"
  2. Tail Cloud Run Logs: Look for messages indicating your application started successfully and is listening on the expected port.

    gcloud run services logs YOUR_SERVICE_NAME \
        --platform managed \
        --region YOUR_REGION \
        --follow
  3. Test with curl: Make a request to the service URL obtained in step 1.

    curl $(gcloud run services describe YOUR_SERVICE_NAME --platform managed --region YOUR_REGION --format "value(status.url)")

    You should receive a successful HTTP 200 response from your application instead of a 404.