How To Create Docker Image Of .NET 6 Workloads And Push It To Google Container Registry

Introduction

This article will try to understand how to use the Google container registry. We would also set up a new .NET 6 application and build a custom image using Docker. In the last step, we would push this image to the Google container registry.

Container Registry is a service for storing private container images. It provides a subset of features offered by Artifact Registry, a universal repository manager, and the recommended service for managing container images and other artifacts in Google Cloud.

In this article, we will cover the following topics:

  • Leverage Cloud shell to set up .NET 6 Project in the VM machine
  • Set up a Docker file and build a custom image
  • Push the image to the google container registry.

Prerequisites

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

Step 1. Leverage Cloud shell to set up .NET 6 Project in the VM machine 

Navigate to the projects menu and create a new project.

How to Create Docker Image of .NET 6 workloads and push it to Google Container registry

 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

Step 2. Set up the Docker file and build a custom image

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

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

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 ["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
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Buildcustomnetcoredemo.dll"]

Create a docker image using the below command.

docker build -t custom-image -f Dockerfile .

Confirm image is created using the command.

docker images

How to Create Docker Image of .NET 6 workloads and push it to Google Container registry

Step 3. Push the image to the google container registry.

 Before pushing the docker image to GCR, we need to tag the image.

 Tag the image

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

Enable google container registry Api under our working project

 How to Create Docker Image of .NET 6 workloads and push it to Google Container registry

Push the image to GCR

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

Navigate to Google container registry Repository to verify image has been pushed successfully.

How to Create Docker Image of .NET 6 workloads and push it to Google Container registry

Summary

In this article, we have learned how to work with GCR

In the upcoming articles, we will see how to push custom images to GAE and GKE 

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