TechTorch

Location:HOME > Technology > content

Technology

Best Practices for Setting Public Ports in Google Cloud Run

April 13, 2025Technology1118
Best Practices for Setting Public Ports in Google Cloud Run Google Clo

Best Practices for Setting Public Ports in Google Cloud Run

Google Cloud Run is a fully managed, serverless compute service that enables developers to run containerized applications. A critical aspect of deploying applications on Cloud Run is ensuring they are accessible over the internet with the appropriate public ports. In this article, we will guide you through the process of setting up the correct public port for your Cloud Run service.

Understanding Cloud Run’s Port Assignment Mechanism

When deploying your application to Google Cloud Run, the platform dynamically assigns a public port. This means you don't need to specify a public port directly in your setup. Instead, your application should be designed to listen on the port specified by the PORT environment variable. This ensures seamless integration with Cloud Run's robust networking capabilities and optimal performance.

Preparing Your Application for Dynamic Port Assignment

To leverage Cloud Run's automatic port assignment feature, you need to make sure your application can handle the environment variable PORT. Here are the steps to prepare your application:

Step 1: Use the PORT Environment Variable in Your Code

Your application's code should be designed to listen on the port provided by the PORT environment variable. This can be achieved by reading the value of PORT from your application's environment and configuring your application to listen on that specific port. Here is an example in Node.js:

// app.jsconst http  require('http');const port  process.env.PORT || 8080;((req, res) > {  res.writeHead(200, {'Content-Type': 'text/plain'});  res.end('hello world
');}).listen(port, ()  {  console.log(`Server listening on port ${port}`);});

In this example, your application listens on the port provided by the PORT environment variable. If PORT is not set, it listens on port 8080.

Step 2: Use the Cloud Run Annotations for Better Compatibility

To ensure your container is fully compatible with Cloud Run, it's a good practice to use the appropriate annotations in your Cloud Build configuration. These annotations help Cloud Run understand how to run your application correctly. Here are some relevant annotations:

service* - Specifies the container image to use. entrypoint - Specifies the command or script to run. args - Specifies additional arguments to pass to the entrypoint.

For example:

# cloudbuild.yamlsteps:
- name: ''
  args: ['run', 'deploy', '--image', '', '--region', 'us-central1', '--platform', 'managed', '--project', 'your-project-id', '--allow-unauthenticated', '--set-env-vars', 'PORT8080', '--timeout', '1800s', '--max-age', '0s', '--manual-scaling', '--min-instances', '1', '--max-instances', '10', '--concurrency', '100']

In this example, the --set-env-vars flag sets the PORT environment variable to 8080 if it's not defined in the application.

Testing Your Cloud Run Service

Once your application is prepared, you can deploy it to Cloud Run using the Cloud Console, Cloud SDK, or Cloud Build. Here are the steps to test your service:

Step 1: Deploy Your Application

Use the Cloud Console or the Cloud SDK to deploy your application. For example, in the Cloud Console, you can deploy your application by navigating to the Cloud Run section and following the prompts to deploy the service using your container image.

Step 2: Test the Application

After deployment, you can test your application by visiting the public URL provided in the Cloud Console. You should see your application running and accessible over the internet using the correct public port.

Conclusion

Setting the public port for Cloud Run is essential for ensuring your application is accessible and performs optimally. By dynamically configuring your application to listen on the PORT environment variable, you can take advantage of Cloud Run's automatic port assignment feature, which simplifies deployment and improves compatibility. With the right setup, your application will be ready to handle millions of users seamlessly.

Further Resources

For more detailed information and advanced configurations, refer to the official documentation provided by Google:

Google Cloud Run Documentation Building and Deploying Cloud Run Applications