doukuai3822
2016-10-11 15:57
浏览 342
已采纳

无法连接到在docker容器内运行的套接字服务器

I have a docker compose file which combines nginx and php like this:

nginx:
  image: nginx
  ports:
    - "80:80"
    - "2443:2443"
  links:
    - phpfpm
  volumes:
    - ./nginx/anonymous.conf:/etc/nginx/conf.d/anonymous.conf
    - ./logs/nginx-error.log:/var/log/nginx/error.log
    - ./logs/nginx-access.log:/var/log/nginx/access.log
    - ./public:/usr/share/nginx/html

phpfpm:
  image: php:fpm
  expose:
   - "2443"
  volumes:
    - ./public:/usr/share/nginx/html

I can see my website i.e index.php page on browser with the virtual host i have already made lets say anonymous.com

Now inside my phpfpm container i started a socket server based on Ratchet which is listening to port 2443

// bin/server.php
$webSocketServer = new WsServer(new Chat());
$server = IoServer::factory(
        new HttpServer($webSocketServer), 2443);
$server->run();

This is how i run my server inside phpfpm container

php /usr/share/nginx/html/bin/server.php 

My understanding is, since i have already exposed 2443 and my ngnix and phpfpm containers are linked. I would be able to connect to my socket server running on phpfpm container by going to telnet anonymous.com 2443

But its not getting connected. Here is the output

$ telnet anonymous.com 2443
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

Please note that previously when i had both nginx and php-fpm were on single container, every thing was working fine. So i am sure that there is nothing wrong with PHP. It just i cannot figure out how to access the socket server from outside world.

Thanks

Update If i use my phpfpm container ip and use it with port 2443 through browser, its working fine. but the thing is i cannot rely on container ip as its all dynamic.

图片转代码服务由CSDN问答提供 功能建议

我有一个docker compose文件,它结合了nginx和php,如下所示:

  nginx:
 image:nginx 
 ports:
  - “80:80”
  - “2443:2443”
 links:
  -  phpfpm 
卷:
  -  ./nginx/  anonymous.conf:/etc/nginx/conf.d/anonymous.conf 
  -  ./logs/nginx-error.log:/var/log/nginx/error.log
  -  ./logs/nginx-access。  log:/var/log/nginx/access.log 
  -  ./public:/usr/share/nginx/html

phpfpm:
 image:php:fpm 
 expose:
  - “2443”\  n卷:
  -  ./public:/usr/share/nginx/html
nn

我可以看到我的网站,即 index.php 我已经制作了虚拟主机的浏览器页面让我们说 anonymous.com

现在在我的phpfpm容器中我启动了一个基于的套接字服务器 Ratchet 正在侦听端口 2443

  // bin / server.php 
 $ webSocketServer = new WsServer(new Chat(  )); 
 $ server = IoServer :: factory(
新的HttpServer($ we  bSocketServer),2443); 
 $ server-> run(); 
   
 
 

这就是我在 phpfpm 中运行服务器的方法 容器

  php /usr/share/nginx/html/bin/server.php 
   
 
 

我的理解是 因为我已经公开了 2443 ,并且我的 ngnix phpfpm 容器已被链接。 我可以通过转到 telnet anonymous.com 2443

连接到运行在 phpfpm 容器上的套接字服务器但是它不是 连接。 这是输出

  $ telnet anonymous.com 2443 
Trying 127.0.0.1 ... 
连接到localhost。
Escape字符是'^]'。
关闭 外国主机。
   
 
 

请注意,以前当我同时使用nginx和php-fpm在单个容器上时,每件事都运行良好。 所以我确信PHP没有任何问题。 它只是我无法弄清楚如何从外部世界访问套接字服务器。

谢谢

更新 如果我 使用我的 phpfpm 容器ip并通过浏览器端口2443使用它,它工作正常。 但事情是我不能依赖容器ip作为它的全部动态。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyu1656 2016-10-11 17:44
    已采纳

    You should use the name of the service as the hostname. So to connect from nginx to phpfpm use phpfpm:2443

    点赞 评论
  • doutangguan2460 2018-12-11 17:10

    I had a similar issue (same output) using a socket server based on Ratchet. Mine was only a PHP container (= no nginx/apache/...). It was because of the 3rd parameter given to the factory :

    $server = IoServer::factory(
      new HttpServer($webSocketServer), 2443, '127.0.0.1'
    );
    

    Removing 127.0.0.1 (and then use the default 0.0.0.0 value) solved the problem.

    点赞 评论

相关推荐 更多相似问题