Dockerizing gRPC service
source link: https://dotnetgik.wordpress.com/2020/09/08/dockerizing-grpc-service/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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
In this article lets try to explore how we can run our gRPC service in the docker lets check below steps to achieve that
- 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
- -d : specifies the detached mode means the command this means the command wont block command prompt and it will return to the command prompt
- -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
- –name : specifies the name of the docker container
- 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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK