Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloudrun-net folder with a .NET 8 web app and instructions to deploy to Cloud Run #2919

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions applications/clouddemo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

This folder contains sample ASP.NET applications:

* [`net4/`](net4/) contains a ASP.NET MVC 5 application that can be run in IIS on Windows.
* [`net4/`](net4/) contains a ASP.NET MVC 5 application that can be run in IIS on Windows.
* [`netcore/`](netcore/) contains a ASP.NET MVC Core application that can be run on Linux or Windows.
* [`cloudrun-net/`](cloudrun-net/) contains a .NET 8 ASP.NET MVC Core web application that can be run locally, and on Cloud Run.

## License

All files in this repository are under the
[Apache License, Version 2.0](LICENSE.txt) unless noted otherwise.
[Apache License, Version 2.0](LICENSE.txt) unless noted otherwise.
56 changes: 56 additions & 0 deletions applications/clouddemo/cloudrun-net/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# Copyright 2025 Google LLC
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

#------------------------------------------------------------------------------
# Build the web application
#------------------------------------------------------------------------------

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY helloworld-csharp/*.csproj ./
RUN dotnet restore

#------------------------------------------------------------------------------
# Create publishing artifacts.
#------------------------------------------------------------------------------

# copy everything else and build app
COPY helloworld-csharp/. ./
RUN dotnet publish ./helloworld-csharp.csproj -c release -o /app --no-restore

#------------------------------------------------------------------------------
# Copy publishing artifacts.
#------------------------------------------------------------------------------

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
EXPOSE 8080

WORKDIR /app
COPY --from=build /app ./

#------------------------------------------------------------------------------
# Run application in Kestrel.
#------------------------------------------------------------------------------

ENTRYPOINT ["dotnet", "helloworld-csharp.dll"]
59 changes: 59 additions & 0 deletions applications/clouddemo/cloudrun-net/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Cloud Run - .NET Demo

This walkthrough creates a basic [ASP.NET MVC Core](https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc) web application.

## Prerequisites

To create and build the application, you'll need

* [.NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)
* [Google Cloud account and project](https://cloud.google.com/resource-manager/docs/creating-managing-projects)

## Build a Linux Docker image
1. Open a developer command prompt on your machine. Alternatively, open [Cloud Shell](https://cloud.google.com/shell/docs/using-cloud-shell).
1. Build a Docker image, using the binaries published to the output folder:

`docker build -t cloudrun-net .`

## Run the container locally
1. Run the container:

`docker run --rm -it -p 8080:8080 cloudrun-net`

1. Point your web browser to http://localhost:8080/ and verify that the application is
working properly.

## Push the container image to Artifact Registry

1. If you haven't done so already, [create an Artifact Registry Docker repository](https://cloud.google.com/artifact-registry/docs/docker/store-docker-container-images#create).
1. Retrieve the URI of your Artifact Registry Docker repository:
<pre>gcloud artifacts repositories describe <b>REPO-NAME</b> \
--location=<b>LOCATION</b> \
--format="value(registryUri)"</pre>
Replace:
- `REPO-NAME` with the name of your Docker repository
- `LOCATION` with the location (region / multi-region) where the Docker repository was created

1. Create a environment variable to hold the Docker image's URI
<pre>export IMAGE_URI=<b>REGISTRY-URI</b>/cloudrun-net</pre>
Replace `REGISTRY-URI` with the URI you retrieved in the previous step.
1. Tag the image:
```bash
docker tag cloudrun-net $IMAGE_URI
```
1. Push the image to Artifact Registry:
```bash
docker push $IMAGE_URI
```

## Deploy to Cloud Run
1. Deploy the container image to Cloud Run
<pre>gcloud run deploy cloudrun-net \
--region=<b>REGION</b> \
--image=$IMAGE_URI \
--allow-unauthenticated</pre>
Replace `REGION` with the region you want to deploy the Cloud Run service to.

If you are prompted to enable additional APIs on the project, for example, the Cloud Run API, respond by pressing y.

1. Point your web browser to the Service URL returned by the command and verify that the application is working properly.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var builder = WebApplication.CreateBuilder(args);

var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
var url = $"http://0.0.0.0:{port}";
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";

var app = builder.Build();

app.MapGet("/", () => $"Hello {target}!");

app.Run(url);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>helloworld_csharp</RootNamespace>
</PropertyGroup>

</Project>
16 changes: 16 additions & 0 deletions applications/clouddemo/cloudrun-net/runTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

dotnet restore helloworld-csharp/ --force
dotnet publish helloworld-csharp/ --no-restore -c Release -f net8.0 2>&1 | %{ "$_" }