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
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.