Google Cloud Run: Containers to Production in Seconds

Reading Time: 6 minutes

Google Cloud Run is a serverless platform that allows you to run containerized applications in google cloud environment. Step by step tutorial to containerize your apps, push them to Google Artifact Registry, and deploy them on Google Cloud Run while understanding the pricing model to optimize costs.

What is Google Cloud Run?

Google Cloud Run is a fully managed serverless platform that enables developers to deploy and run applications in containerized environments.

It is built on top of open standard Knative

  • ZERO infrastructure management
  • pay-per-use (CPU,Memory,Requests and Networking)

It abstracts the underlying infrastructure, providing developers with an effortless way to manage and scale applications without worrying about server provisioning, maintenance, or scaling issues.

Before deploying your application to Google Cloud Run, you need to containerize it.

Containerization involves packaging your application and its dependencies into a single container image, providing consistency across different environments.

This is Handon’s tutorial, and you can follow along.

Google cloud offers free credits worth 400 $ to learn and explore google cloud platform using free trial account. Learn more on how to create google cloud free trial account Google Cloud Free Credits – Google Cloud Tutorials .

Project set up using Cloud Shell

Search “cloud shell” in google cloud console and using terminal window execute below command.

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

Navigate to project folder

cd cloudrundemo

publish .NET Project

dotnet publish -c Release

his will build the application and compiles your app to the publish folder.

Log into cloud shell and navigate to the project explorer view.Copy paste below code into docker file. Update project name as per your projectname in Docker file.

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 ["cloudrundemo.csproj", "."]
RUN dotnet restore "./cloudrundemo.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "cloudrundemo.csproj" -c Release -o /app/build

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

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

To build custom image from your terminal, run the following command:

docker build -t cloud=poc-v1 -f Dockerfile .

To display list of all docker images execute below command.

docker images

To run application image locally execute below command

docker run -p 32767:8080 custom-image

Port ‘8080’ of the container is mapped to port ‘32767’ of the host.

Application is accessed using host port 32767 in web preview.

application running locally
Web Preview

To stop running container using control C command

To learn more about application containerization refer Containerizing An Application In 3 Easy Steps – Google Cloud Tutorials .

Push Container Image to Google Artifact Registry

  • Open the Repositories page in the Google Cloud console.
  • Click Create Repository.
  • Specify  artifactdemo as the repository name.
  • Choose Docker as the format and Standard as the mode.
  • Under Location Type, select Region and then choose the location us-central1.
  • Review configuration and click create.

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

docker tag cloudrun-poc-v1 us-east1-docker.pkg.dev/disco-serenity-412905/artifactdemo/cloudrun-poc-v1

Before you can push or pull images, configure Docker to use the Google Cloud CLI to authenticate requests to Artifact Registry.

To set up authentication to Docker repositories in the region us-central1, run the following command:

gcloud auth configure-docker us-central1-docker.pkg.dev

To push the Docker image, run the following command:

docker push us-east1-docker.pkg.dev/disco-serenity-412905/artifactdemo/cloudrun-poc-v1

Learn more about Google Artifact registry Store Container Images In Google Cloud Artifact Registry – Google Cloud Tutorials

Running Containers in Google Cloud Run

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

It is prerequisites to build and deploy container images to either artifact registry or google container registry.

Open Google Cloud Console – Log in to your Google Cloud Console account.

Enable Cloud Run API – If you haven’t already, enable the Cloud Run API in the API & Services section.

Search cloud run in google cloud console and click on create service.

Select the application image that needs to be deployed on cloud run service.

Cloud Run service

Alternative way is to build container images from source repository using cloud build.

This option will enable continuous integration and continuously deploy new revisions.

Enter a valid service name and select cloud run region. Each Cloud Run service or job resides in a region. Customer data associated with the service or job is stored in selected region.

To know more about cloud run supported regions refer Cloud Run locations  |  Cloud Run Documentation  |  Google Cloud .

Service name and region cannot be altered once service is created.

In CPU allocation and pricing there are 2 options

CPU is only allocated during request processing – In this CPU is allocated only when there are requests made for the applications or invocations. You will be charged per request.

CPU is always allocated – In this option Container is always running and You are charged for the entire lifecycle of the container instance.

next steps are to select auto scaling configuration.

specify minimum and maximum number of instances.

In real world applications minimum instances should always be greater than 0 to prevent cloud start issue.

CPU allocation and pricing

If minimum instance is specified as 0 and in case new request comes for the application than cloud run will create a new container and there would be initial delay for this process. This initial delay is called as cold start.

Authentication

Select authentication type for your service.

Allow All traffic and allow unauthenticated invocations.

By default, container images are encrypted with a Google-managed key. If you need to manage your encryption you can use a customer-encrypted key instead.

Review request configuration and click on create.

cloud run home screen

Once cloud run deployment is successful Google cloud generates a custom URL. Application is accessible using this custom URL.

Multiple version Deployments and traffic splitting in Google Cloud run

Cloud run supports multiple versions of service to run in parallel.

This is primarily important when rolling out new versions of service while old version is still serving traffic.

Repeat step number 3 and create a new image with name cloudrun-poc-v2

In cloud Run service click on EDIT and Deploy new Revision.

Select new Image from artifact registry and uncheck Serve this revision immediately check box.

Under Revisions Tab select new version and assign Traffic as per requirement.

Traffic Assignment

Once testing of new version is completed 100 % traffic can be routed to new version.

Cloud Run charges you only for the resources you use, rounded up to the nearest 100 millisecond. Your total Cloud Run bill will be the sum of the resource usage in the pricing table after the free tier is applied.

Refer for more details Pricing  |  Cloud Run  |  Google Cloud

FAQ

What types of applications are suitable for Google Cloud Run?

Google Cloud Run is well-suited for stateless applications that can handle requests independently. This includes web applications, APIs, microservices, and more. Applications that require persistent storage or background processing might be better suited for other Google Cloud services.

How does scaling work in Google Cloud Run?

Google Cloud Run automatically scales your containers based on incoming request traffic. It can scale from zero to handle thousands of requests per second, and it automatically scales down when the demand decreases. You are billed based on the number of vCPU-seconds and memory used during the scaling.

Can I use my own custom domain with Google Cloud Run?

Yes, you can map your own custom domain to your Google Cloud Run service. This involves configuring the necessary DNS settings and updating your service configuration to use the custom domain.

What languages and runtimes does Google Cloud Run support?

Google Cloud Run supports containers with any language or runtime. As long as your application is packaged in a container, you can deploy it on Google Cloud Run. This flexibility allows you to use your preferred programming language and runtime environment.

How can I monitor and troubleshoot my applications on Google Cloud Run?

Google Cloud Run integrates with various Google Cloud monitoring and logging tools. You can use Cloud Monitoring and Cloud Logging to gain insights into your application’s performance, troubleshoot issues, and set up alerts for critical events.

Scroll to Top