简体   繁体   English

Docker无法访问容器外部的端口

[英]Docker unable to access port outside the container

I am running zookeeper( not thru docker ) on my localhost with port 2181. 我正在本地主机上使用端口2181运行zookeeper(不是通过docker)。

Also, trying to run a docker image thru docker compose as follows. 另外,尝试通过docker compose运行docker镜像,如下所示。

#
  # 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. Zookeepr正在侦听端口2181,但是我的docker进程仍然无法连接到zookeeper端口2181。

Is there anything missing? 缺少什么吗?

Thanks 谢谢

You are pointing to localhost from inside your docker container. 您是从docker容器内部指向localhost 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. 这意味着您的Docker容器尝试在该特定端口上而不是在服务器的localhost上连接到容器本身(localhost)。 You'll need to specify a specific IP to connect or you can run your docker container with the --net="host" option. 您需要指定要连接的特定IP,或者可以使用--net="host"选项运行--net="host"容器。

$ 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. 示例:我创建一个apache容器(默认)并在主机的127.0.0.1:80上映射端口。 So on my server I can curl 127.0.0.1:80 . 所以在我的服务器上我可以卷曲127.0.0.1:80

$ docker run -d -p 80:80 httpd

curl 127.0.0.1:80 from host works: 从主机作品中curl 127.0.0.1:80

$ 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 : 如果我启动安装了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. 连接被拒绝,因为该容器位于默认网桥网络内,并尝试在端口80上卷曲自身。

Now I start the container inside the host network: 现在,我在host网络中启动容器:

$ 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 --net=host转换为--net=host -compose文件意味着您必须添加network_mode: host

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM