Last week I continued my journey exploring gRPC by creating a simple example. Today, I am going to take that project one step further by adding Docker support to the server. Most people agree that containerization is the way to go. So in order to go with the times, gRPC will have to behave in a container. This shouldn’t be a problem since you can poke holes into the container to enable communication in the outside world.
The first thing I did was add Docker support to the server project. There are many different ways to do this but since this isn’t a post on how to do Docker, I will do it the simplest way. I right clicked the project file and then click Add -> Docker support.
Since one of the benefits of Docker is portability, I decided to choose Linux on the Docker File Options dialog. I love seeing my .Net code running on Linux
Doing this adds a Docker file to the project. This file is used to build docker images. At this point I don’t want to change the file too much. One change I did need to make was to punch the hole in the container so that the RPC calls can be made. I do this by adding an Expose value setting it to the port that we are listening on the server. In our case that was 50051.
Now I am ready to build and deploy my server. When doing this, I want to change the build to be a Release build. Then all I have to do his hit Run button. Note that it is already set to Docker.
Let’s launch PowerShell and see what we have done. If you type: Docker ps You will see the containers that are running. You will notice that the port mapping is quite right. I want to map 50051 externally to 50051 internally I do that by stopping the container, removing the container and running the container with the port mappings I want.
Here is the command I ran create the mappings: docker run -p 50051:50051 –name grpcdemo grpcsimpleserver
Now that I have the server up and running, it is time to see if I can talk to it. So I spin up the client project and all is well with the world…Or is it?
Why am I getting this exception? I did some Googling on Bing and figured out that I was setting my server ports incorrectly. When I setup the server I had it listening on 127.0.0.1:50051. Since my Client lives out side the container it could never reach that address. So I had to change it to listen on 0.0.0.0:50051.
Now I run the Client again and get success!
This was a fun little exercise. I enjoyed kicking the tires on gRPC. I see where it can be a powerful tool when building distributed systems. I will continue to play and see what it can do. As always you can get the Here. I modified the project from the last post. I hope you enjoyed following along.