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

无法连接到在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.

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)