image
Published on

Running .NET Core 3.1.1 GCP Cloudrun

Authors

GCP loves .NET

With the release of .NET 3.1.1 on 14 Jan 20 and if you are looking to bootstrap a web app with the following goals in mind: Running on the latest framework version on PaaS

  • Minimum running cost
  • You can consider Google Cloudrun.

What is Google Cloudrun?

Cloud Run is a fully managed compute platform that automatically scales your stateless containers. Cloud Run is serverless: it abstracts away all infrastructure management, so you can focus on what matters most — building great applications.

The following post assumes you have prior knowledge to the following:

  • Using GCP
  • Creating a new Web App running on .NET Core using Visual Studio
  • Docker Containers

The section of this post will guide you through the following:

  • Create a new container in GCP ECR
  • Deploy GCP Cloud Run service

Create a new container image in GCP ECR

GCP Cloud Run runs on container image hosted in GCP ECR. Therefore, before creating a Cloud Run service, you are required to push a container image into GCP ECR.

  • Create a new Web App via Visual Studio 2019; you should be able to run your web app successfully. Results from running “dotnet run” command as follows:

Sample .NET web application

  • Ensure your Web App can listen to the port defined by the environment variable ‘PORT’ (or default to port 8080 if no environment variable is found). By default, GCP Cloud Run container contract runs on port 8080 (see references #1). Sample Program.cs to listen to the port defined by the environment variable.
  • Prepare a docker file for the web app

Test the docker by running docker build -t dotnetcore311cloudrun . and follow by docker run -p 8080:8080 dotnetcore311cloudrun (Note: When you execute docker build command, it is required to add a period(.) at the end of the command) You should be able to access the web app via localhost:8080.

Sample Docker Run .NET web application

  • Build and publish to Google ECR by executing this command: gcloud builds submit — tag gcr.io/{your-project-id}/dotnetcore311cloudrun. You should receive output that the image is successfully uploaded to GCP ECR.

Deploy GCP Cloud Run service

  • Deploy to GCP Cloud Run by executing this command: gcloud run deploy — image gcr.io/{<}your-project-id}/dotnetcore311cloudrun.
  • You will be asked a few questions including the target platform, the region of where the service should run from, the service name, whether to allow unauthenticated invocations. After you provide the information, you should receive output that the service is created successfully.
  • To view the Cloud Run service in GCP console, go to Cloud Run

CloudRun

If you try to access the URL provided in the Cloud Run console, you should be able to see your web app live on .run.app URL.

You have successfully created your first Cloud Run service! 🥳

References: