Dockerizing gRPC service

    In last articles we have seen how we can develop the gRPC service in this article lets check how we can run gRPC service in docker container. if you do not have read about the gRPC service and its basics suggest you go through following articles

  1. gRPC Basics
  2. gRPC Protobuff and beyond
  3. gRPC + Blazor server
  4. To Do App with Blazor wasm and gRPC

In this article lets try to explore how we can run our gRPC service in the docker lets check below steps to achieve that

  1. Add gRPC service

We have seen how to add the gRPC service in the  previous articles but this time lets change the way we add the gRPC service

Add Project

Here while adding the gRPC service lets just enable the docker support which will add the basic support to run our service in the docker.

Once we have added the project you will see the following project structure

We can see the docker file is added with the project .

Docker File

Once we are done with adding the service lets try to dissect the docker file.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS runtime
WORKDIR /myworkingdirectory
EXPOSE 443
EXPOSE 80

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

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

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

Generally docker is build around the concept of image layers every layer builds on previous layer after adding dependencies , package and runtime and finally the image produced contains everything right from your compiled application and then the things needed to run that application . If we see above docker file we see two images we are using in the application first one being the aspnet:3.1-buster-slim and another is sdk:3.1-buster out of this first image aspnet:3.1-buster-slim  is used to run the application and second image sdk:3.1-buster  is used for the compiling publishing the application

Docker stages

Generally, docker file is divided  into the stages each stage starts with ‘FROM AS’  so if we look at our docker file we can see we have 4 stages in order to get the our service up and running in the docker let’s see what are these stages

Stage 1 : Set run time working directory and ports

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS runtime
WORKDIR /myworkingdirectory
EXPOSE 443
EXPOSE 80

In this stage we are setting up the run time here in our case we are setting it to .net core 3.1 then we are setting up the working directory and exposing the ports 443 and 80 of the docker

Stage 2 :  Build , Restore and copy

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

In this stage what we do is we use the image sdk:3.1-buster which is sdk image and here

we run the dotnet restore command which basically restores .net packages then we build the project using dotnet build command  

Stage 3 : Publish the App

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

This step created publishable version of our service

Stage 4 : final  

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

This step copies the published version of the app to the base stage which we have created at the start of the app here we copy the stuff from publish stage and app/publish folder to working directory

Docker Build

We have seen all the docker stages now lets check how to build the image the command that we use to build the image is as follows

                                     docker build -t grpcdocker .

In this -t provides the name of the image and the . at the end of the command tells to use the current folder as the context .

To build this Open our solution folder at the command prompt and just run this command on the command prompt you will be able to see the following outputs

Now that image is been build now its time to run the container

Run the Container   

To run the container, we are going to use following command

              docker run -d -p 5000:443 –name grpcdockerservice grpcdocker

in this command

  1. -d :  specifies the detached mode means the command this means the command wont block command prompt and it will return to the command prompt
  2. -p : specifies port it maps the port of host computer 5000 to the docker port 443 so whenever the traffic hits the localhost:5000 it will forward it to the docker port 443
  3. –name : specifies the name of the docker container
  4. Final parameter is the image that we have built in the previous step

Once we run the command, we get the following output

The output string that we got is the docker container Id which will be unique id

To see if the docker is running run following command which lists all the docker containers running in the host machine

It will show the status of the application running which we have created just a minutes ago .

This was about running gRPC service in the docker in next article lets see how to consume the service into the client app

Source code for above article is available at here

References

  1. https://marcroussy.com/2020/05/01/docker-build-process-for-dotnet/
  2. https://marcroussy.com/2020/05/01/docker-build-process-for-dotnet/
  3. https://docs.microsoft.com/en-us/dotnet/architecture/grpc-for-wcf-developers/docker

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s