From e8691533dc1f20ddb6110d72e10ed55f783c2206 Mon Sep 17 00:00:00 2001 From: Thomas Tacquet Date: Fri, 16 May 2025 17:41:07 +0200 Subject: [PATCH 1/2] feat(content): .NET example --- README.md | 1 + containers/csharp-hello-world/Dockerfile | 18 +++++ .../csharp-hello-world/HelloWorldApp.csproj | 9 +++ containers/csharp-hello-world/Program.cs | 11 +++ containers/csharp-hello-world/README.md | 73 +++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 containers/csharp-hello-world/Dockerfile create mode 100644 containers/csharp-hello-world/HelloWorldApp.csproj create mode 100644 containers/csharp-hello-world/Program.cs create mode 100644 containers/csharp-hello-world/README.md diff --git a/README.md b/README.md index 1c30b2f..6160a31 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ Table of Contents: | **[Terraform NGINX hello world](containers/terraform-nginx-hello-world/README.md)**
A minimal example running the base NGINX image in a serverless container deployed with Terraform. | N/A | [Terraform] | | **[Triggers with Terraform](containers/terraform-triggers/README.md)**
Configuring two SQS triggers, used to trigger two containers, one public, one private. | N/A | [Terraform] | | **[gRPC HTTP2 in Go](containers/grpc-http2-go/README.md)**
A Go gRPC Container using http2 | Go/Protobuf | [CLI] | +| **[.NET C#](containers/csharp-hello-world)**
A .NET C# Container hello world | C# .NET | [CLI] | ### ⚙️ Jobs diff --git a/containers/csharp-hello-world/Dockerfile b/containers/csharp-hello-world/Dockerfile new file mode 100644 index 0000000..3861d62 --- /dev/null +++ b/containers/csharp-hello-world/Dockerfile @@ -0,0 +1,18 @@ +# Use the official .NET runtime image. +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +WORKDIR /app +EXPOSE 8080 + +# Build stage +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +WORKDIR /src +COPY *.csproj . +RUN dotnet restore +COPY . . +RUN dotnet publish -c Release -o /app + +# Final stage +FROM base AS final +WORKDIR /app +COPY --from=build /app . +ENTRYPOINT ["dotnet", "HelloWorldApp.dll"] diff --git a/containers/csharp-hello-world/HelloWorldApp.csproj b/containers/csharp-hello-world/HelloWorldApp.csproj new file mode 100644 index 0000000..1b28a01 --- /dev/null +++ b/containers/csharp-hello-world/HelloWorldApp.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/containers/csharp-hello-world/Program.cs b/containers/csharp-hello-world/Program.cs new file mode 100644 index 0000000..77e35ed --- /dev/null +++ b/containers/csharp-hello-world/Program.cs @@ -0,0 +1,11 @@ +var builder = WebApplication.CreateBuilder(args); + +// Get PORT from environment, default to 8080 if not set +var port = Environment.GetEnvironmentVariable("PORT") ?? "8080"; +builder.WebHost.UseUrls($"http://*:{port}"); + +var app = builder.Build(); + +app.MapGet("/", () => "Hello from Scaleway!"); + +app.Run(); diff --git a/containers/csharp-hello-world/README.md b/containers/csharp-hello-world/README.md new file mode 100644 index 0000000..29e8922 --- /dev/null +++ b/containers/csharp-hello-world/README.md @@ -0,0 +1,73 @@ +# C# .NET hello world + +This example demonstrates the deployment of a simple C# http service on Scaleway Serverless Containers. + +For this example, we will use the CLI to deploy the container, but you can use [other methods](https://www.scaleway.com/en/docs/serverless/containers/reference-content/deploy-container/). + +## Workflow + +Here are the different steps we are going to proceed: + +- Quick set-up of Container Registry to host our .NET container +- Deploy the Serverless Container +- Test the container + +## Deployment + +### Requirements + +To complete the actions presented below, you must have: +- installed and configured the [Scaleway CLI](https://www.scaleway.com/en/docs/developer-tools/scaleway-cli/quickstart/) +- installed [Docker](https://docs.docker.com/engine/install/) to build the image + +### Building the image + +1. Run the following command in a terminal to create Container Registry namespace to store the image: + + ```bash + scw registry namespace create name=hello-dotnet + ``` + + The registry namespace information displays. + +1. Copy the namespace endpoint (in this case, `rg.fr-par.scw.cloud/hello-dotnet`). + +1. Log into the Container Registry namespace you created using Docker: + + ```bash + docker login rg.fr-par.scw.cloud/hello-dotnet -u nologin --password-stdin <<< "$SCW_SECRET_KEY" + ``` + + At this point, you have correctly set up Docker to be able to push your image online. + +1. In a terminal, access this directory (containing the Dockerfile), and run the following command to build and tag the image: + + ```bash + docker build --platform linux/amd64 -t rg.fr-par.scw.cloud/hello-dotnet/dotnet:v1 . + ``` + +1. Tag and push the image to the registry namespace: + + ```bash + docker push rg.fr-par.scw.cloud/hello-dotnet/dotnet:v1 + ``` + +### Deploying the image + +In a terminal, run the following command to create a Serverless Containers namespace: + + ```bash + scw container namespace create name=hello + ``` + The namespace information displays. + +1. Copy the namespace ID. + +1. Run the following command to create and deploy the container: + + ```bash + scw container container create namespace-id= name=hello registry-image=rg.fr-par.scw.cloud/hello-dotnet/dotnet:v1 + ``` + The container information displays. + +1. Copy the DomainName (endpoint) to test your container, you can put the endpoint in your web browser for testing. From b7630af0e4d72c2953508d75a4699e492b77fb89 Mon Sep 17 00:00:00 2001 From: Thomas Tacquet Date: Tue, 20 May 2025 08:24:24 +0200 Subject: [PATCH 2/2] remove port metadata --- containers/csharp-hello-world/Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/containers/csharp-hello-world/Dockerfile b/containers/csharp-hello-world/Dockerfile index 3861d62..a70efc6 100644 --- a/containers/csharp-hello-world/Dockerfile +++ b/containers/csharp-hello-world/Dockerfile @@ -1,7 +1,6 @@ # Use the official .NET runtime image. FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base WORKDIR /app -EXPOSE 8080 # Build stage FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build