0

Similarly to Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable I cannot start my container because it does not (need to) listen on a port. It is a Discord bot that just needs outbound connections for the APIs.

Is there a way I can get around this error? I've tried listening on port 0.0.0.0:8080 using socket module with

import socket

s = socket.socket()
s.bind(("0.0.0.0", 8080))
s.listen()
davide m.
  • 452
  • 4
  • 19

1 Answers1

2

Cloud Run is oriented to request-driven tasks and this explains Cloud Run's listen requirement.

Generally (!) clients make requests to your Cloud Run service endpoint triggering the creation of instances to process the requests and generate responses.

Generally (!) if there are no outstanding responses to be sent, the service scales down to zero instances.

Running a bot, you will need to configure Cloud Run artificially to:

  1. Always run (and pay for) at least one instance (so that the bot isn't terminated)
  2. Respond (!) to incoming requests (on one thread|process)
  3. Run your bot (on one thread|process)

To do both #2 and #3 you'll need to consider Python multithreading|multiprocessing.

For #2, the code in your question is insufficient. You can use low-level sockets, but it will need to respond to incoming requests and so you will need to implement a server. It would be simpler to use e.g. Flask which gives you an HTTP server with very little code.

And this server code only exists to satisfy the Cloud Run requirement, it is not required for your bot.

If I were you, I'd run the bot on a Compute Engine VM. You can do this for Free.

If your bot is already packaged using a container, you can deploy the container directly to a VM.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • I was trying to set up a VM but even if I've set up the configuration as the [free tier](https://cloud.google.com/free/docs/free-cloud-features#free-tier-usage-limits) suggests, it still shows that the VM will be charging a monthly amount. I am worried that when the doc says "Only available in the specified region" it means "Your Google's account region" rather than "Your VM region" – davide m. Aug 28 '22 at 09:01
  • For Google Cloud resources "Regions" always applies to resources. I think (!?) there isn't a concept of regions for Google accounts. There *may* be costs associated with running resources on the Free Tier, it is good to be diligent. You will be able to run a VM for free just ensure you meet the [requirements](https://cloud.google.com/free/docs/free-cloud-features#compute) – DazWilkin Aug 28 '22 at 15:14