Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker unable to access port outside the container

I am running zookeeper( not thru docker ) on my localhost with port 2181.

Also, trying to run a docker image thru docker compose as follows.

#
  # Mesos
  #
  mesos-master:
    image: mesosphere/mesos-master:1.0.3
    restart: always
    privileged: true
    network_mode: host
    volumes:
      - ~/mesos-data/master:/tmp/mesos
    environment:
      MESOS_CLUSTER: "mesos-cluster"
      MESOS_QUORUM: "1"
      MESOS_ZK: "zk://localhost:2181/mesos"
      MESOS_PORT: 5000
      MESOS_REGISTRY_FETCH_TIMEOUT: "2mins"
      MESOS_EXECUTOR_REGISTRATION_TIMEOUT: "2mins"
      MESOS_LOGGING_LEVEL: INFO
      MESOS_INITIALIZE_DRIVER_LOGGING: "false"

Zookeepr is listening on port 2181, but still my docker process is unable to connect to the zookeeper port 2181.

Is there anything missing?

Thanks

like image 337
user1578872 Avatar asked Aug 31 '25 04:08

user1578872


1 Answers

You are pointing to localhost from inside your docker container. This means that your docker container tries to connect to the container itself (localhost) on that specific port and NOT on the localhost of your server. You'll need to specify a specific IP to connect or you can run your docker container with the --net="host" option.

$ docker run -d --net="host" ...

This will create your container inside the host network and not inside the default bridge network.

Example: I create a apache container (default) and map port on 127.0.0.1:80 of the host. So on my server I can curl 127.0.0.1:80.

$ docker run -d -p 80:80 httpd

curl 127.0.0.1:80 from host works:

$ curl localhost:80
<html><body><h1>It works!</h1></body></html>

If I start a help container with curl installed in which I will curl 127.0.0.1:80:

    $ docker run --rm tutum/curl /bin/bash -c 'curl 127.0.0.1:80'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused

Connection is refused because the container is inside the default bridge network and tries to curl itself on port 80.

Now I start the container inside the host network:

$ docker run --net=host --rm tutum/curl /bin/bash -c 'curl 127.0.0.1:80'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    45  100    45    0     0   8573      0 --:--:-- --:--:-- --:--:-- 22500
<html><body><h1>It works!</h1></body></html>

The --net=host translated to your docker-compose file means you have to add network_mode: host

like image 103
lvthillo Avatar answered Sep 02 '25 20:09

lvthillo