Deploying grpc service on docker

Himanshu Chaudhary
2 min readSep 13, 2020

Docker has been quite popular now a days to implement services and deploy anywhere. No dependency on host os lead to its growth. This story will be a continuation of part-1 of two parts story on creating a grpc service using golang.

We deployed a golang grpc service on local host in part-1. Docker provide an easy way to create and deploy container anywhere without worrying about the dependencies and packages required to run the services.

Multi-stage builds are the way to create small size images which contains only required packages. Dockerfile to create multi-stage build contains more than one FROM statement which will use a different base and copy binary created in the previous stage to next stage. Lets dive into creating a multi-stage build dockerfile.

In above Dockerfile, we have created a builder image to install all the packages required to generate a golang binary, then copy the binary to new base image and run the service using bash to pass environment variables to grpc server.

cd into the directory or provide the path to dockerfile to create a build.

docker build -t rpc <path_to_dockerfile>

The grpc service will throw an error when we try to access it using grpc client, since docker containers cannot be able to connect to local mySQL database. To verify the statement, we can try to access running docker service using grpc client as mentioned below.

go run cmd/client-grpc/main.go -server=$(host_ip):9090

Since docker container has its own host, docker treats any local service like mysql database running in our localhost as a remote service. In order to connect to remote service we need to provide a host. Docker provides a way to add host to the running container. This way we can connect to any service running outside of docker container.

docker run --env-file env_file --add-host=mysql_server:$(host_ip) -d -p 9090:9090 --name rpc-server rpc:latest

host_ip in above command will be the ip of your system which we can get using ifconfig

Last step will be to access running docker service using grpc client in local host:

go run cmd/client-grpc/main.go -server=$(host_ip):9090

host_ip in above command will remain same as in docker container.

Concluding to the story, grpc services using protocol buffers are an efficient way of creating micro-services and available in many languages. grpc services use HTTP2 as protocol, hence we can create streaming service as well. Follow for updates….

--

--

Himanshu Chaudhary

Cloud Engineer, Kubernetes, Docker, CI/CD, Golang, gRPC, Micro services, git, Infrastructure as a Service, Storage, Compute, Protobuf, REST.