I had a full python script which for days couldn't run in Google Cloud Run but worked fine locally.
I built a small test application and I get the following error in Google Cloud logs:
textPayload: "terminated: Application failed to start: failed to load /usr/local/bin/uvicorn: exec format error"
Running the container locally worked fine.
docker run -e PORT=8080 -p 8080:8080 gcr.io/sophia-db784/cloud-run-test:v1.1
docker build -t gcr.io/sophia-db784/cloud-run-test:v1.1 .
docker push gcr.io/sophia-db784/cloud-run-test:v1.1
gcloud run deploy cloud-run-test --image gcr.io/sophia-db784/cloud-run-test:v1.1 --platform managed
The console terminal displays:
Deploying container to Cloud Run service [cloud-run-test] in project [sophia-db784] region [us-central1]
X Deploying...
- Creating Revision...
. Routing traffic...
Deployment failed
ERROR: (gcloud.run.deploy) Revision 'cloud-run-test-00005-k78' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.
Logs URL: https://console.cloud.google.com/logs/viewer?project=sophia-db784&resource=cloud_run_revision/service_name/cloud-run-test/revision_name/cloud-run-test-00005-k78&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22cloud-run-test%22%0Aresource.labels.revision_name%3D%22cloud-run-test-00005-k78%22
For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start
I expected a container which runs successfully in local docker should work fine in Google Cloud Run.
This is the script:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World from FastAPI on Google Cloud Run!"}
This is my Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
COPY . .
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir fastapi uvicorn
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Define environment variable
ENV NAME World
# Run Uvicorn when the container launches
CMD ["uvicorn", "cloud-run-test:app", "--host", "0.0.0.0", "--port", "8080"]
My real script has slightly more complexity in that I need to access a secret in Google Secret Manager, and I spent a few days trying to debug and thinking that was the problem in terms of permissions.
But with this simple app, I still get the exact same error.
As mentioned in this document
Note: If you build your container image on a ARM based machine, then it might not work as expected when used with Cloud Run. To solve this issue, build your image using Cloud Build.
Also you can refer thread1 & thread2 when building the docker image on macos
build your Docker container with the
--platform linux/amd64flag before deploying the image to Cloud Run
docker buildx build --platform linux/amd64 -t image-name:v1.1 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With