Docker not able to connect via host IP or localhost

I have created a docker image which runs kafka inside the container. I have also done port mappings in the docker-compose.yml for mapping the host ports with container ports.

After bringing up the docker image (using docker-compose), I am able to see that the docker image is up and running and a container is also started as shown below:

  • spring-cloud-config in docker-compose https certificate not found
  • How to set a specific (fixed) IP address when I create a docker machine or container?
  • Sync folder in docker image from Jhipster
  • Reach one concourse task's container from another one
  • Run graphical process in Docker container, detach and reconnect to the running GUI
  • Docker: Can a secret created and deleted in a single RUN command be recovered from the resulting image?
  • e39cd6452307        dockerregistry:5000/confluent:latest   "/run.sh"              10 hours ago        Up 10 hours         0.0.0.0:8081->8081/tcp, 0.0.0.0:2108->2181/tcp, 0.0.0.0:9920->9092/tcp   confluent
    

    ‘ps-ef | grep docker’ also provides me the following result:

    root      6594  5257  0 Jan17 ?        00:00:00 docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9920 -container-ip 172.17.0.1 -container-port 9092
    root      6605  5257  0 Jan17 ?        00:00:00 docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8081 -container-ip 172.17.0.1 -container-port 8081
    root      6617  5257  0 Jan17 ?        00:04:22 docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 2108 -container-ip 172.17.0.1 -container-port 2181
    

    Weird thing starts here:

    If I try to connect to service running inside the container from my host machine by using container IP and port (IP: 172.17.0.1, Port: 2181), my connection is successful and I get proper response.

    However, If I try to connect to the service from host machine using the mapped host IP and port(using 10.xx.xx.xx:2108), I get a connection refused message. (xx is just used for masking my machine ip)

    Also, ‘telnet 10.xx.xx.xx 2108’ works fine.

    What could be the problem in this case. I have docker version 1.7.1 and docker-compose version 1.5.0

    Please help.

  • How to set Java heap size (Xms/Xmx) inside Docker container?
  • How to select file to copy based on Docker environment variable?
  • How does Docker for Windows run Linux containers?
  • How do you get debugging symbols working in linux perf tool inside Docker containers?
  • Understanding overlay networks in docker
  • How to view the namespaces range of a Docker container to choose correct Samba domain member idmap range?
  • 2 Solutions collect form web for “Docker not able to connect via host IP or localhost”

    As i can see that it is working fine inside the container using container ip and port ,that points to

    docker-compose

    file , may be port mapping is not done there properly.

    Could you please upload your docker-compose because the only issue is of port as it is not exposed properly.
    Also you can check in your docker run command if the

    -p port:port

    is actually the port you want to expose or not.

    I had the same problem and was able to fix it by specifying the host that the server within the container uses. See my answer here: Access Docker forwarded port on Mac

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.