I have a git repo with the given folder structure
grpc-repo
|---grpc-client (:8083)
|---grpc-server (:9000)
|---docker-compose.yml
|---Dockerfile
|---Dockerfile2
Now, these gRPC client-server project is running seamless normally. But then, I need to dockerize this project.
Given below is my docker-compose.yml
version: "3.3"
services:
client:
container_name: grpc-client
build:
dockerfile: Dockerfile
context: .
volumes:
- .:/app
ports:
- 8083:8083
networks:
- my-network
server:
container_name: grpc-server
build:
dockerfile: Dockerfile2
context: .
volumes:
- .:/app
ports:
- 9000:9000
networks:
- my-network
networks:
my-network:
driver: bridge
After dockerizing the project, I am easily able to connect to the gRPC client.. but the client is not able to connect to the gRPC server.
It says ...
rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp :9000: connect: connection refused
This is how I am creating a connection to the server in my golang code
conn, err := grpc.Dial(":9000", grpc.WithInsecure())
if err != nil {
log.Fatalf("could not connect: %s", err)
}
Can anyone help me with what am I doing wrong here ? Thanks in advance !
CodePudding user response:
To access a gRPC running in a docker container from another container you have to use DNS resolution.
grpc.Dial("dns:///grpc-server:9000", grpc.WithInsecure())
where grpc-server
is the container name you declared in container_name: grpc-server