Home / Google Cloud / Containerize a .NET 6 Application And Publish A Docker Container Image To google cloud registry

Containerize a .NET 6 Application And Publish A Docker Container Image To google cloud registry

Last updated on May 11th, 2023

Estimated reading time: 4 minutes


In this article, you’ll learn how to containerize a .NET 6 application with Docker. Containers have many features and benefits, such as being an immutable infrastructure, providing a portable architecture, and enabling scalability. The image is pushed to google container registry.

If you are new to Google Cloud Platform refer our article on Google cloud free tier account – Full Stack Tutorials Hub .


  • Google free tier or paid account.
  • Basic knowledge of using Google cloud services.

Create .NET app Using Google Cloud Shell

Login in to Google cloud console and navigate to projects menu and create a new project.

Google Cloud Project Set up

It would take some time for your project to set up in google cloud. Once the project is ready, navigate to the new project.

Search for cloud shell. Select cloud shell editor. This would provision a new VM instance in the cloud.

This VM instance would be all .NET 6 SDK and Docker pre-installed.

Create a .NET 6 project using below CLI command.

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

 Publish the application using the below command.

dotnet publish -c Release

Set up the Docker file

In the cloud shell, navigate to the project explorer view and add Docker File at the project level

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

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

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

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

he Dockerfile file is used by the docker build command to create a container image. This file is a text file named Dockerfile that doesn’t have an extension.

The FROM keyword requires a fully qualified Docker container image name. The Microsoft Container Registry (MCR, mcr.microsoft.com) is a syndicate of Docker Hub — which hosts publicly accessible containers. The dotnet segment is the container repository, whereas the sdk or aspnet segment is the container image name. The image is tagged with 6.0, which is used for versioning.

Save the Dockerfile file. The directory structure of the working folder should look like the following

Project folder structure

From your terminal, run the following command:

docker build -t custom-image -f Dockerfile .

Docker will process each line in the Dockerfile. The . in the docker build command sets the build context of the image. The -f switch is the path to the Dockerfile. This command builds the image and creates a local repository named counter-image that points to that image. After this command finishes, run docker images to see a list of images installed:

docker images

Single run

Docker provides the docker run command to create and run the container as a single command. This command eliminates the need to run docker create and then docker start. You can also set this command to automatically delete the container when the container stops. Port ‘8080’ of the container is mapped to port ‘32767’ of the host.

docker run -p 32767:8080 custom-image

Push Image to Google Container Registry

Before pushing the docker image to Google Container Registry, we need to tag image in below format

docker tag custom-image gcr.io/buildcustomimage/custom-image:0.1

Enable google container registry Api under our working project

Push the image to GCR

docker push gcr.io/buildcustomimage/custom-image:0.1
Container Image Successfully pushed to Google Registry

In this article, you have learned how to build custom Images for ASP.NET Core and push it to google container Registry.

Next steps

Deploy ASP.NET Core Containerize applications to Google App Engine

Run .NET6 app in Google App Engine Flexible environment – Full Stack Tutorials Hub

Deploy ASP.NET Core Containerize applications to Google Kubernetes Engine

Google Kubernetes Services Archives – Full Stack Tutorials Hub

Scroll to Top