Run .NET 6 containerized applications on Google Cloud Run

Introduction

Google Cloud run is a platform as a service offering that lets you run frontend, backend services, and APIs, and deploy websites and applications without the need to manage infrastructure.

In this article, you will learn to containerize the .NET 6 application, push container images to the cloud artifact registry, and run application container images in the cloud-run environment.

Prerequisites

This is a hands-on tutorial and you will need a Google Cloud free tier account to leverage cloud services. Google Cloud provides 400 $ worth of free credits using a free tier account.

Step-by-step tutorial to step up free tier account Google Cloud Free Credits - Google Cloud Tutorials.

In this article, we will cover the following topics.

  • Set up a new .NET 6 application using cloud shell and build container Image
  • Push container image to cloud artifact registry
  • Run containers on Google Cloud run

Step 1. Set up a new .NET 6 application using cloud shell

Visit Google Cloud console https://cloud.google.com/ and sign in using your free trial account.

In the Google Cloud search console type "cloud shell" and open a new cloud shell terminal.

Create a new .NET 6 project using the below command

dotnet new mvc --framework "net6.0" -o cloudrunblogdemo

Navigate to the project folder

cd cloudrunblogdemo

Publish the .NET 6 project using the below command

dotnet publish -c Release

In the cloud shell navigate to Project Explorer and create a new Dockerfile.

Copy and paste the below code.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 8080
ENV ASPNETCORE_URLS=http://*:8080

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["cloudrunblogdemo.csproj", "."]
RUN dotnet restore "./cloudrunblogdemo.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "cloudrunblogdemo.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "cloudrunblogdemo.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "cloudrunblogdemo.dll"]

Project Explorer

To build a custom image from the cloud shell terminal execute the below command.

docker build -t cloud-blog-v1 -f Dockerfile .

To display a list of all docker images execute the below command.

docker images

Repository

Step 2. Push Container Image to Google Artifact Registry

Google Cloud artifact registry is a full registry is a fully managed service used to store container images and non-container artifacts.

Search "Artifact Registry" in the Google search console and enable the Artifact registry API.

Create a new artifact registry with the below details.

Artifact Registry

Region

Learn more about Google Cloud data encryption here Google Cloud Storage Encryption - Cloud KMS - Google Cloud Tutorials.

Review the configuration and click on Create.

Before you push the Docker image to the Artifact Registry, you must tag it with the repository name.

docker tag cloud-blog-v1 us-east1-docker.pkg.dev/disco-serenity-412905/artifactdemo/cloud-blog-v1

Before the container image is pushed to the artifact registry Google Cloud CLI needs to authenticate requests to the artifact registry.

To push the docker image in the artifact registry execute the below command.

docker push us-east1-docker.pkg.dev/disco-serenity-412905/artifactdemo/cloud-blog-v1

Cloud

Accept authentication pop-up.

To learn more Google Cloud artifact registry refer to Store Container Images In Google Cloud Artifact Registry - Google Cloud Tutorials

Step 3. Run containers in Google Cloud Run

Once your application container image is stored in the Google Artifact Registry, you can easily deploy it to Google Cloud Run.

In the Google Cloud console search "cloud run" and click on create service.

In this tutorial, we will deploy a container image from the artifact registry to the cloud-run service.

Select the container image from the artifact registry.

Cloud run

Allow unauthenticated invocations.

Select the CPU always allocated option.

In auto scaling configuration select a minimum of 1 instance and a maximum of 10 instances.

Authentication

Specify container port as 8080.

Allow All Traffic

Select default configuration for other parameters.

Review the configuration and click on Create.

Once the cloud-run service is ready it will generate a unique URL to access the application.

URL

.NET 6 application is up and running on the Google Cloud run platform.

Visit the Logs, networking, and Metrics tab to monitor cloud-run service.

In this article, you have learned how to run container applications in a Google Cloud environment.

In the upcoming articles, we will learn to deploy microservices-based applications using Google's compute engine.

Thanks a lot for reading. I hope you liked this article. Please share your valuable suggestions and feedback. Write in the comment box in case you have any questions. Have a good day!


Similar Articles