Search code examples
dockerportgrpc

How to access services on the docker container and host simultaneously


I have a service on docker 8888 and a local grpc service running on 0.0.0.0:50051 and I want to connect to that port through the docker container.

Docker run command:

sudo docker run -ti -p 8888:8888 -p 50051:50051 -v $(pwd):<folder> -e <aws-credentials> <directory> jupyter

Error:

docker: Error response from daemon: 
driver failed programming external connectivity on endpoint strange_lamarr (<string>): 
Error starting userland proxy: listen tcp4 0.0.0.0:50051: bind: address already in use.
ERRO[0000] error waiting for container: context canceled 

Here the first service is ran through docker, so if I try adding network="host" to the command, the first service fails. I just want to use local service for the second port.

Docker is trying to use the port instead of connecting to that port, I have tried copying and creating an instance of the service on docker itself but it uses a bunch of local graphics that I cannot really replicate on docker easily.


Solution

  • The port mapping -p 50051:50051 is used when you need to access the container from the host.

    When you need to access the host from the container, you need to add a hostname that's mapped to the host-gateway. The convention is to call it host.docker.internal. You do that by adding --add-host=host.docker.internal:host-gateway as an option to your docker run command, so it becomes

    sudo docker run -ti -v $(pwd):<folder> -e <aws-credentials> <directory> --add-host=host.docker.internal:host-gateway jupyter
    

    Then you can connect from the container by using the hostname host.docker.internal and port 50051.