I'm trying to deploy a gRPC service written in Go on Cloud Run, but I always get this error message on Cloud Run: The user-provided container failed to start and listen on the port defined provided by the PORT=5000 environment variable
The services itself works locally fine.
What do I have to change in my code? All the examples I saw online look the same and they are working (according to the author).
Thanks in advance!
UPDATE:
I checked the Log on GCP and the problem is in the connection with the Database. This step client.Schema.Create(context.Background())
fails and I get this message in the log: failed creating schema resources: querying server version dial tcp <ip_address>:5432: connect: connection timed out
The database is already connected to Cloud Run
main.go:
client, err := ent.Open("postgres database")
if err != nil {
log.Fatalf("failed opening connection to postgres: %v", err)
}
defer client.Close()
if err := client.Schema.Create(context.Background()); err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
server := grpc.NewServer()
// Register services
// ...
lis, err := net.Listen("tcp", ":5000")
if err != nil {
log.Fatalf("failed listening: %s", err)
}
if err := server.Serve(lis); err != nil {
log.Fatalf("server ended: %s", err)
}
Dockerfile:
FROM golang:1.21
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /pm_backend
EXPOSE 5000
CMD ["/pm_backend"]
Cloud Run Settings:
The user-provided container failed to start and listen on the port defined provided by the PORT=5000 environment variable
The error clearly does indicate that there is an issue with the specific defined incoming HTTP requests ports and the container is failing to listen on the expected port.
You need to troubleshoot the issue by checking the suggestions in Cloud Run troubleshooting official Documentation
To resolve this issue, rule out the following potential causes:
- Verify that you can run your container image locally. If your container image cannot run locally, you need to diagnose and fix the issue locally first.
- Check if your container is listening for requests on the expected port as documented in the container runtime contract. Your container must listen for incoming requests on the port that is defined by Cloud Run and provided in the PORT environment variable. See Configuring containers for instructions on how to specify the port.
- Check if your container is listening on all network interfaces, commonly denoted as 0.0.0.0.
- Verify that your container image is compiled for 64-bit Linux as required by the container runtime contract.
- Use Cloud Logging to look for application errors in stdout or stderr logs. You can also look for crashes captured in Error Reporting.
Based on your error log message:
failed creating schema resources: querying server version dial tcp <ip\_address\>:5432: connect: connection timed out
As @John Henley suggested in the comment it seems you are specifying an IP address as a connection string. But as suggested in the Documentation you need to use Unix Sockets.
And also as comment by @Boris BRESCIANI you can refer github for Example on connection with unix socket.